pub fn duration(seconds: u64) -> String {
match seconds {
0..60 => format!("{seconds}s"),
60..3600 => format!("{}m {:02}s", seconds / 60, seconds % 60),
3600..86_400 => format!("{}h {:02}m", seconds / 3600, seconds % 3600 / 60),
_ => format!("{}d {}h", seconds / 86_400, seconds % 86_400 / 3600),
}
}
pub fn ago(at: u64, now: u64) -> String {
match now.saturating_sub(at) {
0..10 => "just now".to_owned(),
seconds => format!("{} ago", duration(seconds)),
}
}
pub fn cpu_time(milliseconds: u64) -> String {
if milliseconds < 60_000 {
let tenths = (milliseconds + 50) / 100;
format!("{}.{}s", tenths / 10, tenths % 10)
} else {
duration(milliseconds / 1000)
}
}
pub fn cores_used(millicores: u64) -> String {
let hundredths = millicores.div_ceil(10);
let (whole, part) = (hundredths / 100, hundredths % 100);
if part == 0 {
whole.to_string()
} else if part.is_multiple_of(10) {
format!("{whole}.{}", part / 10)
} else {
format!("{whole}.{part:02}")
}
}
pub fn in_cores(millicores: u64) -> String {
match cores_used(millicores).as_str() {
"1" => "1 core".to_owned(),
number => format!("{number} cores"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_duration_reads_like_a_stopwatch_in_every_client() {
assert_eq!(duration(9), "9s");
assert_eq!(duration(72), "1m 12s");
assert_eq!(duration(3909), "1h 05m");
assert_eq!(duration(3 * 86_400 + 4 * 3600), "3d 4h");
}
#[test]
fn an_age_is_the_duration_since() {
assert_eq!(ago(100, 105), "just now");
assert_eq!(ago(0, 3909), "1h 05m ago");
assert_eq!(
ago(200, 100),
"just now",
"a time ahead of the clock is now"
);
}
#[test]
fn cpu_time_keeps_work_under_a_second() {
assert_eq!(cpu_time(4600), "4.6s");
assert_eq!(cpu_time(600), "0.6s");
assert_eq!(cpu_time(125_000), "2m 05s");
}
#[test]
fn cores_in_use_never_read_as_none() {
assert_eq!(cores_used(1), "0.01");
assert_eq!(cores_used(250), "0.25");
assert_eq!(cores_used(1500), "1.5");
assert_eq!(cores_used(12_000), "12");
assert_eq!(cores_used(0), "0");
assert_eq!(in_cores(1000), "1 core");
assert_eq!(in_cores(1250), "1.25 cores");
}
}