Files
sccache-dash/crates/server/src/pretty.rs
T
swaits 3cbc02e631 Server half of sccache-dash
Polls sccache's counters every 10 s, keeps a JSON-lines history, and serves
the page and its API on 127.0.0.1: one difference-with-reset mechanism behind
every range, per-language time saved, problems, a copyable summary, and
extra tables for multi-level and distributed caches.

The page is embedded from crates/ui/dist when it exists, with a placeholder
until then. POST /api/zero refuses foreign Host and Origin headers.
Tests run against a stand-in sccache script; sample fixtures for the
multi-level and distributed shapes are made up.
2026-11-28 11:23:39 -07:00

136 lines
4.3 KiB
Rust

//! How numbers, times and sizes are written for people.
/// `2815` as "2,815".
pub fn thousands(n: u64) -> String {
let digits = n.to_string();
let mut out = String::with_capacity(digits.len() + digits.len() / 3);
for (i, c) in digits.chars().enumerate() {
if i > 0 && (digits.len() - i).is_multiple_of(3) {
out.push(',');
}
out.push(c);
}
out
}
/// A share as a percentage with one decimal, dropping a needless ".0": `0.076` as "7.6", `1.0` as "100".
pub fn pct(share: f64) -> String {
let s = format!("{:.1}", share * 100.0);
s.strip_suffix(".0").unwrap_or(&s).to_owned()
}
/// Seconds rounded down to minutes, the way the summary says them: "3h 05m", "48m", "<1m".
pub fn hours_and_minutes(secs: f64) -> String {
let mins = (secs / 60.0).floor() as u64;
match mins {
0 => "<1m".to_owned(),
1..=59 => format!("{mins}m"),
_ => format!("{}h {:02}m", mins / 60, mins % 60),
}
}
/// A short length of time: "210 ms", "0.9 s", "7.8 s", "20 min".
pub fn short(secs: f64) -> String {
if secs < 1.0 {
format!("{:.0} ms", secs * 1000.0)
} else if secs < 600.0 {
let s = format!("{secs:.1}");
format!("{} s", s.strip_suffix(".0").unwrap_or(&s))
} else {
format!("{:.0} min", secs / 60.0)
}
}
/// Two sizes as one phrase: sccache's 10G limit reads "10.0 of 10.0 GB", and sizes in different
/// units read "900.0 MB of 10.0 GB".
pub fn bytes_of(size: u64, max: u64) -> String {
let (a, au) = amount(size);
let (b, bu) = amount(max);
if au == bu {
format!("{a} of {b} {au}")
} else {
format!("{a} {au} of {b} {bu}")
}
}
fn amount(b: u64) -> (String, &'static str) {
const K: f64 = 1024.0;
let b = b as f64;
if b >= K * K * K * K {
(format!("{:.1}", b / (K * K * K * K)), "TB")
} else if b >= K * K * K {
(format!("{:.1}", b / (K * K * K)), "GB")
} else if b >= K * K {
(format!("{:.1}", b / (K * K)), "MB")
} else {
(format!("{b:.0}"), "KB")
}
}
/// Unix seconds as a file-name-friendly UTC time: `0` as "19700101-000000".
pub fn stamp(secs: i64) -> String {
let (days, rem) = (secs.div_euclid(86_400), secs.rem_euclid(86_400));
// Days since 1970-01-01 to a date, by Howard Hinnant's algorithm.
let z = days + 719_468;
let (era, doe) = (z.div_euclid(146_097), z.rem_euclid(146_097));
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let m = if mp < 10 { mp + 3 } else { mp - 9 };
let y = yoe + era * 400 + i64::from(m <= 2);
format!(
"{y:04}{m:02}{d:02}-{:02}{:02}{:02}",
rem / 3600,
rem % 3600 / 60,
rem % 60
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numbers_as_written() {
assert_eq!(thousands(0), "0");
assert_eq!(thousands(2815), "2,815");
assert_eq!(thousands(1_234_567), "1,234,567");
assert_eq!(pct(0.076), "7.6");
assert_eq!(pct(0.5018), "50.2");
assert_eq!(pct(1.0), "100");
}
#[test]
fn times_as_written() {
assert_eq!(hours_and_minutes(11100.0), "3h 05m");
assert_eq!(hours_and_minutes(48.4 * 60.0), "48m");
assert_eq!(hours_and_minutes(20.0), "<1m");
assert_eq!(short(0.21), "210 ms");
assert_eq!(short(0.9), "900 ms");
assert_eq!(short(10.0), "10 s");
assert_eq!(short(60.0), "60 s");
assert_eq!(short(1200.0), "20 min");
}
#[test]
fn sizes_and_dates_as_written() {
assert_eq!(
bytes_of(10 * 1024 * 1024 * 1024, 10 * 1024 * 1024 * 1024),
"10.0 of 10.0 GB"
);
assert_eq!(bytes_of(1024 * 1024, 1024 * 1024), "1.0 of 1.0 MB");
assert_eq!(bytes_of(10_726_353_131, 10_737_418_240), "10.0 of 10.0 GB");
assert_eq!(
bytes_of(9 * 1024 * 1024 * 1024, 10 * 1024 * 1024 * 1024),
"9.0 of 10.0 GB"
);
assert_eq!(
bytes_of(900 * 1024 * 1024, 10 * 1024 * 1024 * 1024),
"900.0 MB of 10.0 GB"
);
assert_eq!(stamp(0), "19700101-000000");
assert_eq!(stamp(1_764_000_000), "20251124-160000"); // what `date -u -d @1764000000` says
}
}