Files
sccache-dash/crates/api/src/lib.rs
T
2026-11-28 11:40:04 -07:00

251 lines
8.1 KiB
Rust

//! 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,
}
impl Range {
pub const ALL: [Range; 5] = [
Range::Hour,
Range::Day,
Range::Week,
Range::Month,
Range::SinceReset,
];
/// The value used in `?range=`.
pub fn code(self) -> &'static str {
match self {
Range::Hour => "1h",
Range::Day => "24h",
Range::Week => "7d",
Range::Month => "30d",
Range::SinceReset => "reset",
}
}
pub fn parse(code: &str) -> Option<Range> {
Range::ALL.into_iter().find(|r| r.code() == code)
}
/// For headings: "last 24 hours", "since reset".
pub fn label(self) -> &'static str {
match self {
Range::Hour => "last hour",
Range::Day => "last 24 hours",
Range::Week => "last 7 days",
Range::Month => "last 30 days",
Range::SinceReset => "since reset",
}
}
}
#[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,
/// What is known about how long each language takes to build, from saved
/// history. Sorted by compiles seen, largest first.
#[serde(default)]
pub build_times: Vec<BuildTimes>,
/// 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,
/// Every compile that was built instead of served from cache. Timeouts,
/// read errors and forced recaches are counted inside this, as sccache does.
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,
/// The range the true saving falls in with 90% confidence (low, high), from
/// resampling the build times seen. `None` when there is too little data.
#[serde(default)]
pub interval_90: Option<(f64, f64)>,
pub hits_left_out: u64,
/// Plain-language account of how the figure was reached, for the hover note.
pub note: String,
}
/// The spread of build times seen for one language. sccache reports only running
/// totals, so each data point is the average over the builds that finished
/// between two readings ten seconds apart: often one build, sometimes a few.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct BuildTimes {
pub language: String,
/// Builds seen.
pub compiles: u64,
pub mean_ms: f64,
pub median_ms: f64,
pub p90_ms: f64,
/// Histogram, shortest first, bin edges evenly spaced on a log scale.
pub bins: Vec<Bin>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Bin {
pub from_ms: f64,
pub to_ms: f64,
pub compiles: u64,
}
#[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)>,
}