Repository navigation
Conversation
Wires the savings package into HTTP. Three handlers, all gated on
state + prices being configured (mirrors /api/prices contract).
/api/savings/daily?days=N — KPI-bar feed
/api/savings/intraday?date — drill-down with flow decomposition +
predicted-vs-actual overlay from
planner_diagnostics
/api/savings/summary — today / 7d / 30d totals
Per-day memo cache (savingsCache) follows the same pattern as
dailyCache: past days computed once, today always re-runs. Adds a
small mpc.Service.LatestParams() getter so the handlers can read
ExportOrePerKWh / ExportBonusOreKwh / ExportFeeOreKwh under RLock —
keeping reconstructed historical savings comparable with the live
"vs no battery" number on the MPC card.
Tests cover: nil state, nil prices, empty DB, today with seeded
history, intraday shape + bad-date 400, summary monotonicity, and
past-day cache hit (validates by closing the store after first
request and confirming the second still resolves yesterday).
Stacked on #198.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ff512e93f3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| s.mu.RLock() | ||
| defer s.mu.RUnlock() | ||
| return s.Defaults |
There was a problem hiding this comment.
Return effective export terms in LatestParams
LatestParams() currently returns s.Defaults verbatim, but the planner actually applies export bonus/fee from s.ExportBonusOreKwh and s.ExportFeeOreKwh during replan(). That means savings endpoints re-cost history with missing export terms whenever ExportOrePerKWh == 0 and bonus/fee are configured, producing incorrect actual_ore, no_battery_ore, and savings_ore that no longer match MPC’s own cost model.
Useful? React with 👍 / 👎.
| Actions []struct { | ||
| SlotStartMs int64 `json:"slot_start_ms"` | ||
| CostOre float64 `json:"cost_ore"` | ||
| BatteryW float64 `json:"battery_w"` | ||
| GridW float64 `json:"grid_w"` | ||
| } `json:"actions"` |
There was a problem hiding this comment.
Parse diagnostic slots instead of non-existent actions
The predicted overlay unmarshals planner diagnostics into a struct tagged with json:"actions", but persisted planner_diagnostics rows are mpc.Diagnostic payloads that expose slot rows under slots. In practice this leaves pa.Actions empty, so predicted_ore and prediction_error_ore are never attached even when matching diagnostics exist, making the overlay silently non-functional.
Useful? React with 👍 / 👎.
Stacked on #198.
Summary
PR 2 of 3 in the historical savings feature track. Wires the
pure-Go reconstruction package from #198 into HTTP.
Three handlers, all gated on
Deps.State != nilANDDeps.Prices != nil— handlers return200 {enabled: false}whenthe dependencies aren't configured, mirroring
/api/prices.GET /api/savings/daily?days=NGET /api/savings/intraday?date=YYYY-MM-DDGET /api/savings/summaryWhat's added
savingsCache— per-day memoization keyed byYYYY-MM-DD, samepattern as
dailyCache. Past days computed once per process,today always re-runs.
mpc.Service.LatestParams()— RLock'd snapshot ofDefaultssothe handlers can re-cost historical slots with the same export
terms (
ExportOrePerKWh/ExportBonusOreKwh/ExportFeeOreKwh)the live plan applies.
attachPredictedOverlay— best-effort: for each slot, look up theplanner_diagnosticssnapshot active at slot start, find thematching
Action.CostOre, attachpredicted_oreandprediction_error_oreto the row. Snapshots are cached within onerequest — reading the same Plan JSON 96 times for one day's
drill-down would be silly. Failures are silent (the slot ships
without the optional fields).
Test plan
go test ./internal/api/...— 8 new tests pass:lets the second request resolve yesterday (today recomputes and
can 500, which is fine — it's correctly excluded from the cache)
go test ./...— full suite green (34 packages, 600+ tests).What's NOT in this PR
web/savings.js+ KPI card + drill-down tableland in PR 3. Without the UI nothing visibly changes for operators
yet, but
curlalready gives real numbers from a populated state.db.