Optional retention: trim old raw JSON, keep every column forever - #20
Merged
Conversation
--retain-raw-days N (min 7; default 0 = never, preserving the existing
promise) blanks the raw JSON blob on vitals/wifi/lifetime/ambient
samples older than N days in a daily chunked background pass. Columns
are untouched, so charts, session pages, the thermal model's 120-day
lookback and the degradation watch see identical history; what is given
up is re-interpreting trimmed rows for fields never extracted. Vitals
wait for the diagnostics backfill (its source is the raw blob);
forecast snapshots and version info are never trimmed. Freed pages are
reused so the file goes near-flat; a one-shot --compact command VACUUMs
to hand space back to the filesystem.
Hardened by its own tests: SQLite does not promise to short-circuit
COALESCE past json_extract(''), so the pre-backfill diagnostics
fallback is CASE-guarded against trimmed rows, and vacuum() checkpoints
the WAL before sizing.
Measured on a full 45-day production snapshot: retain 14 days trimmed
1,127,172 rows in 8.3 s and --compact shrank the file 1331.7 -> 636.5 MB
in 6.7 s.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
20 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Nothing is ever deleted, and the database grows ~1 GB/month on a busy install — measured on production: 1.12 GB in 45 days, of which
vitals_samplesis 94 % and the raw JSON blobs are ~84 % of that (~600 of ~712 B/row). Backups, disk on the always-on box, and every copy-for-analysis workflow degrade monthly. Roadmap item from #1 ("database stays near-flat on long-running installs").Code touched
wallmonitor/config.py—--retain-raw-days N(WM_RETAIN_RAW_DAYS; default 0 = never trim, preserving the current promise; minimum 7 so a typo can't gut a database) and--compact(one-shot maintenance run: VACUUM, print sizes, exit; exempt from the--hostrequirement like--discover).wallmonitor/db.pytrim_raw(cutoff_ts)blanksrawto''(the column isNOT NULL) onvitals/wifi/lifetime/ambientsamples older than the cutoff, in 10k-row chunks each holding the write lock briefly. A per-table timestamp cursor in settings makes the daily pass scan only rows that newly aged past the cutoff. Vitals are gated on the diagnostics backfill being complete — the backfill's source is the raw blob.forecast_samples(tiny; raw carries fields with no column, e.g.steady_state_se_chistory) andversion_infoare never trimmed.vacuum()checkpoints the WAL (TRUNCATE) before and afterVACUUMso the reported sizes mean what they say.CASE-guardsjson_extract— SQLite does not promise to short-circuitCOALESCEpastjson_extract(''), which is a hard error, not NULL; and the backfillUPDATEskipsraw = ''rows.wallmonitor/__main__.py— the backfill task grows into_maintenance: one-time backfill, then (when retention is enabled) a daily trim pass, with counts logged.--compacthandled before any component starts.wallmonitor/poller.py—retain_raw_daysexposed on/api/statusfor future UI honesty.Docs —
running.mdgains a Retention section (what is kept, what re-interpretability costs, why the file plateaus rather than shrinks, how--compactreclaims);recording.md's fidelity claim and the README bullet now reference the policy.Deliberately not in scope: downsampling old samples to a coarser cadence (phase 2 if ever needed — columns at full cadence are only ~16 % of the growth once raw is trimmed), automated backups, CSV export.
Risk
CASEguard,raw != '').--compactis exclusive while it runs; documented to run with the service stopped. The daily trim itself never shrinks the file — freed pages are reused, which is the "near-flat" behavior wanted.Verification
tests/test_retention.py: cutoff respected with columns intact and served through bothvitals_rangebranches on trimmed rows; the vitals gate; cursor resumes instead of rescanning; VACUUM reclaims the trimmed bytes (WAL-checkpointed sizing); flag validation (3rejected,30accepted, default off). The first run of these tests caught both hardening bugs above — theCOALESCEnon-short-circuit would have broken pre-backfill queries on any trimmed database.trim_rawat 14-day retention touched 1,127,172 rows in 8.3 s;--compactshrank the file 1331.7 → 636.5 MB in 6.7 s. Steady state at 14-day retention: raw exists only for the newest two weeks (~250 MB of blobs) plus columns forever (~5 MB/month) — the file plateaus.Deploy:
git pull+ restart; enabling retention is a per-install decision (WM_RETAIN_RAW_DAYSin the service unit). Suggested for the reference install: 90 days to start — comfortably beyond the thermal model's 120-day column lookback (which retention doesn't touch anyway) and every past reanalysis window, while still capping growth.🤖 Generated with Claude Code