Dashboard page for sccache-dash (crates/ui)

An eframe/egui page compiled to WebAssembly with trunk. It draws the one
View the server sends: controls on top, three big figures, six cards, and
the collapsible sections Problems, Where compiles went, Over time, Speed,
Extras and Raw counters. The range, refresh interval and theme are
remembered between visits with eframe storage.

mise.toml names trunk 0.22.2 as the build tool.
This commit is contained in:
swaits committed 2026-11-28 11:30:54 -07:00
1 parent a8e7bd66cf
commit b80749ebb5
16 files changed
+2242

No files matched your search

+284
View File
@@ -0,0 +1,284 @@
//! How numbers, durations and times are written on the page.
use sccache_dash_api::Range;
/// The code the server uses for a range, as in `/api/view?range=24h`.
pub fn range_code(range: Range) -> &'static str {
match range {
Range::Hour => "1h",
Range::Day => "24h",
Range::Week => "7d",
Range::Month => "30d",
Range::SinceReset => "reset",
}
}
/// The range a remembered code names.
pub fn range_from_code(code: &str) -> Option<Range> {
[
Range::Hour,
Range::Day,
Range::Week,
Range::Month,
Range::SinceReset,
]
.into_iter()
.find(|range| range_code(*range) == code)
}
/// The heading over the figures: "LAST 24 HOURS".
pub fn range_title(range: Range) -> &'static str {
match range {
Range::Hour => "LAST HOUR",
Range::Day => "LAST 24 HOURS",
Range::Week => "LAST 7 DAYS",
Range::Month => "LAST 30 DAYS",
Range::SinceReset => "SINCE RESET",
}
}
/// How a comparison with the period before is worded: "previous 24h".
pub fn previous_label(range: Range) -> &'static str {
match range {
Range::Hour => "previous hour",
Range::Day => "previous 24h",
Range::Week => "previous 7d",
Range::Month => "previous 30d",
Range::SinceReset => "the period before",
}
}
/// How the refresh control names an interval: "Every 10 s", or "Off".
pub fn refresh_label(secs: u64) -> String {
if secs == 0 {
"Off".into()
} else {
format!("Every {secs} s")
}
}
/// A count with thousands separators: 2815 becomes "2,815".
pub fn count(n: u64) -> String {
let digits = n.to_string();
let first = (digits.len() - 1) % 3 + 1;
let mut out = String::with_capacity(digits.len() + digits.len() / 3);
out.push_str(&digits[..first]);
for (index, digit) in digits[first..].chars().enumerate() {
if index > 0 && index % 3 == 0 {
out.push(',');
}
out.push(digit);
}
out
}
/// A ratio as whole percent: 0.456 becomes "46%"; "—" when there is no ratio.
pub fn percent(ratio: Option<f64>) -> String {
ratio.map_or_else(|| "—".into(), |ratio| format!("{:.0}%", ratio * 100.0))
}
/// `part` of `whole` as whole percent; "—" when `whole` is zero.
pub fn share(part: u64, whole: u64) -> String {
percent((whole > 0).then(|| part as f64 / whole as f64))
}
/// Seconds the short way: "0.7 ms", "8.1 s", "3m 05s", "3h 05m", "2d 3h".
pub fn duration(seconds: f64) -> String {
if !seconds.is_finite() {
return "—".into();
}
let seconds = seconds.max(0.0);
if seconds < 1.0 {
return format!("{:.1} ms", seconds * 1000.0);
}
let tenths = (seconds * 10.0).round() as u64;
let (whole, tenth) = (tenths / 10, tenths % 10);
if whole < 60 {
return format!("{whole}.{tenth} s");
}
let (minutes, second) = (whole / 60, whole % 60);
if minutes < 60 {
return format!("{minutes}m {second:02}s");
}
let (hours, minute) = (minutes / 60, minutes % 60);
if hours < 24 {
return format!("{hours}h {minute:02}m");
}
let (days, hour) = (hours / 24, hours % 24);
format!("{days}d {hour}h")
}
/// A multiple in round numbers: 11432.7 becomes "11,000", 7.42 becomes "7.4".
pub fn round_number(n: f64) -> String {
if !n.is_finite() || n <= 0.0 {
return "—".into();
}
if n < 10.0 {
return format!("{n:.1}");
}
let round_to = if n >= 1000.0 {
100.0
} else if n >= 100.0 {
10.0
} else {
1.0
};
count((n / round_to).round() as u64 * round_to as u64)
}
/// A size in bytes the way file managers show it: "10.0 GB".
pub fn bytes(n: u64) -> String {
const K: f64 = 1024.0;
let (value, unit) = match n as f64 {
value if value < K => return format!("{n} B"),
value if value < K * K => (value / K, "KB"),
value if value < K * K * K => (value / (K * K), "MB"),
value if value < K * K * K * K => (value / (K * K * K), "GB"),
value => (value / (K * K * K * K), "TB"),
};
format!("{value:.1} {unit}")
}
/// Seconds to add to a Unix time to get the clock time of this computer.
/// The browser knows its time zone; a native window would need to find it another way.
pub fn local_offset() -> i64 {
#[cfg(target_arch = "wasm32")]
{
// `get_timezone_offset` is minutes to add to local time to get UTC.
-(js_sys::Date::new_0().get_timezone_offset() as i64) * 60
}
#[cfg(not(target_arch = "wasm32"))]
{
0
}
}
/// A moment as month, day and clock time.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Moment {
pub month: u32,
pub day: u32,
pub hour: u32,
pub minute: u32,
}
/// The moment `unix` seconds after 1 January 1970.
pub fn moment(unix: i64) -> Moment {
let days = unix.div_euclid(86_400);
let secs_of_day = unix.rem_euclid(86_400);
// Howard Hinnant's civil-from-days.
let z = days + 719_468;
let day_of_era = z.rem_euclid(146_097);
let year_of_era =
(day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let mp = (5 * day_of_year + 2) / 153;
let day = day_of_year - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
Moment {
month: month as u32,
day: day as u32,
hour: (secs_of_day / 3600) as u32,
minute: (secs_of_day % 3600 / 60) as u32,
}
}
impl Moment {
/// "14:05"
pub fn clock(self) -> String {
format!("{:02}:{:02}", self.hour, self.minute)
}
/// "Nov 12"
pub fn date(self) -> String {
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let month = MONTHS[(self.month.clamp(1, 12) - 1) as usize];
format!("{month} {}", self.day)
}
}
/// "Nov 12, 14:05" for a moment given in Unix seconds.
pub fn moment_text(unix: i64) -> String {
let moment = moment(unix);
format!("{}, {}", moment.date(), moment.clock())
}
/// Spacing of the ticks on a time axis that long, in seconds: whole minutes,
/// hours or days, giving about four to eight ticks.
pub fn tick_step(span_secs: f64) -> i64 {
const STEPS: [i64; 14] = [
60, 120, 300, 600, 900, 1800, 3600, 7200, 10_800, 21_600, 43_200, 86_400, 172_800,
604_800,
];
STEPS
.into_iter()
.find(|step| span_secs / *step as f64 <= 7.5)
.unwrap_or(604_800)
}
/// The label for one tick of a time axis: the clock time, or the date for
/// ticks that fall on midnight or are a day or more apart.
pub fn tick_label(unix: i64, step: i64) -> String {
let moment = moment(unix);
if step >= 86_400 || unix.rem_euclid(86_400) == 0 {
moment.date()
} else {
moment.clock()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_have_thousands_separators() {
assert_eq!(count(0), "0");
assert_eq!(count(325), "325");
assert_eq!(count(2815), "2,815");
assert_eq!(count(123_456), "123,456");
assert_eq!(count(1_234_567_890), "1,234,567,890");
}
#[test]
fn durations_are_short_and_round_cleanly() {
assert_eq!(duration(0.0007), "0.7 ms");
assert_eq!(duration(0.0), "0.0 ms");
assert_eq!(duration(8.14), "8.1 s");
assert_eq!(duration(59.96), "1m 00s");
assert_eq!(duration(185.0), "3m 05s");
assert_eq!(duration(11_100.0), "3h 05m");
assert_eq!(duration(183_600.0), "2d 3h");
assert_eq!(duration(f64::NAN), "—");
}
#[test]
fn moments_match_the_calendar() {
assert_eq!(
moment(0),
Moment {
month: 1,
day: 1,
hour: 0,
minute: 0
}
);
// 1,700,000,000 is 14 November 2023, 22:13:20 UTC.
assert_eq!(moment_text(1_700_000_000), "Nov 14, 22:13");
// Day 95 after the epoch is 6 April 1970.
assert_eq!(moment_text(95 * 86_400 + 12 * 3600), "Apr 6, 12:00");
}
#[test]
fn numbers_are_round_and_ticked() {
assert_eq!(round_number(11_432.7), "11,400");
assert_eq!(round_number(7.42), "7.4");
assert_eq!(bytes(10 * 1024 * 1024 * 1024), "10.0 GB");
assert_eq!(tick_step(3660.0), 600);
assert_eq!(tick_step(86_640.0), 21_600);
assert_eq!(tick_label(43_200, 600), "12:00");
assert_eq!(tick_label(86_400, 600), "Jan 2");
}
}