use crate::config_policy::StatusMetrics as MetricsSnapshot;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct MetricLabels {
pub cache: String,
pub input: String,
pub llm: String,
pub step: String,
pub steps: String,
pub tokens_per_second: String,
pub tools: String,
pub ttft: String,
pub turn: String,
pub turns: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetricCell {
pub label: String,
pub value: String,
pub value_first: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetricGroup {
Input,
Cache,
Llm,
Turns,
Latency,
}
const GROUP_ORDER: [MetricGroup; 5] = [
MetricGroup::Turns,
MetricGroup::Llm,
MetricGroup::Latency,
MetricGroup::Cache,
MetricGroup::Input,
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetricGroupCells {
pub group: MetricGroup,
pub cells: Vec<MetricCell>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Separators {
pub cell: &'static str,
pub group: &'static str,
}
impl Separators {
pub const UNICODE: Self = Self {
cell: " · ",
group: " │ ",
};
pub const ASCII: Self = Self {
cell: " . ",
group: " | ",
};
#[must_use]
pub fn for_ascii(ascii_safe: bool) -> Self {
if ascii_safe {
Self::ASCII
} else {
Self::UNICODE
}
}
}
#[must_use]
pub fn format_duration(duration: Duration) -> String {
let ms = duration.as_millis();
if ms == 0 {
return "0s".to_string();
}
if ms < 1_000 {
return format!("{ms}ms");
}
let secs = duration.as_secs();
if secs < 60 {
let tenths = (ms + 50) / 100;
return format!("{}.{}s", tenths / 10, tenths % 10);
}
if secs < 3_600 {
return format!("{}m{:02}s", secs / 60, secs % 60);
}
format!("{}h{:02}m", secs / 3_600, (secs % 3_600) / 60)
}
#[must_use]
pub fn format_tokens(tokens: u64) -> String {
const UNITS: [(u64, &str); 3] = [(1_000_000_000, "B"), (1_000_000, "M"), (1_000, "K")];
for (scale, suffix) in UNITS {
if tokens >= scale {
let scaled = tokens as f64 / scale as f64;
return if scaled >= 100.0 {
format!("{scaled:.0}{suffix}")
} else {
format!("{scaled:.1}{suffix}")
};
}
}
tokens.to_string()
}
#[must_use]
pub fn format_rate(rate: f64) -> String {
if rate < 10.0 {
format!("{rate:.1}")
} else {
format!("{rate:.0}")
}
}
#[must_use]
pub fn build_groups(snapshot: MetricsSnapshot, labels: &MetricLabels) -> Vec<MetricGroupCells> {
let label = |value: &String| value.clone();
let mut groups = Vec::new();
for group in GROUP_ORDER {
let cells = match group {
MetricGroup::Turns => {
if snapshot.turns == 0 && snapshot.steps == 0 {
continue;
}
let mut cells = Vec::new();
if snapshot.turns > 0 {
cells.push(MetricCell {
label: label(if snapshot.turns == 1 {
&labels.turn
} else {
&labels.turns
}),
value: snapshot.turns.to_string(),
value_first: true,
});
}
if snapshot.steps > 0 {
cells.push(MetricCell {
label: label(if snapshot.steps == 1 {
&labels.step
} else {
&labels.steps
}),
value: snapshot.steps.to_string(),
value_first: true,
});
}
if cells.is_empty() {
continue;
}
cells
}
MetricGroup::Llm => {
let mut cells = Vec::new();
if !snapshot.llm_time.is_zero() {
cells.push(MetricCell {
label: label(&labels.llm),
value: format_duration(snapshot.llm_time),
value_first: false,
});
}
if !snapshot.tool_time.is_zero() {
cells.push(MetricCell {
label: label(&labels.tools),
value: format_duration(snapshot.tool_time),
value_first: false,
});
}
if cells.is_empty() {
continue;
}
cells
}
MetricGroup::Latency => {
let mut cells = Vec::new();
if let Some(ttft) = snapshot.ttft_avg {
cells.push(MetricCell {
label: label(&labels.ttft),
value: format_duration(ttft),
value_first: false,
});
}
if let Some(rate) = snapshot.tokens_per_second {
cells.push(MetricCell {
label: label(&labels.tokens_per_second),
value: format_rate(rate),
value_first: true,
});
}
if cells.is_empty() {
continue;
}
cells
}
MetricGroup::Cache => {
let Some(pct) = snapshot.cache_hit_percent else {
continue;
};
vec![MetricCell {
label: label(&labels.cache),
value: format!("{pct}%"),
value_first: false,
}]
}
MetricGroup::Input => {
if snapshot.input_tokens == 0 {
continue;
}
vec![MetricCell {
label: label(&labels.input),
value: format_tokens(snapshot.input_tokens),
value_first: false,
}]
}
};
groups.push(MetricGroupCells { group, cells });
}
groups
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderedStrip {
pub groups: Vec<MetricGroupCells>,
pub separators: Separators,
}
impl RenderedStrip {
#[must_use]
pub fn text(&self) -> String {
let mut out = String::new();
for (index, group) in self.groups.iter().enumerate() {
if index > 0 {
out.push_str(self.separators.group);
}
for (cell_index, cell) in group.cells.iter().enumerate() {
if cell_index > 0 {
out.push_str(self.separators.cell);
}
if cell.value_first {
out.push_str(&cell.value);
out.push(' ');
out.push_str(&cell.label);
} else {
out.push_str(&cell.label);
out.push(' ');
out.push_str(&cell.value);
}
}
}
out
}
}