use forge_ci::proto::RunnerSample; pub const VIEW_W: f64 = 360.0; pub const VIEW_H: f64 = 110.0; pub const X0: f64 = 40.0; pub const X1: f64 = 356.0; pub const Y0: f64 = 6.0; pub const Y1: f64 = 94.0; pub const BUCKET_MS: i64 = 30_000; pub const BUCKETS: usize = 120; const GAP_MS: i64 = 30_000; const TIP_W: f64 = 128.0; const TIP_H: f64 = 40.0; const TIP_GAP: f64 = 8.0; #[derive(Clone, Copy, Debug, PartialEq)] pub struct Bucket { pub at_ms: i64, pub first_ms: i64, pub last_ms: i64, pub cpu: f64, pub mem_used: f64, pub mem_total: f64, pub rx: f64, pub tx: f64, pub disk_used: f64, pub disk_total: f64, pub running: u32, } #[derive(Clone, Debug, PartialEq)] pub struct Path { pub d: String, pub class: &'static str, } #[derive(Clone, Debug, PartialEq)] pub struct Hit { pub x: f64, pub w: f64, pub cx: f64, pub bucket: Bucket, pub tip_x: f64, pub tip_y: f64, pub tip_w: f64, pub tip_h: f64, } pub type Series = (fn(&Bucket) -> f64, &'static str); #[derive(Clone, Debug, PartialEq)] pub struct Svg { pub grids: Vec, pub ticks: Vec<(f64, f64)>, pub bands: Vec<(f64, f64)>, pub paths: Vec, pub hits: Vec, } pub fn chart_points(samples: &[RunnerSample], now_ms: i64) -> Vec> { let start = now_ms - BUCKET_MS * BUCKETS as i64; let mut sums: Vec> = vec![None; BUCKETS]; for s in samples { if s.at_ms < start || s.at_ms > now_ms { continue; } let index = (((s.at_ms - start) / BUCKET_MS) as usize).min(BUCKETS - 1); let at_ms = start + index as i64 * BUCKET_MS; let (bucket, count) = sums[index].get_or_insert(( Bucket { at_ms, first_ms: s.at_ms, last_ms: s.at_ms, cpu: 0.0, mem_used: 0.0, mem_total: 0.0, rx: 0.0, tx: 0.0, disk_used: 0.0, disk_total: 0.0, running: 0, }, 0.0, )); bucket.first_ms = bucket.first_ms.min(s.at_ms); bucket.last_ms = bucket.last_ms.max(s.at_ms); bucket.cpu += f64::from(s.cpu_percent); bucket.mem_used += s.mem_used_bytes as f64; bucket.mem_total = bucket.mem_total.max(s.mem_total_bytes as f64); bucket.rx += s.net_rx_bytes_per_s as f64; bucket.tx += s.net_tx_bytes_per_s as f64; bucket.disk_used += s.disk_used_bytes as f64; bucket.disk_total = bucket.disk_total.max(s.disk_total_bytes as f64); bucket.running = bucket.running.max(s.running_jobs); *count += 1.0; } sums.into_iter() .map(|slot| { slot.map(|(mut bucket, count)| { bucket.cpu /= count; bucket.mem_used /= count; bucket.rx /= count; bucket.tx /= count; bucket.disk_used /= count; bucket }) }) .collect() } pub fn nice_max(value: u64) -> u64 { if value <= 1 { return 1; } let value = u128::from(value); let mut unit = 1u128; while unit * 10 <= value { unit *= 10; } let nice = [unit, unit * 2, unit * 5 / 2, unit * 5, unit * 10] .into_iter() .find(|candidate| *candidate >= value) .unwrap_or(unit * 10); u64::try_from(nice).unwrap_or(u64::MAX) } pub fn net_max(points: &[Option]) -> f64 { let peak = points .iter() .flatten() .map(|b| b.rx.max(b.tx)) .fold(0.0_f64, f64::max); nice_max((peak.ceil() as u64).max(1024)) as f64 } pub fn svg(points: &[Option], max: f64, series: &[Series], band: bool) -> Svg { let max = if max > 0.0 { max } else { 1.0 }; let bw = bucket_width(); let bands = if band { points .iter() .enumerate() .filter(|(_, p)| p.is_some_and(|b| b.running > 0)) .map(|(i, _)| (X0 + i as f64 * bw, bw)) .collect() } else { Vec::new() }; let paths = series .iter() .map(|(value, class)| Path { d: line_d(points, *value, max), class, }) .collect(); let hits = points .iter() .enumerate() .filter_map(|(i, p)| { let bucket = (*p)?; let cx = X0 + (i as f64 + 0.5) * bw; let tip_x = if cx > (X0 + X1) / 2.0 { cx - TIP_GAP - TIP_W } else { cx + TIP_GAP }; Some(Hit { x: X0 + i as f64 * bw, w: bw, cx, bucket, tip_x, tip_y: Y0 + 2.0, tip_w: TIP_W, tip_h: TIP_H, }) }) .collect(); Svg { grids: vec![y_of(max / 2.0, max), y_of(max, max)], ticks: vec![ (y_of(0.0, max), 0.0), (y_of(max / 2.0, max), max / 2.0), (y_of(max, max), max), ], bands, paths, hits, } } fn bucket_width() -> f64 { (X1 - X0) / BUCKETS as f64 } fn y_of(value: f64, max: f64) -> f64 { Y1 - (value / max).clamp(0.0, 1.0) * (Y1 - Y0) } fn line_d(points: &[Option], value: fn(&Bucket) -> f64, max: f64) -> String { let bw = bucket_width(); let mut d = String::new(); let mut prev: Option = None; for (i, point) in points.iter().enumerate() { let Some(b) = point else { prev = None; continue; }; let joined = prev.is_some_and(|q| b.first_ms - q.last_ms <= GAP_MS); let x = X0 + (i as f64 + 0.5) * bw; let y = y_of(value(b), max); d.push_str(if joined { " L" } else { " M" }); d.push_str(&format!("{x:.1} {y:.1}")); prev = Some(*b); } d.trim_start().to_owned() } #[cfg(test)] mod tests { use super::*; fn sample(at_ms: i64, cpu: f32) -> RunnerSample { RunnerSample { at_ms, cpu_percent: cpu, mem_used_bytes: 0, mem_total_bytes: 0, net_rx_bytes_per_s: 0, net_tx_bytes_per_s: 0, disk_used_bytes: 0, disk_total_bytes: 0, running_jobs: 0, } } #[test] fn buckets_average_and_a_gap_breaks_the_line() { let now = 7_200_000; let start = now - BUCKET_MS * BUCKETS as i64; let samples = [ sample(start, 10.0), sample(start + 5_000, 30.0), sample(start + 65_000, 50.0), ]; let points = chart_points(&samples, now); assert_eq!(points[0].map(|b| b.cpu), Some(20.0)); assert_eq!(points[1], None); assert_eq!(points[2].map(|b| b.cpu), Some(50.0)); let d = line_d(&points, |b| b.cpu, 100.0); assert_eq!(d.matches('M').count(), 2, "{d}"); assert_eq!(d.matches('L').count(), 0, "{d}"); } }