Data contract between server and page, live stats fixture

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-28 10:40:26 -07:00
commit a8e7bd66cf
5 files changed
+212

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
[package]
name = "sccache-dash-api"
description = "Data contract between the sccache-dash server and its page"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+185
View File
@@ -0,0 +1,185 @@
//! The contract between the server and the page.
//!
//! The server does all the arithmetic and sends one `View`; the page only draws it.
//!
//! - `GET /api/view?range=1h|24h|7d|30d|reset` -> `View` as JSON
//! - `GET /api/export` -> the current stats exactly as
//! `sccache --show-stats --stats-format json` prints them, as a download
//! - `POST /api/zero` -> zeroes sccache's counters; saved history is kept
use serde::{Deserialize, Serialize};
pub const DEFAULT_PORT: u16 = 8226;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Range {
#[serde(rename = "1h")]
Hour,
#[serde(rename = "24h")]
Day,
#[serde(rename = "7d")]
Week,
#[serde(rename = "30d")]
Month,
/// sccache's own counters since they were last zeroed or its server started.
#[serde(rename = "reset")]
SinceReset,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct View {
pub range: Option<Range>,
/// Unix seconds when the server made this view.
pub generated_at: i64,
pub server: Server,
/// Counts within the range.
pub totals: Totals,
/// The period of the same length just before the range; `None` when there is
/// no saved data for it or the range is `SinceReset`.
pub previous: Option<Totals>,
pub timing: Timing,
pub time_saved: TimeSaved,
/// Sorted by hits + misses, largest first.
pub languages: Vec<Language>,
/// Why compiles were not cacheable, largest first, e.g. ("crate-type", 562).
pub not_cached: Vec<(String, u64)>,
/// Oldest first, evenly spaced, at most 120.
pub buckets: Vec<Bucket>,
/// Most serious first. Empty means no problems.
pub problems: Vec<Problem>,
/// Shown only when sccache reports them: multi-level cache, distributed compiling.
pub extras: Vec<Extra>,
/// The current sccache stats JSON, untouched. `Null` when it could not be read.
pub raw: serde_json::Value,
/// The text "Copy Summary" puts on the clipboard.
pub summary_markdown: String,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum Status {
/// Compiles arrived in the last five minutes.
Running,
/// Up, but no compiles in the last five minutes.
Idle,
/// sccache is missing or would not start; see `Server::error`.
#[default]
NotRunning,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Server {
pub status: Status,
/// Version the running server reports.
pub version: Option<String>,
/// Version of the `sccache` command on the service's PATH.
pub client_version: Option<String>,
/// As sccache words it, e.g. `Local disk: "/home/me/.cache/sccache"`.
pub cache_location: Option<String>,
pub cache_size: Option<u64>,
pub max_cache_size: Option<u64>,
pub error: Option<String>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Totals {
pub requests: u64,
pub executed: u64,
pub hits: u64,
pub misses: u64,
pub not_cacheable: u64,
pub unsupported_compiler: u64,
pub not_compile: u64,
pub timeouts: u64,
pub read_errors: u64,
pub write_errors: u64,
/// The sum of sccache's per-language `cache_errors`.
pub other_errors: u64,
pub compile_fails: u64,
pub forced_recaches: u64,
pub cache_writes: u64,
}
impl Totals {
/// Hits out of cacheable compiles: the figure sccache itself prints.
pub fn hit_rate(&self) -> Option<f64> {
ratio(self.hits, self.hits + self.misses)
}
/// Hits out of every compiler call.
pub fn hit_rate_of_all(&self) -> Option<f64> {
ratio(self.hits, self.requests)
}
pub fn errors(&self) -> u64 {
self.read_errors + self.write_errors + self.other_errors
}
}
fn ratio(n: u64, d: u64) -> Option<f64> {
(d > 0).then(|| n as f64 / d as f64)
}
/// Averages within the range, in milliseconds; `None` when nothing of that kind happened.
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Timing {
pub avg_hit_read_ms: Option<f64>,
pub avg_cache_write_ms: Option<f64>,
pub avg_compile_ms: Option<f64>,
}
/// A rough, deliberately low estimate. A hit counts only when the build time of
/// its language is known from saved history; other hits are left out and counted
/// in `hits_left_out`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct TimeSaved {
pub seconds: f64,
pub hits_left_out: u64,
/// Plain-language account of how the figure was reached, for the hover note.
pub note: String,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Language {
pub name: String,
pub hits: u64,
pub misses: u64,
pub errors: u64,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Bucket {
/// Unix seconds at the start of the bucket.
pub start: i64,
pub hits: u64,
pub misses: u64,
/// Not cacheable + unsupported compiler + not a compile.
pub other: u64,
pub errors: u64,
pub saved_seconds: f64,
/// (language, hits, misses), for the language filter.
pub languages: Vec<(String, u64, u64)>,
/// sccache's counters went back to zero in this bucket.
pub reset: bool,
/// Nothing was recorded in this bucket (the service was not running).
pub gap: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Severity {
Warning,
Error,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Problem {
pub severity: Severity,
/// Short, e.g. "Rust hit rate 8%".
pub title: String,
/// One sentence saying what it means and what to try.
pub detail: String,
}
/// A titled table of label and value, drawn as given.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Extra {
pub title: String,
pub rows: Vec<(String, String)>,
}