Phase 7: telemetry and usage (daily rollup + week dashboard)

Build on Phases 1–6. Telemetry already arrived on
restrict/<device>/telemetry; this phase stores it in a daily rollup and
shows it.

- store: migration 003 adds telemetry_daily (device, day, app seconds,
  site seconds). InsertTelemetry maintains the affected days in the same
  transaction; RebuildRollups backfills and repairs it. UsageRange and
  TopUsage serve the week grid and top lists. CapAudit enforces the
  size-capped append-only audit log from AGENTS.md §7.
- web: GET /devices/{id}/usage — Monday-to-Sunday totals plus the top 10
  apps and top 10 sites, using CSP-safe native <progress> bars.
- theme.js: light/dark from the OS preference, with a nav toggle, loaded
  as an external script so the strict CSP still holds.
- cmd/control: `rollup` subcommand; rollup rebuild and audit cap at
  start-up and every 15 minutes.
- Version 0.5.0-phase7 (compose, Makefile, docs).

Tests: go test ./... and go vet ./... pass in golang:1.25-alpine.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-10-13 16:18:56 +11:00
parent 9af543c394
commit b58993d994
20 changed files with 1077 additions and 20 deletions

View File

@@ -0,0 +1,24 @@
-- Migration 003 — Phase 7. Daily usage rollup (AGENTS.md §7 retention).
--
-- telemetry holds the per-day detail: one row per (device, day, app, domain).
-- This table is the daily rollup the dashboard reads for a week and that is
-- kept long. Apps and domains are summed into separate columns on purpose: a
-- report can carry both for the same session, so adding them together would
-- double-count the time.
--
-- The rollup is written whenever a telemetry report lands and can be rebuilt
-- from telemetry at any time (store.RebuildRollups), so it is derived data,
-- never a second source of truth.
CREATE TABLE IF NOT EXISTS telemetry_daily (
device_id INTEGER NOT NULL REFERENCES devices(id),
day TEXT NOT NULL, -- YYYY-MM-DD, local day
app_seconds INTEGER NOT NULL DEFAULT 0,
domain_seconds INTEGER NOT NULL DEFAULT 0,
app_count INTEGER NOT NULL DEFAULT 0,
domain_count INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
PRIMARY KEY (device_id, day)
);
CREATE INDEX IF NOT EXISTS idx_telemetry_daily_day ON telemetry_daily(day);