From 8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 20 Nov 2026 17:13:44 +0000 Subject: db: one module per aggregate, and an interface you can read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit queries.rs was 1,501 lines holding 83 queries across seventeen aggregates, separated by comment banners that said "settings", "release", "notices" and then kept them in one file. The banners were the design; they just never got files. Each one has one now, verbatim — the split is provably content-only, the moved text being identical to the original as a sorted set of lines. model.rs and builds.rs were each two concerns deep, so they became directories. model.rs held what a row looks like and, separately, the state machines that say which moves are legal; builds.rs held the build machine and, below its own `step rows` banner, the step machine. The impl_state_enum! macro implements StateEnum for all five enums from one place, so it moved with its invocations rather than being exported across a boundary to chase them. They are directories rather than new sibling modules on purpose: `db::model::CandidateState` and `db::builds::steps` are used across the crate and by the integration tests, and renaming them would be a bigger change than the one being made. No call site and no test changed. db/mod.rs now lists its interface instead of spraying one. `pub use model::*` and `pub use queries::*` are gone, replaced by seventeen explicit exports, and the query modules are private — so a query that was not chosen for export is not reachable, and the width of the module is visible from its own text rather than only from a grep. The ratchet caught the win immediately: db.glob went 2 -> 0, which failed a_shrunk_interface_is_written_back until the budget was written back at the new value. db is off the glob allowlist now rather than holding a stale zero. 277 passed / 0 failed, clippy -D warnings clean, largest file in src/db 363 lines (candidates.rs, unchanged from before this work). --- src/db/metrics.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/db/metrics.rs (limited to 'src/db/metrics.rs') diff --git a/src/db/metrics.rs b/src/db/metrics.rs new file mode 100644 index 0000000..39a6b10 --- /dev/null +++ b/src/db/metrics.rs @@ -0,0 +1,101 @@ +use rusqlite::params; + +use crate::db::*; +use crate::error::{Error, Result}; +use crate::time; + +// -------------------------------------------------------------- metrics reads -- + +fn count_rows(db: &Db, sql: &str) -> Result { + let sql = sql.to_string(); + db.read(move |c| { + c.query_row(&sql, [], |r| r.get::<_, i64>(0)) + .map_err(|e| Error::db(e.to_string())) + }) +} + +pub fn build_state_counts(db: &Db) -> Result> { + db.read(|c| { + let mut st = c.prepare("SELECT state, count(*) FROM build GROUP BY state ORDER BY state")?; + let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, i64>(1)?)))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn oldest_queued_age_secs(db: &Db, now: &str) -> Result { + db.read(|c| { + c.query_row( + "SELECT julianday(?1) - julianday(min(created_at)) FROM build WHERE state='queued'", + params![now], + |r| r.get::<_, Option>(0), + ) + .map(|v| (v.unwrap_or(0.0) * 86400.0) as i64) + .map(|v| v.max(0)) + .map_err(|e| Error::db(e.to_string())) + }) +} + +pub fn count_pending_reviews(db: &Db) -> Result { + count_rows(db, "SELECT count(*) FROM candidate WHERE state='pending_review'") +} + +pub fn pending_review_ages_secs(db: &Db, now: &str) -> Result> { + db.read(|c| { + let mut st = c.prepare("SELECT julianday(?1) - julianday(detected_at) FROM candidate WHERE state='pending_review'")?; + let rows = st.query_map(params![now], |r| r.get::<_, Option>(0))?; + let mut out = Vec::new(); + for r in rows { + out.push((r?.unwrap_or(0.0) * 86400.0) as i64); + } + Ok(out) + }) +} + +pub fn count_index_packages(db: &Db) -> Result { + count_rows(db, "SELECT count(*) FROM package WHERE state='in_index'") +} + +pub fn count_published_releases(db: &Db) -> Result { + count_rows(db, "SELECT count(*) FROM release WHERE state='published'") +} + +pub fn count_subscriptions(db: &Db) -> Result { + count_rows(db, "SELECT count(*) FROM subscription") +} + +pub fn count_unacknowledged_notices(db: &Db) -> Result { + count_rows(db, "SELECT count(*) FROM notice WHERE acknowledged_at IS NULL") +} + +pub fn artifact_bytes_served(db: &Db) -> Result { + db.read(|c| { + c.query_row( + "SELECT COALESCE(sum(size),0) FROM artifact WHERE purged_at IS NULL", + [], + |r| r.get::<_, i64>(0), + ) + .map(|v| v.max(0) as u64) + .map_err(|e| Error::db(e.to_string())) + }) +} + +pub fn last_gc_age_secs(db: &Db, now: &str) -> Result> { + let at = db.read(|c| { + c.query_row("SELECT max(ran_at) FROM gc_run", [], |r| r.get::<_, Option>(0)) + .map_err(|e| Error::db(e.to_string())) + })?; + Ok(at.and_then(|t| time::delta_secs(now, &t))) +} + +pub fn last_poll_age_secs(db: &Db, now: &str) -> Result> { + let at = db.read(|c| { + c.query_row("SELECT max(last_checked_at) FROM subscription", [], |r| r.get::<_, Option>(0)) + .map_err(|e| Error::db(e.to_string())) + })?; + Ok(at.and_then(|t| time::delta_secs(now, &t))) +} + -- cgit v1.2.3