1use std::fmt::Write as _;
2
3use crate::analysis::validate::ValidationReport;
4use crate::analysis::{OverviewResult, ProjectResult, SessionResult, TrendResult};
5use crate::pricing::calculator::PricingCalculator;
6
7fn format_number(n: u64) -> String {
10 let s = n.to_string();
11 let mut result = String::with_capacity(s.len() + s.len() / 3);
12 for (i, ch) in s.chars().rev().enumerate() {
13 if i > 0 && i % 3 == 0 {
14 result.push(',');
15 }
16 result.push(ch);
17 }
18 result.chars().rev().collect()
19}
20
21fn format_cost(c: f64) -> String {
22 let abs = c.abs();
23 let total_cents = (abs * 100.0).round() as u64;
24 let whole = total_cents / 100;
25 let cents = total_cents % 100;
26 let sign = if c < 0.0 { "-" } else { "" };
27 format!("{}${}.{:02}", sign, format_number(whole), cents)
28}
29
30fn format_duration(minutes: f64) -> String {
31 if minutes < 1.0 {
32 format!("{:.0}s", minutes * 60.0)
33 } else if minutes < 60.0 {
34 format!("{:.0}m", minutes)
35 } else {
36 let h = (minutes / 60.0).floor();
37 let m = (minutes % 60.0).round();
38 format!("{:.0}h{:.0}m", h, m)
39 }
40}
41
42pub fn render_overview(result: &OverviewResult, calc: &PricingCalculator) -> String {
45 let mut out = String::new();
46 let _ = calc;
47
48 let range = result.quality.time_range
49 .map(|(s, e)| {
50 let ls = s.with_timezone(&chrono::Local);
51 let le = e.with_timezone(&chrono::Local);
52 format!("{} ~ {}", ls.format("%Y-%m-%d"), le.format("%Y-%m-%d"))
53 })
54 .unwrap_or_default();
55
56 writeln!(out, "Claude Code Token Report").unwrap();
57 writeln!(out, "{}", range).unwrap();
58 writeln!(out).unwrap();
59
60 writeln!(out, " {} conversations, {} rounds of back-and-forth",
61 format_number(result.total_sessions as u64),
62 format_number(result.total_turns as u64)).unwrap();
63 if result.total_agent_turns > 0 {
64 writeln!(out, " ({} agent turns, {:.0}% of total)",
65 format_number(result.total_agent_turns as u64),
66 result.total_agent_turns as f64 / result.total_turns.max(1) as f64 * 100.0).unwrap();
67 }
68 writeln!(out).unwrap();
69
70 writeln!(out, " Claude read {} tokens",
71 format_number(result.total_context_tokens)).unwrap();
72 writeln!(out, " Claude wrote {} tokens",
73 format_number(result.total_output_tokens)).unwrap();
74 writeln!(out).unwrap();
75
76 writeln!(out, " Cache saved you {} ({:.0}% of reads were free)",
77 format_cost(result.cache_savings.total_saved),
78 result.cache_savings.savings_pct).unwrap();
79 writeln!(out, " All that would cost {} at API rates",
80 format_cost(result.total_cost)).unwrap();
81
82 if let Some(ref sub) = result.subscription_value {
84 writeln!(out, " Subscription: {}/mo -> {:.1}x value multiplier",
85 format_cost(sub.monthly_price), sub.value_multiplier).unwrap();
86 }
87
88 writeln!(out).unwrap();
90 writeln!(out, " Model Wrote Rounds Cost").unwrap();
91 writeln!(out, " ---------------------------------------------------------").unwrap();
92
93 let mut models: Vec<(&String, &crate::analysis::AggregatedTokens)> = result.tokens_by_model.iter().collect();
94 models.sort_by(|a, b| {
95 let ca = result.cost_by_model.get(a.0).unwrap_or(&0.0);
96 let cb = result.cost_by_model.get(b.0).unwrap_or(&0.0);
97 cb.partial_cmp(ca).unwrap_or(std::cmp::Ordering::Equal)
98 });
99
100 for (model, tokens) in &models {
101 let cost = result.cost_by_model.get(*model).unwrap_or(&0.0);
102 let short = short_model(model);
103 writeln!(out, " {:<25} {:>10} {:>9} {:>9}",
104 short,
105 format_number(tokens.output_tokens),
106 format_number(tokens.turns as u64),
107 format_cost(*cost)).unwrap();
108 }
109
110 writeln!(out).unwrap();
112 let cat = &result.cost_by_category;
113 let total = result.total_cost.max(0.001);
114 writeln!(out, " Cost Breakdown").unwrap();
115 writeln!(out, " Output: {:>9} ({:.0}%)", format_cost(cat.output_cost), cat.output_cost / total * 100.0).unwrap();
116 writeln!(out, " Cache Write: {:>9} ({:.0}%)", format_cost(cat.cache_write_5m_cost + cat.cache_write_1h_cost),
117 (cat.cache_write_5m_cost + cat.cache_write_1h_cost) / total * 100.0).unwrap();
118 writeln!(out, " Input: {:>9} ({:.0}%)", format_cost(cat.input_cost), cat.input_cost / total * 100.0).unwrap();
119 writeln!(out, " Cache Read: {:>9} ({:.0}%)", format_cost(cat.cache_read_cost), cat.cache_read_cost / total * 100.0).unwrap();
120
121 if !result.tool_counts.is_empty() {
123 writeln!(out).unwrap();
124 writeln!(out, " Top Tools").unwrap();
125 for (name, count) in result.tool_counts.iter().take(10) {
126 let bar_len = (*count as f64 / result.tool_counts[0].1.max(1) as f64 * 20.0).round() as usize;
127 writeln!(out, " {:<18} {:>6} {}", name, format_number(*count as u64), "█".repeat(bar_len)).unwrap();
128 }
129 }
130
131 if !result.session_summaries.is_empty() {
133 writeln!(out).unwrap();
134 writeln!(out, " Top Projects Sessions Turns Cost").unwrap();
135 writeln!(out, " -------------------------------------------------------------------").unwrap();
136
137 let mut project_map: std::collections::HashMap<&str, (usize, usize, f64)> = std::collections::HashMap::new();
138 for s in &result.session_summaries {
139 let e = project_map.entry(&s.project_display_name).or_default();
140 e.0 += 1;
141 e.1 += s.turn_count;
142 e.2 += s.cost;
143 }
144 let mut projects: Vec<_> = project_map.into_iter().collect();
145 projects.sort_by(|a, b| b.1.2.partial_cmp(&a.1.2).unwrap_or(std::cmp::Ordering::Equal));
146
147 for (name, (sessions, turns, cost)) in projects.iter().take(5) {
148 writeln!(out, " {:<40} {:>5} {:>7} {:>9}",
149 name, sessions, turns, format_cost(*cost)).unwrap();
150 }
151 }
152
153 writeln!(out).unwrap();
155 writeln!(out, " Data: {} session files, {} agent files",
156 result.quality.total_session_files, result.quality.total_agent_files).unwrap();
157 if result.quality.orphan_agents > 0 {
158 writeln!(out, " ({} orphan agents without parent session)", result.quality.orphan_agents).unwrap();
159 }
160
161 writeln!(out).unwrap();
162
163 out
164}
165
166fn short_model(name: &str) -> String {
167 let s = name.strip_prefix("claude-").unwrap_or(name);
168 if s.len() > 9 {
169 let last_dash = s.rfind('-').unwrap_or(s.len());
170 let suffix = &s[last_dash + 1..];
171 if suffix.len() == 8 && suffix.chars().all(|c| c.is_ascii_digit()) {
172 return s[..last_dash].to_string();
173 }
174 }
175 s.to_string()
176}
177
178pub fn render_projects(result: &ProjectResult) -> String {
181 let mut out = String::new();
182 let mut total_cost = 0.0f64;
183
184 writeln!(out, "Projects by Cost").unwrap();
185 writeln!(out).unwrap();
186 writeln!(out, " # Project Sessions Turns Agent $/Sess Model Cost").unwrap();
187 writeln!(out, " ─────────────────────────────────────────────────────────────────────────────────────────").unwrap();
188
189 for (i, proj) in result.projects.iter().enumerate() {
190 let avg_cost = if proj.session_count > 0 { proj.cost / proj.session_count as f64 } else { 0.0 };
191 let model_short = short_model(&proj.primary_model);
192 writeln!(out, " {:>2}. {:<30} {:>5} {:>6} {:>5} {:>6} {:<12} {:>9}",
193 i + 1,
194 truncate_str(&proj.display_name, 30),
195 proj.session_count,
196 proj.total_turns,
197 proj.agent_turns,
198 format_cost(avg_cost),
199 truncate_str(&model_short, 12),
200 format_cost(proj.cost),
201 ).unwrap();
202 total_cost += proj.cost;
203 }
204
205 writeln!(out).unwrap();
206 writeln!(out, " Total: {} projects, {}", result.projects.len(), format_cost(total_cost)).unwrap();
207 out
208}
209
210fn truncate_str(s: &str, max: usize) -> String {
211 if s.len() <= max { s.to_string() }
212 else { format!("{}...", &s[..s.floor_char_boundary(max.saturating_sub(3))]) }
213}
214
215pub fn render_session(result: &SessionResult) -> String {
218 let mut out = String::new();
219
220 let main_turns = result.turn_details.iter().filter(|t| !t.is_agent).count();
221
222 writeln!(out, "Session {} {}", &result.session_id[..result.session_id.len().min(8)], result.project).unwrap();
223 writeln!(out).unwrap();
224 writeln!(out, " Turns: {:>6} (+ {} agent) Duration: {}",
225 main_turns, result.agent_summary.total_agent_turns, format_duration(result.duration_minutes)).unwrap();
226 writeln!(out, " Model: {:<20} MaxCtx: {}",
227 result.model, format_number(result.max_context)).unwrap();
228 writeln!(out, " CacheHit: {:>5.1}% Compacts: {}",
229 result.total_tokens.cache_read_tokens as f64 / result.total_tokens.context_tokens().max(1) as f64 * 100.0,
230 result.compaction_count).unwrap();
231 writeln!(out, " Cost: {}", format_cost(result.total_cost)).unwrap();
232
233 out
234}
235
236pub fn render_trend(result: &TrendResult) -> String {
239 let mut out = String::new();
240 let mut total_cost = 0.0f64;
241 let mut total_turns = 0usize;
242
243 let max_cost = result.entries.iter().map(|e| e.cost).fold(0.0f64, f64::max);
245
246 writeln!(out, "Usage by {}", result.group_label).unwrap();
247 writeln!(out).unwrap();
248
249 for entry in &result.entries {
250 let bar_len = if max_cost > 0.0 { (entry.cost / max_cost * 16.0).round() as usize } else { 0 };
252 let bar = "▇".repeat(bar_len);
253
254 let top_model = entry.models.iter()
256 .max_by_key(|(_, tokens)| *tokens)
257 .map(|(m, _)| short_model(m))
258 .unwrap_or_default();
259
260 let cpt = if entry.turn_count > 0 { entry.cost / entry.turn_count as f64 } else { 0.0 };
262
263 writeln!(out, " {:<10} {:>4} sess {:>6} turns {:>9} ${:.3}/t {:<12} {}",
264 entry.label, entry.session_count, entry.turn_count,
265 format_cost(entry.cost), cpt,
266 truncate_str(&top_model, 12),
267 bar,
268 ).unwrap();
269 total_cost += entry.cost;
270 total_turns += entry.turn_count;
271 }
272
273 writeln!(out).unwrap();
274 let avg_cpt = if total_turns > 0 { total_cost / total_turns as f64 } else { 0.0 };
275 writeln!(out, " Total: {} ({} turns, avg ${:.3}/turn)", format_cost(total_cost), format_number(total_turns as u64), avg_cpt).unwrap();
276 out
277}
278
279pub fn render_validation(report: &ValidationReport, failures_only: bool) -> String {
280 let mut out = String::new();
281
282 writeln!(out, "Token Validation Report").unwrap();
283 writeln!(out, "{}", "━".repeat(60)).unwrap();
284 writeln!(out).unwrap();
285
286 writeln!(out, "Structure Checks:").unwrap();
288 for check in &report.structure_checks {
289 if failures_only && check.passed { continue; }
290 let status = if check.passed { "OK" } else { "FAIL" };
291 if check.passed {
292 writeln!(out, " [{:>4}] {}: {}", status, check.name, check.actual).unwrap();
293 } else {
294 writeln!(out, " [{:>4}] {}: expected={}, actual={}", status, check.name, check.expected, check.actual).unwrap();
295 }
296 }
297 writeln!(out).unwrap();
298
299 let mut fail_sessions = Vec::new();
301 for sv in &report.session_results {
302 let all_checks: Vec<_> = sv.token_checks.iter().chain(sv.agent_checks.iter()).collect();
303 let has_failures = all_checks.iter().any(|c| !c.passed);
304
305 if failures_only && !has_failures { continue; }
306
307 if has_failures {
308 fail_sessions.push(sv);
309 }
310 }
311
312 if !failures_only {
313 writeln!(out, "Session Validation: {} sessions checked", report.session_results.len()).unwrap();
314 let sessions_ok = report.summary.sessions_passed;
315 let sessions_fail = report.summary.sessions_validated - sessions_ok;
316 writeln!(out, " {} PASS, {} FAIL", sessions_ok, sessions_fail).unwrap();
317 writeln!(out).unwrap();
318 }
319
320 if !fail_sessions.is_empty() {
322 writeln!(out, "Failed Sessions:").unwrap();
323 writeln!(out).unwrap();
324 }
325 for sv in &fail_sessions {
326 writeln!(out, " Session {} {}", &sv.session_id[..8.min(sv.session_id.len())], sv.project).unwrap();
327 for check in sv.token_checks.iter().chain(sv.agent_checks.iter()) {
328 if !check.passed {
329 writeln!(out, " [FAIL] {}: expected={}, actual={}", check.name, check.expected, check.actual).unwrap();
330 }
331 }
332 writeln!(out).unwrap();
333 }
334
335 writeln!(out, "{}", "━".repeat(60)).unwrap();
337 let result_text = if report.summary.failed == 0 { "PASS" } else { "FAIL" };
338 writeln!(out, "Result: {} ({}/{} checks passed, {} sessions validated)",
339 result_text,
340 report.summary.passed,
341 report.summary.total_checks,
342 report.summary.sessions_validated,
343 ).unwrap();
344
345 out
346}
347
348#[cfg(test)]
351mod tests {
352 use super::*;
353
354 #[test]
355 fn test_format_number() {
356 assert_eq!(format_number(0), "0");
357 assert_eq!(format_number(999), "999");
358 assert_eq!(format_number(1_000), "1,000");
359 assert_eq!(format_number(1_234_567), "1,234,567");
360 }
361
362 #[test]
363 fn test_format_cost() {
364 assert_eq!(format_cost(0.0), "$0.00");
365 assert_eq!(format_cost(1.5), "$1.50");
366 assert_eq!(format_cost(1234.56), "$1,234.56");
367 }
368}