Keep the history in SQLite, one view per range made after each poll

Each row of the readings table is the difference since the row before it,
at most one a minute and only when something changed, so a range total is
one indexed query. Per-language counts, not-cacheable reasons and build
observations have their own tables; what the last poll and the last row
saw is remembered in the state table, so differences survive a restart.

The five views are computed once per poll and kept; /api/view answers
from them and never touches the database, which only the polling task
holds. /api/zero wakes that task instead of reading the counters itself.

Build observations: after a poll in which every build that finished was
of one language, one row with the difference in compilations and compile
time, for the build-time histogram. Time saved uses their per-language
average. Views fill the api's build_times and interval_90 as empty/None.
This commit is contained in:
swaits committed 2026-11-28 11:44:50 -07:00
1 parent a83ae5e0d9
commit 7f58931b7b
8 files changed
+970 -642

No files matched your search

+13 -16
View File
@@ -106,6 +106,13 @@ impl Counters {
(if reset { self.clone() } else { diff }, reset)
}
/// The language of the misses when they were all of one language: (name, misses).
pub fn one_missed_language(&self) -> Option<(&str, u64)> {
let mut with_misses = self.langs.iter().filter(|(_, v)| v[1] > 0);
let (name, v) = with_misses.next()?;
with_misses.next().is_none().then(|| (name.as_str(), v[1]))
}
/// Adds another set of counters into this one.
pub fn add(&mut self, other: &Counters) {
macro_rules! sum {
@@ -422,22 +429,12 @@ mod tests {
}
#[test]
fn a_saved_line_is_small() {
let reading = crate::history::Reading {
t: 1_764_000_000,
c: parse(LIVE).unwrap().counters,
};
let line = serde_json::to_string(&reading).unwrap();
assert!(line.len() < 1024, "{} bytes: {line}", line.len());
assert!(
line.starts_with("{\"t\":1764000000,\"c\":{\"totals\":{\"requests\":7351"),
"{line}"
);
assert!(line.ends_with("[\"multiple input files\",3]]}}"), "{line}");
assert_eq!(
serde_json::from_str::<crate::history::Reading>(&line).unwrap(),
reading
);
fn counters_survive_the_round_trip_through_saved_state() {
// The totals sccache-dash remembers between polls are saved as JSON.
let counters = parse(LIVE).unwrap().counters;
let text = serde_json::to_string(&counters).unwrap();
assert_eq!(serde_json::from_str::<Counters>(&text).unwrap(), counters);
assert!(text.len() < 1024, "{} bytes: {text}", text.len());
}
#[test]