top + report + log --follow (issues #37 #38) - top 10 par lancements, digest markdown hebdo/mensuel, suivi du journal en direct
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
use super::*;
|
||||
use crate::events::{self, EventKind};
|
||||
use crate::output::{print_json, Table};
|
||||
use anyhow::Context;
|
||||
use serde::Serialize;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
@@ -79,6 +80,61 @@ pub fn aggregate(app: &App, agent: Option<&str>, period: Option<&str>) -> Result
|
||||
Ok(map.into_values().collect())
|
||||
}
|
||||
|
||||
/// Top 10 agents by launch count (issue #37).
|
||||
pub fn top(app: &App, period: Option<&str>) -> Result<Vec<AgentStats>> {
|
||||
let mut rows = aggregate(app, None, period)?;
|
||||
rows.sort_by(|a, b| b.launches.cmp(&a.launches).then(b.total_duration_s.cmp(&a.total_duration_s)));
|
||||
rows.truncate(10);
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
/// Markdown weekly/monthly report over the last N days (issue #37).
|
||||
pub fn report_md(app: &App, days: u32) -> String {
|
||||
let cut = chrono::Utc::now() - chrono::Duration::days(days as i64);
|
||||
let events = events::read_events(&app.events_dir(), 0);
|
||||
let mut total_launches = 0u64;
|
||||
let mut failures = 0u64;
|
||||
let mut changes = 0u64;
|
||||
let mut duration = 0u64;
|
||||
for ev in events {
|
||||
let ok = chrono::DateTime::parse_from_rfc3339(&ev.ts)
|
||||
.map(|t| t.with_timezone(&chrono::Utc) >= cut)
|
||||
.unwrap_or(false);
|
||||
if !ok {
|
||||
continue;
|
||||
}
|
||||
match ev.kind {
|
||||
EventKind::Start | EventKind::Run => total_launches += 1,
|
||||
EventKind::Install | EventKind::Update | EventKind::Uninstall => changes += 1,
|
||||
_ => {}
|
||||
}
|
||||
if let Some(d) = ev.duration_s {
|
||||
duration += d;
|
||||
}
|
||||
if let Some(c) = ev.exit_code {
|
||||
if c != 0 {
|
||||
failures += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let sessions = crate::sessions::load(app).map(|s| s.sessions.len()).unwrap_or(0);
|
||||
let top_rows = top(app, None).unwrap_or_default();
|
||||
let mut md = format!(
|
||||
"# Rapport agent-manager — {days} derniers jours\n\n- Lancements : {total_launches}\n- Échecs : {failures}\n- Changements (install/update/uninstall) : {changes}\n- Durée cumulée : {}s\n- Sessions enregistrées : {sessions}\n",
|
||||
duration
|
||||
);
|
||||
if !top_rows.is_empty() {
|
||||
md.push_str("\n## Top agents\n\n| Agent | Lancements | Durée | Échecs |\n|---|---|---|---|\n");
|
||||
for r in &top_rows {
|
||||
md.push_str(&format!(
|
||||
"| {} | {} | {}s | {} |\n",
|
||||
r.agent, r.launches, r.total_duration_s, r.failures
|
||||
));
|
||||
}
|
||||
}
|
||||
md
|
||||
}
|
||||
|
||||
pub fn run(app: &App, agent: Option<&str>, period: Option<&str>) -> Result<i32> {
|
||||
let rows = aggregate(app, agent, period)?;
|
||||
if app.json() {
|
||||
@@ -114,6 +170,66 @@ pub fn run(app: &App, agent: Option<&str>, period: Option<&str>) -> Result<i32>
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// am top : the most used agents (issue #37).
|
||||
pub fn run_top(app: &App, period: Option<&str>) -> Result<i32> {
|
||||
let rows = top(app, period)?;
|
||||
if app.json() {
|
||||
print_json(&rows);
|
||||
return Ok(0);
|
||||
}
|
||||
let color = app.color();
|
||||
let theme = app.theme();
|
||||
if rows.is_empty() {
|
||||
app.log.info("no usage recorded yet");
|
||||
return Ok(0);
|
||||
}
|
||||
let max_launches = rows.iter().map(|r| r.launches).max().unwrap_or(1);
|
||||
let mut table = Table::new(vec!["AGENT", "LAUNCHES", "DURATION", "FAIL"]);
|
||||
for r in &rows {
|
||||
table.row(vec![
|
||||
r.agent.clone(),
|
||||
format!("{} {}", r.launches, bar(r.launches, max_launches, 8)),
|
||||
format_duration(r.total_duration_s),
|
||||
if r.failures > 0 {
|
||||
crate::output::status_style("error", theme, color)
|
||||
} else {
|
||||
r.failures.to_string()
|
||||
},
|
||||
]);
|
||||
}
|
||||
print!("{}", table.render());
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// am report : write the markdown digest (issue #37).
|
||||
pub fn run_report(
|
||||
app: &App,
|
||||
last_week: bool,
|
||||
last_month: bool,
|
||||
output: Option<&std::path::Path>,
|
||||
) -> Result<i32> {
|
||||
let days: u32 = if last_week { 7 } else { 30 };
|
||||
let md = report_md(app, days);
|
||||
let path = match output {
|
||||
Some(p) => p.to_path_buf(),
|
||||
None => {
|
||||
let stamp = chrono::Local::now().format("%Y%m%d");
|
||||
app.events_dir().join(format!("report-{stamp}.md"))
|
||||
}
|
||||
};
|
||||
if app.dry_run() {
|
||||
app.log.dry(format!("would write {}", path.display()));
|
||||
return Ok(0);
|
||||
}
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
std::fs::write(&path, md)
|
||||
.with_context(|| format!("cannot write {}", path.display()))?;
|
||||
app.log.success(&format!("report written to {}", path.display()));
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// ASCII bar: relative weight of v against max.
|
||||
fn bar(v: u64, max: u64, width: usize) -> String {
|
||||
if max == 0 {
|
||||
|
||||
Reference in New Issue
Block a user