| 1 | //! Pure session metrics rendering; host observation and painting remain outside. |
| 2 | use crate::config_policy::StatusMetrics as MetricsSnapshot; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | #[derive(Debug, Clone)] |
| 6 | pub struct MetricLabels { |
| 7 | pub cache: String, |
| 8 | pub input: String, |
| 9 | pub llm: String, |
| 10 | pub step: String, |
| 11 | pub steps: String, |
| 12 | pub tokens_per_second: String, |
| 13 | pub tools: String, |
| 14 | pub ttft: String, |
| 15 | pub turn: String, |
| 16 | pub turns: String, |
| 17 | } |
| 18 | |
| 19 | /// One rendered cell: a value with its localized short label. |
| 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 21 | pub struct MetricCell { |
| 22 | pub label: String, |
| 23 | pub value: String, |
| 24 | /// `label` first (`4 turns`) or value first (`LLM 11m46s`). |
| 25 | pub value_first: bool, |
| 26 | } |
| 27 | /// Group priority, highest kept first. When the row is too narrow, groups |
| 28 | /// are dropped from the end of this list; inside a group the second cell |
| 29 | /// (steps, tools, tok/s) is dropped before the group itself. |
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 31 | pub enum MetricGroup { |
| 32 | Input, |
| 33 | Cache, |
| 34 | Llm, |
| 35 | Turns, |
| 36 | Latency, |
| 37 | } |
| 38 | |
| 39 | /// The DSH-style layout order, left to right. |
| 40 | const GROUP_ORDER: [MetricGroup; 5] = [ |
| 41 | MetricGroup::Turns, |
| 42 | MetricGroup::Llm, |
| 43 | MetricGroup::Latency, |
| 44 | MetricGroup::Cache, |
| 45 | MetricGroup::Input, |
| 46 | ]; |
| 47 | |
| 48 | /// A group of one or two cells separated by ` · `. |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub struct MetricGroupCells { |
| 51 | pub group: MetricGroup, |
| 52 | pub cells: Vec<MetricCell>, |
| 53 | } |
| 54 | |
| 55 | /// Separators used between cells and between groups. |
| 56 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 57 | pub struct Separators { |
| 58 | pub cell: &'static str, |
| 59 | pub group: &'static str, |
| 60 | } |
| 61 | |
| 62 | impl Separators { |
| 63 | /// Unicode: ` · ` inside a group, ` │ ` between groups. |
| 64 | pub const UNICODE: Self = Self { |
| 65 | cell: " · ", |
| 66 | group: " │ ", |
| 67 | }; |
| 68 | /// ASCII-safe: ` . ` and ` | `. |
| 69 | pub const ASCII: Self = Self { |
| 70 | cell: " . ", |
| 71 | group: " | ", |
| 72 | }; |
| 73 | |
| 74 | #[must_use] |
| 75 | pub fn for_ascii(ascii_safe: bool) -> Self { |
| 76 | if ascii_safe { |
| 77 | Self::ASCII |
| 78 | } else { |
| 79 | Self::UNICODE |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// Format a duration the way the strip does: `11m46s`, `1h02m`, `1.5s`, `320ms`. |
| 85 | #[must_use] |
| 86 | pub fn format_duration(duration: Duration) -> String { |
| 87 | let ms = duration.as_millis(); |
| 88 | if ms == 0 { |
| 89 | return "0s".to_string(); |
| 90 | } |
| 91 | if ms < 1_000 { |
| 92 | return format!("{ms}ms"); |
| 93 | } |
| 94 | let secs = duration.as_secs(); |
| 95 | if secs < 60 { |
| 96 | let tenths = (ms + 50) / 100; |
| 97 | return format!("{}.{}s", tenths / 10, tenths % 10); |
| 98 | } |
| 99 | if secs < 3_600 { |
| 100 | return format!("{}m{:02}s", secs / 60, secs % 60); |
| 101 | } |
| 102 | format!("{}h{:02}m", secs / 3_600, (secs % 3_600) / 60) |
| 103 | } |
| 104 | |
| 105 | /// Format a token count: `842`, `12.3K`, `9.3M`, `1.2B`. |
| 106 | #[must_use] |
| 107 | pub fn format_tokens(tokens: u64) -> String { |
| 108 | const UNITS: [(u64, &str); 3] = [(1_000_000_000, "B"), (1_000_000, "M"), (1_000, "K")]; |
| 109 | for (scale, suffix) in UNITS { |
| 110 | if tokens >= scale { |
| 111 | let scaled = tokens as f64 / scale as f64; |
| 112 | return if scaled >= 100.0 { |
| 113 | format!("{scaled:.0}{suffix}") |
| 114 | } else { |
| 115 | format!("{scaled:.1}{suffix}") |
| 116 | }; |
| 117 | } |
| 118 | } |
| 119 | tokens.to_string() |
| 120 | } |
| 121 | |
| 122 | /// Format an output rate: `120` or `7.5` (the label carries `tok/s`). |
| 123 | #[must_use] |
| 124 | pub fn format_rate(rate: f64) -> String { |
| 125 | if rate < 10.0 { |
| 126 | format!("{rate:.1}") |
| 127 | } else { |
| 128 | format!("{rate:.0}") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Build the cells for every group that has something truthful to show. |
| 133 | /// |
| 134 | /// A cell whose evidence has not arrived is omitted — never a placeholder: |
| 135 | /// `TTFT avg` / `tok/s` appear only once a model call reported them, `Cache |
| 136 | /// hit` only when a provider reported cache classes, `Input` only after the |
| 137 | /// first usage receipt. Turn cells are present once the session has started |
| 138 | /// (zero turns is a real count). Step cells wait for the first completed |
| 139 | /// model or tool call so `0 steps` cannot look like a stalled scoreboard. |
| 140 | #[must_use] |
| 141 | pub fn build_groups(snapshot: MetricsSnapshot, labels: &MetricLabels) -> Vec<MetricGroupCells> { |
| 142 | let label = |value: &String| value.clone(); |
| 143 | let mut groups = Vec::new(); |
| 144 | for group in GROUP_ORDER { |
| 145 | let cells = match group { |
| 146 | MetricGroup::Turns => { |
| 147 | if snapshot.turns == 0 && snapshot.steps == 0 { |
| 148 | continue; |
| 149 | } |
| 150 | let mut cells = Vec::new(); |
| 151 | if snapshot.turns > 0 { |
| 152 | cells.push(MetricCell { |
| 153 | label: label(if snapshot.turns == 1 { |
| 154 | &labels.turn |
| 155 | } else { |
| 156 | &labels.turns |
| 157 | }), |
| 158 | value: snapshot.turns.to_string(), |
| 159 | value_first: true, |
| 160 | }); |
| 161 | } |
| 162 | if snapshot.steps > 0 { |
| 163 | cells.push(MetricCell { |
| 164 | label: label(if snapshot.steps == 1 { |
| 165 | &labels.step |
| 166 | } else { |
| 167 | &labels.steps |
| 168 | }), |
| 169 | value: snapshot.steps.to_string(), |
| 170 | value_first: true, |
| 171 | }); |
| 172 | } |
| 173 | if cells.is_empty() { |
| 174 | continue; |
| 175 | } |
| 176 | cells |
| 177 | } |
| 178 | MetricGroup::Llm => { |
| 179 | let mut cells = Vec::new(); |
| 180 | if !snapshot.llm_time.is_zero() { |
| 181 | cells.push(MetricCell { |
| 182 | label: label(&labels.llm), |
| 183 | value: format_duration(snapshot.llm_time), |
| 184 | value_first: false, |
| 185 | }); |
| 186 | } |
| 187 | if !snapshot.tool_time.is_zero() { |
| 188 | cells.push(MetricCell { |
| 189 | label: label(&labels.tools), |
| 190 | value: format_duration(snapshot.tool_time), |
| 191 | value_first: false, |
| 192 | }); |
| 193 | } |
| 194 | if cells.is_empty() { |
| 195 | continue; |
| 196 | } |
| 197 | cells |
| 198 | } |
| 199 | MetricGroup::Latency => { |
| 200 | let mut cells = Vec::new(); |
| 201 | if let Some(ttft) = snapshot.ttft_avg { |
| 202 | cells.push(MetricCell { |
| 203 | label: label(&labels.ttft), |
| 204 | value: format_duration(ttft), |
| 205 | value_first: false, |
| 206 | }); |
| 207 | } |
| 208 | if let Some(rate) = snapshot.tokens_per_second { |
| 209 | cells.push(MetricCell { |
| 210 | label: label(&labels.tokens_per_second), |
| 211 | value: format_rate(rate), |
| 212 | value_first: true, |
| 213 | }); |
| 214 | } |
| 215 | if cells.is_empty() { |
| 216 | continue; |
| 217 | } |
| 218 | cells |
| 219 | } |
| 220 | MetricGroup::Cache => { |
| 221 | let Some(pct) = snapshot.cache_hit_percent else { |
| 222 | continue; |
| 223 | }; |
| 224 | vec![MetricCell { |
| 225 | label: label(&labels.cache), |
| 226 | value: format!("{pct}%"), |
| 227 | value_first: false, |
| 228 | }] |
| 229 | } |
| 230 | MetricGroup::Input => { |
| 231 | if snapshot.input_tokens == 0 { |
| 232 | continue; |
| 233 | } |
| 234 | vec![MetricCell { |
| 235 | label: label(&labels.input), |
| 236 | value: format_tokens(snapshot.input_tokens), |
| 237 | value_first: false, |
| 238 | }] |
| 239 | } |
| 240 | }; |
| 241 | groups.push(MetricGroupCells { group, cells }); |
| 242 | } |
| 243 | groups |
| 244 | } |
| 245 | |
| 246 | /// A rendered strip: the plain text (for tests, `/status`, and width math) |
| 247 | /// plus the cells that survived the budget, so the painter can style labels |
| 248 | /// and values differently. |
| 249 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 250 | pub struct RenderedStrip { |
| 251 | pub groups: Vec<MetricGroupCells>, |
| 252 | pub separators: Separators, |
| 253 | } |
| 254 | |
| 255 | impl RenderedStrip { |
| 256 | /// Plain-text form: `4 turns · 108 steps │ LLM 11m46s · tools 1m52s │ …`. |
| 257 | #[must_use] |
| 258 | pub fn text(&self) -> String { |
| 259 | let mut out = String::new(); |
| 260 | for (index, group) in self.groups.iter().enumerate() { |
| 261 | if index > 0 { |
| 262 | out.push_str(self.separators.group); |
| 263 | } |
| 264 | for (cell_index, cell) in group.cells.iter().enumerate() { |
| 265 | if cell_index > 0 { |
| 266 | out.push_str(self.separators.cell); |
| 267 | } |
| 268 | if cell.value_first { |
| 269 | out.push_str(&cell.value); |
| 270 | out.push(' '); |
| 271 | out.push_str(&cell.label); |
| 272 | } else { |
| 273 | out.push_str(&cell.label); |
| 274 | out.push(' '); |
| 275 | out.push_str(&cell.value); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | out |
| 280 | } |
| 281 | } |
| 282 |