Repository navigation
perf(state): persist daily energy aggregates — kill the 25 s cold-start - #308
Merged
Merged
Conversation
2026-05-25 measurement on the live Pi (state.db = 1 GB):
/api/energy/daily?days=30 → ~25 s (cold cache)
/api/energy/daily?days=90 → ~15 s (cold cache, fewer cache misses
because the date range fits a
previously-warm window)
/api/energy/daily?days=7 → ~0.35 s (mostly warm)
The handler already had an in-memory cache, but it dies on every
restart. Closed days are immutable — recomputing them every cold
start is pure waste.
Add a persistent two-tier cache:
1. New SQLite table energy_daily(day PK, six Wh columns,
computed_at_ms).
2. State methods LoadDailyEnergy / SaveDailyEnergy.
3. handleEnergyDaily checks in-memory → persistent → recompute,
and lazily writes a row on first computation for any closed
day. Today's row is excluded — the day is still accumulating.
Expected impact: cold /api/energy/daily?days=30 drops from ~25 s to
the cost of 30 indexed PK lookups (<50 ms on the Pi). After the
first warm load the persistent rows live forever; restart-free
benefit accrues from there.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.
Measured baseline (2026-05-25, live Pi, state.db = 1 GB)
/api/energy/daily?days=30/api/energy/daily?days=90/api/energy/daily?days=7Snapshot dir = 6 GB (9 snapshots × ~670 MB each), state.db = 1 GB.
Root cause
The handler already had an in-memory cache, but it dies on every restart. Closed days are immutable — recomputing the per-day SQL integration every cold start is pure waste.
What this PR does
Persistent two-tier cache:
energy_daily(day PRIMARY KEY, ...six Wh columns, computed_at_ms).state.LoadDailyEnergy(day)/state.SaveDailyEnergy(day, de).handleEnergyDailychecks in-memory → persistent → recompute, and lazily writes a row on first computation for any closed day. Today's row is excluded (day still accumulating).Expected impact
Cold
/api/energy/daily?days=30drops from ~25 s to the cost of 30 indexed PK lookups (<50 ms on the Pi). After the first warm load the persistent rows live forever — restart-free benefit accrues from there.Tests
TestDailyEnergyPersistRoundtrip— save/load + upsert overwriteTestDailyEnergyMissReturnsFalse— miss semanticsgo test ./...greenOut of scope (follow-up)
🤖 Generated with Claude Code