commit a8e7bd66cfde3e0e3258a2fd1495f84b601713ce Author: Stephen Waits Date: Sat Nov 28 10:40:26 2026 -0700 Data contract between server and page, live stats fixture Co-Authored-By: Claude Fable 5.2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9f2772 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target/ +dist/ +.sisi/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d9b55d6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[workspace] +resolver = "2" +members = ["crates/api", "crates/server", "crates/ui"] + +[workspace.package] +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" +repository = "https://github.com/swaits/sccache-dash" + +[profile.release] +opt-level = "s" +lto = true diff --git a/crates/api/Cargo.toml b/crates/api/Cargo.toml new file mode 100644 index 0000000..751a72a --- /dev/null +++ b/crates/api/Cargo.toml @@ -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" diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs new file mode 100644 index 0000000..3b020a4 --- /dev/null +++ b/crates/api/src/lib.rs @@ -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, + /// 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, + pub timing: Timing, + pub time_saved: TimeSaved, + /// Sorted by hits + misses, largest first. + pub languages: Vec, + /// 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, + /// Most serious first. Empty means no problems. + pub problems: Vec, + /// Shown only when sccache reports them: multi-level cache, distributed compiling. + pub extras: Vec, + /// 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, + /// Version of the `sccache` command on the service's PATH. + pub client_version: Option, + /// As sccache words it, e.g. `Local disk: "/home/me/.cache/sccache"`. + pub cache_location: Option, + pub cache_size: Option, + pub max_cache_size: Option, + pub error: Option, +} + +#[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 { + ratio(self.hits, self.hits + self.misses) + } + /// Hits out of every compiler call. + pub fn hit_rate_of_all(&self) -> Option { + 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 { + (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, + pub avg_cache_write_ms: Option, + pub avg_compile_ms: Option, +} + +/// 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)>, +} diff --git a/fixtures/live-0.18.0.json b/fixtures/live-0.18.0.json new file mode 100644 index 0000000..63fd43f --- /dev/null +++ b/fixtures/live-0.18.0.json @@ -0,0 +1 @@ +{"stats":{"compile_requests":7351,"requests_unsupported_compiler":0,"requests_not_compile":27,"requests_not_cacheable":829,"requests_executed":6495,"cache_errors":{"counts":{},"adv_counts":{}},"cache_hits":{"counts":{"Rust":229,"C/C++":2082,"Assembler":886},"adv_counts":{"assemblerToPreprocess [gcc]":886,"c [gcc]":2082,"rust":229}},"cache_misses":{"counts":{"Rust":3256},"adv_counts":{"rust":3256}},"cache_timeouts":0,"cache_read_errors":0,"non_cacheable_compilations":0,"forced_recaches":0,"cache_write_errors":0,"cache_writes":3256,"cache_write_duration":{"secs":8,"nanos":758680079},"cache_read_hit_duration":{"secs":2,"nanos":190691578},"compilations":3256,"compiler_write_duration":{"secs":25343,"nanos":119040435},"compile_fails":39,"not_cached":{"missing input":18,"crate-type":621,"missing emit":7,"multiple input files":3,"incremental":105,"-":35,"-o":40},"dist_compiles":{},"dist_errors":0,"multi_level":null},"cache_location":"Local disk: \"/home/swaits/.cache/sccache\"","cache_size":10726353131,"max_cache_size":10737418240,"use_preprocessor_cache_mode":true,"version":"0.18.0","basedirs":[]} \ No newline at end of file