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

+185
View File
@@ -0,0 +1,185 @@
//! Colours and small drawings shared by the sections.
use eframe::egui::{self, Color32, Pos2, RichText, Sense, Shape, Stroke, Ui};
/// Served from the cache.
pub const CACHE: Color32 = Color32::from_rgb(0x4C, 0xAF, 0x50);
/// Compiled because the cache had nothing.
pub const BUILT: Color32 = Color32::from_rgb(0x42, 0xA5, 0xF5);
pub const NOT_CACHEABLE: Color32 = Color32::from_rgb(0xFF, 0xA7, 0x26);
pub const UNSUPPORTED: Color32 = Color32::from_rgb(0xAB, 0x47, 0xBC);
pub const NOT_COMPILE: Color32 = Color32::from_rgb(0x26, 0xA6, 0x9A);
pub const ERRORS: Color32 = Color32::from_rgb(0xEF, 0x53, 0x50);
/// Text colours that mean "good", "worth a look" and "bad", for the theme being drawn.
pub fn marks(dark: bool) -> [Color32; 3] {
if dark {
[
Color32::from_rgb(0x3F, 0xB9, 0x50),
Color32::from_rgb(0xD2, 0x99, 0x22),
Color32::from_rgb(0xF8, 0x51, 0x49),
]
} else {
[
Color32::from_rgb(0x1A, 0x7F, 0x37),
Color32::from_rgb(0x9A, 0x67, 0x00),
Color32::from_rgb(0xCF, 0x22, 0x2E),
]
}
}
/// The colour of a hit rate: green from 70%, yellow from 40%, red below.
pub fn hit_rate_color(rate: Option<f64>, dark: bool) -> Color32 {
let [good, warn, bad] = marks(dark);
match rate {
Some(rate) if rate >= 0.7 => good,
Some(rate) if rate >= 0.4 => warn,
Some(_) => bad,
None => Color32::GRAY,
}
}
/// A titled section that starts open and can be folded away.
pub fn section<R>(
ui: &mut Ui,
id: &'static str,
title: impl Into<egui::WidgetText>,
body: impl FnOnce(&mut Ui) -> R,
) -> Option<R> {
ui.add_space(8.0);
egui::CollapsingHeader::new(title.into().strong().size(17.0))
.id_salt(id)
.default_open(true)
.show(ui, body)
.body_returned
}
/// A message that must be noticed, in the colour of how bad it is.
pub fn banner(ui: &mut Ui, text: impl Into<RichText>, color: Color32) {
let mut text: RichText = text.into();
text = text.strong();
egui::Frame::NONE
.fill(color.gamma_multiply(0.22))
.stroke(Stroke::new(1.0, color))
.corner_radius(6.0)
.inner_margin(8.0)
.show(ui, |ui| {
ui.add(egui::Label::new(text).wrap());
});
ui.add_space(6.0);
}
/// A small coloured square in front of a legend entry.
pub fn swatch(ui: &mut Ui, color: Color32) {
let (rect, _) = ui.allocate_exact_size(egui::vec2(10.0, 10.0), Sense::hover());
ui.painter().rect_filled(rect, 2.0, color);
}
/// A trend line one point per bucket, drawn in a strip `height` tall. `None`
/// points break the line, so a gap never looks like a zero. `top` is the value
/// drawn at the top of the strip.
pub fn sparkline(ui: &mut Ui, height: f32, values: &[Option<f64>], top: f64, color: Color32) {
let (rect, _) = ui.allocate_exact_size(egui::vec2(ui.available_width(), height), Sense::hover());
let painter = ui.painter();
painter.hline(
rect.x_range(),
rect.bottom() - 0.5,
Stroke::new(1.0, ui.visuals().widgets.noninteractive.bg_stroke.color),
);
if values.is_empty() || top <= 0.0 {
return;
}
let at = |index: usize, value: f64| {
Pos2::new(
rect.left() + rect.width() * ((index as f32 + 0.5) / values.len() as f32),
rect.bottom() - (rect.height() * (value / top) as f32).min(rect.height()),
)
};
let mut run: Vec<Pos2> = Vec::new();
let flush = |run: &mut Vec<Pos2>, painter: &egui::Painter| match run.len() {
0 => {}
1 => {
painter.circle_filled(run[0], 2.0, color);
run.clear();
}
_ => {
painter.add(Shape::line(std::mem::take(run), Stroke::new(1.5, color)));
}
};
for (index, value) in values.iter().enumerate() {
match value {
Some(value) => run.push(at(index, *value)),
None => flush(&mut run, painter),
}
}
flush(&mut run, painter);
}
/// A bar filled from the left to `fraction` of its width (0 to 1), always `height` tall.
pub fn meter(ui: &mut Ui, height: f32, fraction: f64, fill: Color32) {
let (rect, _) = ui.allocate_exact_size(egui::vec2(ui.available_width(), height), Sense::hover());
let painter = ui.painter();
painter.rect_filled(rect, height / 2.0, ui.visuals().extreme_bg_color);
let width = (rect.width() * fraction.clamp(0.0, 1.0) as f32).min(rect.width());
if width > 0.5 {
painter.rect_filled(
egui::Rect::from_min_max(rect.min, egui::pos2(rect.left() + width, rect.bottom())),
height / 2.0,
fill,
);
}
}
/// One bar split into coloured parts. The parts fill the whole width when
/// their counts add up to `total`; a part with no count gets no width.
pub fn stacked_bar(ui: &mut Ui, height: f32, parts: &[(&'static str, u64, Color32)], total: u64) {
let (rect, response) = ui.allocate_exact_size(egui::vec2(ui.available_width(), height), Sense::hover());
let painter = ui.painter();
painter.rect_filled(rect, 4.0, ui.visuals().extreme_bg_color);
let mut x = rect.left();
let mut tip = String::new();
for (name, part, color) in parts {
let width = if total > 0 {
rect.width() * *part as f64 as f32 / total as f32
} else {
0.0
};
if width > 0.0 {
let part_rect = egui::Rect::from_min_max(egui::pos2(x, rect.top()), egui::pos2(x + width, rect.bottom()));
painter.rect_filled(part_rect, 4.0, *color);
x += width;
tip.push_str(&format!("{name}: {} ({})\n", crate::fmt::count(*part), crate::fmt::share(*part, total)));
}
}
if !tip.is_empty() {
response.clone().on_hover_text(tip.trim_end());
}
}
/// A short bar of fixed width showing hits against misses for one language.
pub fn hit_bar(ui: &mut Ui, width: f32, hits: u64, misses: u64) {
let (rect, _) = ui.allocate_exact_size(egui::vec2(width, 12.0), Sense::hover());
let painter = ui.painter();
painter.rect_filled(rect, 3.0, ui.visuals().extreme_bg_color);
let total = hits + misses;
if total > 0 {
let hit_width = rect.width() * hits as f32 / total as f32;
painter.rect_filled(
egui::Rect::from_min_max(rect.min, egui::pos2(rect.left() + hit_width, rect.bottom())),
3.0,
CACHE,
);
}
}
/// Where a value between a tenth of a millisecond and 100 seconds sits on a
/// scale that lets 0.7 ms and 8 s both be seen.
pub fn log_fraction(ms: f64) -> f64 {
const LOW: f64 = -1.0;
const HIGH: f64 = 5.0;
if !ms.is_finite() || ms <= 0.0 {
return 0.0;
}
let position = (ms.log10() - LOW) / (HIGH - LOW);
position.clamp(0.02, 1.0)
}