mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-27 06:46:01 +00:00
feat(stats): record daily snapshots and serve a trend API
The aggregation worker now writes one row per UTC day to stats_snapshots. The row holds package count, total size, vulnerable package count, the four severity counts and the blocked count. The write is an idempotent upsert per day. A migration backfills package counts from cached_at and deleted_at as the number of packages alive at the end of each day. Vulnerability fields backfill from scan_results timestamps and stay empty before the first scan. GET /api/stats/trend returns daily points for 30day, 90day or 1year with a constant number of queries. The Stats page shows a chart of cached and vulnerable packages with a period selector and severity toggles. The Dashboard shows a compact version. The Lean model gains theorems for the backfill and upsert.
This commit is contained in:
@@ -6,6 +6,10 @@ import Mathlib
|
||||
Models `pkg/metadata/gormstore/aggregation_worker.go` (`AggregateHourly`, `AggregateDaily`) and
|
||||
`pkg/metadata/gormstore/gormstore_v2.go` `GetTimeSeriesStats`. Time is in whole minutes since the
|
||||
epoch; an hour is 60 minutes and a day 24 hours. Omits: unique IPs, packages, registries.
|
||||
|
||||
The last three sections model the trend snapshots and backfill in `pkg/metadata/gormstore/snapshots.go`,
|
||||
the soft-delete purge in `pkg/metadata/gormstore/purge.go` and `pkg/cache/cache.go` `purgeDeleted`,
|
||||
and the severity filter in `pkg/metadata/gormstore/dashboard.go`.
|
||||
-/
|
||||
|
||||
namespace Gohoarder.Stats
|
||||
@@ -107,4 +111,112 @@ theorem fixed_sqlite_registry_row_idempotent (old new : ℕ) :
|
||||
theorem fixed_sqlite_package_row_keeps_late_events (old new : ℕ) :
|
||||
chartTotal (upsertAdditive old new) = old + new := by simp [chartTotal, upsertAdditive]
|
||||
|
||||
/-! ### Daily snapshots and backfill (`snapshots.go`) -/
|
||||
|
||||
/-- Minutes in a UTC day. -/
|
||||
def dayLen : ℕ := 1440
|
||||
|
||||
/-- `stateAtDayEnd`: a package cached at `cached` and deleted at `x` (`none` = still cached) counts in
|
||||
the snapshot of day `d` iff it was cached before the day ended and not deleted before it ended. -/
|
||||
def aliveAtEnd (cached : ℕ) (deleted : Option ℕ) (d : ℕ) : Prop :=
|
||||
cached < dayLen * (d + 1) ∧ ∀ x, deleted = some x → dayLen * (d + 1) ≤ x
|
||||
|
||||
/-- A package cached and deleted inside one day is in no snapshot. -/
|
||||
theorem same_day_package_never_counted (cached x d : ℕ) (hx : x < dayLen * (d + 1)) :
|
||||
¬ aliveAtEnd cached (some x) d := by
|
||||
rintro ⟨_, h⟩
|
||||
exact absurd (h x rfl) (by omega)
|
||||
|
||||
theorem undeleted_package_counts_from_its_day (cached d : ℕ) :
|
||||
aliveAtEnd cached none d ↔ cached < dayLen * (d + 1) := by
|
||||
simp [aliveAtEnd]
|
||||
|
||||
/-- The backfill adds only each day's change to a running total (`deltas`, then the cumulative loop). -/
|
||||
def delta (s : ℕ → ℤ) (d : ℕ) : ℤ := s d - (if d = 0 then 0 else s (d - 1))
|
||||
|
||||
/-- The running total of the daily changes equals the end-of-day state: the sweep loses nothing. -/
|
||||
theorem sweep_telescopes (s : ℕ → ℤ) (n : ℕ) :
|
||||
∑ d ∈ Finset.range (n + 1), delta s d = s n := by
|
||||
induction n with
|
||||
| zero => simp [delta]
|
||||
| succ n ih =>
|
||||
rw [Finset.sum_range_succ, ih]
|
||||
simp [delta]
|
||||
|
||||
/-- One snapshot row per day, `none` = no row. `upsertSnapshot` (`SnapshotStats`) replaces the day's row. -/
|
||||
def upsertSnapshot (rows : ℕ → Option ℕ) (day v : ℕ) : ℕ → Option ℕ := Function.update rows day (some v)
|
||||
|
||||
/-- Running the snapshot twice in a day leaves the last write only. -/
|
||||
theorem snapshot_last_write_wins (rows : ℕ → Option ℕ) (day a b : ℕ) :
|
||||
upsertSnapshot (upsertSnapshot rows day a) day b = upsertSnapshot rows day b :=
|
||||
Function.update_idem _ _ _
|
||||
|
||||
theorem snapshot_leaves_other_days (rows : ℕ → Option ℕ) (day other v : ℕ) (h : other ≠ day) :
|
||||
upsertSnapshot rows day v other = rows other :=
|
||||
Function.update_of_ne h _ _
|
||||
|
||||
/-- The backfill insert (`ON CONFLICT DO NOTHING`) never replaces an existing row. -/
|
||||
def insertMissing (rows : ℕ → Option ℕ) (day v : ℕ) : ℕ → Option ℕ :=
|
||||
fun d => if d = day then (rows d).or (some v) else rows d
|
||||
|
||||
theorem backfill_keeps_existing (rows : ℕ → Option ℕ) (day a v : ℕ) (h : rows day = some a) :
|
||||
insertMissing rows day v day = some a := by
|
||||
simp [insertMissing, h]
|
||||
|
||||
/-- The backfill runs only while no past-day snapshot exists, so it cannot rewrite history. -/
|
||||
def backfillRuns (pastRows : ℕ) : Bool := pastRows == 0
|
||||
|
||||
theorem backfill_idle_once_history_exists (n : ℕ) : backfillRuns (n + 1) = false := by
|
||||
simp [backfillRuns]
|
||||
|
||||
/-! ### Soft-delete purge (`purge.go`, `cache.go` `purgeDeleted`) -/
|
||||
|
||||
/-- Cutoff = now - retention; a row is purged iff it was deleted before the cutoff. A row that is
|
||||
not deleted (`none`) is never purged. -/
|
||||
def purgeable (now retention : ℕ) (deleted : Option ℕ) : Prop :=
|
||||
∃ x, deleted = some x ∧ x + retention < now
|
||||
|
||||
theorem active_row_never_purged (now retention : ℕ) : ¬ purgeable now retention none := by
|
||||
rintro ⟨x, h, _⟩
|
||||
cases h
|
||||
|
||||
theorem recent_delete_kept (now retention x : ℕ) (h : now ≤ x + retention) :
|
||||
¬ purgeable now retention (some x) := by
|
||||
rintro ⟨y, hy, hlt⟩
|
||||
cases hy
|
||||
omega
|
||||
|
||||
/-- The purge touches package rows only; the snapshot rows come out as they went in. -/
|
||||
def purgeStep {P : Type} (pkgs : List P) (keep : P → Bool) (snaps : ℕ → Option ℕ) :
|
||||
List P × (ℕ → Option ℕ) := (pkgs.filter keep, snaps)
|
||||
|
||||
theorem purge_keeps_snapshots {P : Type} (pkgs : List P) (keep : P → Bool) (snaps : ℕ → Option ℕ) :
|
||||
(purgeStep pkgs keep snaps).2 = snaps := rfl
|
||||
|
||||
/-- One pass purges at most `maxPurgeBatches` batches of `limit` rows. -/
|
||||
def maxPurgeBatches : ℕ := 100
|
||||
|
||||
theorem purge_pass_bounded (batches limit : ℕ) (h : batches ≤ maxPurgeBatches) :
|
||||
batches * limit ≤ maxPurgeBatches * limit :=
|
||||
Nat.mul_le_mul_right limit h
|
||||
|
||||
/-! ### Severity filter (`dashboard.go` `severityFilter`, `latestScanWith`) -/
|
||||
|
||||
/-- A package passes when no severity is selected, or its latest scan counts one of them. -/
|
||||
def passes (selected : List ℕ) (counts : ℕ → ℕ) : Prop := selected = [] ∨ ∃ s ∈ selected, 0 < counts s
|
||||
|
||||
/-- OR semantics: selecting one more severity never hides a package. -/
|
||||
theorem passes_monotone (sel sel' : List ℕ) (counts : ℕ → ℕ) (h : ∀ s ∈ sel, s ∈ sel')
|
||||
(hne : sel ≠ []) (hp : passes sel counts) : passes sel' counts := by
|
||||
right
|
||||
rcases hp with hp | ⟨s, hs, hc⟩
|
||||
· exact absurd hp hne
|
||||
· exact ⟨s, h s hs, hc⟩
|
||||
|
||||
/-- A package with no finding of any selected severity is filtered out. -/
|
||||
theorem clean_package_filtered (sel : List ℕ) (hne : sel ≠ []) : ¬ passes sel (fun _ => 0) := by
|
||||
rintro (h | ⟨s, _, hc⟩)
|
||||
· exact hne h
|
||||
· exact absurd hc (by simp)
|
||||
|
||||
end Gohoarder.Stats
|
||||
|
||||
Reference in New Issue
Block a user