use std::collections::BTreeMap;
use std::path::Path;
use anyhow::Result;
use brok_core::metrics::{Event, percentile};
use console::style;
use serde::Serialize;
use crate::ui;
#[derive(Debug, clap::Args)]
pub struct Args {
#[arg(short, long, default_value_t = 7)]
days: u32,
#[arg(long)]
json: bool,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Latency {
pub count: usize,
pub p50_ms: Option<u64>,
pub p95_ms: Option<u64>,
}
impl Latency {
fn of(values: &[u64]) -> Self {
Self { count: values.len(), p50_ms: percentile(values, 50.0), p95_ms: percentile(values, 95.0) }
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Searches {
pub latency: Latency,
pub empty: usize,
pub semantic: usize,
pub with_links: usize,
pub with_memories: usize,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Answers {
pub latency: Latency,
pub first_token: Latency,
pub failed: usize,
pub models: BTreeMap<String, usize>,
pub skills: BTreeMap<String, usize>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Indexing {
pub latency: Latency,
pub files_indexed: u64,
pub last: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Failure {
pub at: String,
pub command: String,
pub message: String,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Errors {
pub count: usize,
pub commands: BTreeMap<String, usize>,
pub recent: Vec<Failure>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct Summary {
pub days: u32,
pub events: usize,
pub search: Searches,
pub answers: Answers,
pub index: Indexing,
pub errors: Errors,
}
const RECENT_ERRORS: usize = 5;
pub fn summarize(events: &[Event], days: u32) -> Summary {
let ms = |kind: &str, filter: &dyn Fn(&Event) -> bool| -> Vec<u64> {
events.iter().filter(|e| e.kind == kind && filter(e)).filter_map(|e| e.ms).collect()
};
let searches: Vec<&Event> = events.iter().filter(|e| e.kind == "search").collect();
let count = |filter: &dyn Fn(&&Event) -> bool| searches.iter().filter(|e| filter(e)).count();
let search = Searches {
latency: Latency::of(&ms("search", &|_| true)),
empty: count(&|e| e.number("hits") == Some(0.0)),
semantic: count(&|e| e.flag("semantic") == Some(true)),
with_links: count(&|e| e.number("links").is_some_and(|n| n > 0.0)),
with_memories: count(&|e| e.number("memories").is_some_and(|n| n > 0.0)),
};
let ok = |e: &Event| e.flag("ok") != Some(false);
let mut answers = Answers {
latency: Latency::of(&ms("answer", &ok)),
first_token: Latency::of(
&events
.iter()
.filter(|e| e.kind == "answer")
.filter_map(|e| e.number("first_token_ms"))
.map(|n| n as u64)
.collect::<Vec<_>>(),
),
..Answers::default()
};
for event in events.iter().filter(|e| e.kind == "answer") {
if ok(event) {
if let (Some(provider), Some(model)) = (event.text("provider"), event.text("model")) {
*answers.models.entry(format!("{provider}/{model}")).or_default() += 1;
}
} else {
answers.failed += 1;
}
*answers.skills.entry(event.text("skill").unwrap_or("ask").to_owned()).or_default() += 1;
}
let runs: Vec<&Event> = events.iter().filter(|e| e.kind == "index").collect();
let index = Indexing {
latency: Latency::of(&ms("index", &|_| true)),
files_indexed: runs.iter().filter_map(|e| e.number("files_indexed")).map(|n| n as u64).sum(),
last: runs.last().map(|e| e.at.clone()),
};
let failures: Vec<&Event> = events.iter().filter(|e| e.kind == "error").collect();
let mut errors = Errors { count: failures.len(), ..Errors::default() };
for failure in &failures {
*errors.commands.entry(failure.text("command").unwrap_or("?").to_owned()).or_default() += 1;
}
errors.recent = failures
.iter()
.rev()
.take(RECENT_ERRORS)
.map(|e| Failure {
at: e.at.clone(),
command: e.text("command").unwrap_or("?").to_owned(),
message: e.text("message").unwrap_or_default().to_owned(),
})
.collect();
Summary { days, events: events.len(), search, answers, index, errors }
}
pub async fn run(cwd: &Path, args: Args) -> Result<()> {
let workspace = super::workspace(cwd)?;
let days = args.days.max(1);
let summary = summarize(&brok_core::metrics::load(workspace.paths(), days), days);
if args.json {
println!("{}", serde_json::to_string_pretty(&summary)?);
return Ok(());
}
if !workspace.config().metrics.enabled {
ui::warn("metrics are off in this workspace (metrics.enabled in brok.yml)");
}
print(&summary);
Ok(())
}
fn print(summary: &Summary) {
ui::header(&format!("Last {} day(s)", summary.days));
if summary.events == 0 {
println!(" nothing recorded yet");
return;
}
let s = &summary.search;
println!(
" {:<9} {} · {} with no results · {} used linked workspaces · {} found memories",
style("search").bold(),
latency(&s.latency, "search", "searches"),
s.empty,
s.with_links,
s.with_memories
);
let a = &summary.answers;
let models = top(&a.models);
println!(
" {:<9} {} · {} failed · first words {}{}",
style("answers").bold(),
latency(&a.latency, "answer", "answers"),
a.failed,
a.first_token.p50_ms.map_or_else(|| "-".to_owned(), duration),
if models.is_empty() { String::new() } else { format!(" · {models}") }
);
let i = &summary.index;
println!(
" {:<9} {} · {} files indexed{}",
style("index").bold(),
latency(&i.latency, "run", "runs"),
i.files_indexed,
i.last.as_deref().map(|at| format!(" · last {}", short(at))).unwrap_or_default()
);
let e = &summary.errors;
println!(
" {:<9} {}{}",
style("errors").bold(),
e.count,
if e.count > 0 { format!(" · {}", top(&e.commands)) } else { String::new() }
);
for failure in &e.recent {
println!(" {} {}: {}", style(short(&failure.at)).dim(), failure.command, style(&failure.message).dim());
}
if e.count > 0 {
ui::hint("report one with `brok bug`");
}
}
fn latency(latency: &Latency, one: &str, many: &str) -> String {
let what = if latency.count == 1 { one } else { many };
match (latency.p50_ms, latency.p95_ms) {
(Some(p50), Some(p95)) => format!("{} {what} · p50 {} · p95 {}", latency.count, duration(p50), duration(p95)),
_ => format!("{} {what}", latency.count),
}
}
fn duration(ms: u64) -> String {
match ms {
0..1_000 => format!("{ms} ms"),
1_000..60_000 => format!("{:.1} s", ms as f64 / 1_000.0),
_ => format!("{:.1} min", ms as f64 / 60_000.0),
}
}
fn top(counts: &BTreeMap<String, usize>) -> String {
let mut entries: Vec<(&String, &usize)> = counts.iter().collect();
entries.sort_by(|a, b| b.1.cmp(a.1).then_with(|| a.0.cmp(b.0)));
entries.iter().take(3).map(|(k, v)| format!("{k} ({v})")).collect::<Vec<_>>().join(", ")
}
fn short(at: &str) -> String {
at.get(..16).unwrap_or(at).replace('T', " ")
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn event(kind: &str, ms: Option<u64>, data: serde_json::Value) -> Event {
Event {
at: "2026-11-03T10:00:00Z".into(),
kind: kind.into(),
ms,
data: data.as_object().cloned().unwrap_or_default(),
}
}
#[test]
fn summary() {
let events = vec![
event("search", Some(10), json!({ "hits": 8, "semantic": true, "links": 2, "memories": 1 })),
event("search", Some(30), json!({ "hits": 0, "semantic": false, "links": 0, "memories": 0 })),
event(
"answer",
Some(4_000),
json!({ "ok": true, "provider": "ollama", "model": "qwen", "first_token_ms": 900 }),
),
event("answer", Some(9_000), json!({ "ok": false, "skill": "code-review" })),
event("index", Some(200), json!({ "files_indexed": 12 })),
event("error", None, json!({ "command": "ask", "message": "no answer" })),
];
let summary = summarize(&events, 7);
assert_eq!(summary.events, 6);
assert_eq!(summary.search.latency, Latency { count: 2, p50_ms: Some(10), p95_ms: Some(30) });
assert_eq!((summary.search.empty, summary.search.semantic, summary.search.with_links), (1, 1, 1));
assert_eq!(summary.answers.latency.count, 1);
assert_eq!(summary.answers.failed, 1);
assert_eq!(summary.answers.first_token.p50_ms, Some(900));
assert_eq!(summary.answers.models.get("ollama/qwen"), Some(&1));
assert_eq!(summary.answers.skills.get("code-review"), Some(&1));
assert_eq!(summary.index.files_indexed, 12);
assert_eq!(summary.errors.recent[0].message, "no answer");
}
#[test]
fn durations() {
assert_eq!(duration(42), "42 ms");
assert_eq!(duration(1_800), "1.8 s");
assert_eq!(duration(150_000), "2.5 min");
}
}