codewhale_command_contract/
metrics.rs1use crate::config_policy::StatusMetrics as MetricsSnapshot;
3use std::time::Duration;
4
5#[derive(Debug, Clone)]
6pub 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#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct MetricCell {
22 pub label: String,
23 pub value: String,
24 pub value_first: bool,
26}
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum MetricGroup {
32 Input,
33 Cache,
34 Llm,
35 Turns,
36 Latency,
37}
38
39const GROUP_ORDER: [MetricGroup; 5] = [
41 MetricGroup::Turns,
42 MetricGroup::Llm,
43 MetricGroup::Latency,
44 MetricGroup::Cache,
45 MetricGroup::Input,
46];
47
48#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct MetricGroupCells {
51 pub group: MetricGroup,
52 pub cells: Vec<MetricCell>,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub struct Separators {
58 pub cell: &'static str,
59 pub group: &'static str,
60}
61
62impl Separators {
63 pub const UNICODE: Self = Self {
65 cell: " · ",
66 group: " │ ",
67 };
68 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#[must_use]
86pub 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#[must_use]
107pub 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#[must_use]
124pub 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#[must_use]
141pub 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#[derive(Debug, Clone, PartialEq, Eq)]
250pub struct RenderedStrip {
251 pub groups: Vec<MetricGroupCells>,
252 pub separators: Separators,
253}
254
255impl RenderedStrip {
256 #[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}