Skip to content

feat(state): aggregate dashboard history after counting interval energy - #1294

Merged
frahlg merged 2 commits into
masterfrom
feat/dashboard-energy-aggregates-20260917
Sep 17, 2026
Merged

frahlg merged 2 commits into
masterfrom
feat/dashboard-energy-aggregates-20260917

Conversation

@frahlg

@frahlg frahlg commented Sep 17, 2026

Copy link
Copy Markdown
Member

Dashboard power rows still grew at poll cadence after scalar history moved to bounded aggregates. Averaging those rows before counting energy would cancel short import/export changes and hide gaps.

Record signed energy and measured coverage from each original site interval in the same transaction as its retry receipt. Keep chart summaries at 10 seconds for 24 hours, one minute through 30 days and five minutes through two years. These small site summaries stay in SQLite; scalar history uses the Parquet path merged in #1290. Minute energy totals remain for two years, then quarter-hour totals preserve local day boundaries and normal tariff periods. Existing legacy chart tiers remain readable.

Energy and cost readers use the interval totals. A range or tariff edge inside a retained interval remains uncovered; the code never invents how that energy split. Missing site observations break integration. Chart means retain sample counts and source resolution; detail JSON retains its actual timestamp and is marked unsuitable for forecast training. Schema 6 prevents an older binary from silently ignoring the new history format.

Validation: full local make verify passed. Regression tests compare original and aggregated import/export, battery flows, costs, tariff boundaries, outages, receipt retries, restart, retention and local day boundaries. A full backup/restore test verifies the energy cursor and duplicate timestamp handling.

Closes #1291. Continues #1287. Does not close the SD-card IO latency gate in #1246 or the control-loop IO issue #1247. No running Core was replaced and no EV charging cycle was tested.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-17T12:09:28.696931Z 0c14db7 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c14db7717

ℹ️ 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".

SELECT ts_ms, grid_w, pv_w, bat_w, load_w, bat_soc, 2,1,86400000,ts_ms FROM history_cold
WHERE ts_ms BETWEEN ? AND ?
),
UNION ALL SELECT last_ms,grid_w,pv_w,bat_w,load_w,bat_soc,-1,n,resolution_ms,first_ms FROM history_dashboard WHERE last_ms BETWEEN ? AND ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep dashboard aggregates out of research training data

When aggregation is enabled, this adds history_dashboard rows to every LoadHistory consumer even though their flat power fields are sample means while their JSON contains only the newest tick. The inspected handleLoadResearchDump/buildLoadResearchBuckets path consumes these rows without checking resolution_ms or the new aggregate_not_for_training marker, extracts EV/V2X power from that last-tick JSON, and counts each aggregate as one sample; for example, two ticks with different EV power can export averaged grid power paired with only the second EV value. This silently corrupts the model-oriented research bundle, so that path needs compatible per-signal aggregates or must reject these dashboard summaries.

Useful? React with 👍 / 👎.

Comment thread go/internal/state/history_dashboard.go Outdated
Comment on lines +95 to +98
_, err := tx.ExecContext(ctx, `INSERT INTO history_site_energy VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT(start_ms,resolution_ms) DO UPDATE SET first_ms=MIN(first_ms,excluded.first_ms),last_ms=MAX(last_ms,excluded.last_ms),covered_ms=covered_ms+excluded.covered_ms,n=n+excluded.n,
import_wh=import_wh+excluded.import_wh,export_wh=export_wh+excluded.export_wh,pv_wh=pv_wh+excluded.pv_wh,bat_charge_wh=bat_charge_wh+excluded.bat_charge_wh,bat_discharge_wh=bat_discharge_wh+excluded.bat_discharge_wh,load_wh=load_wh+excluded.load_wh,ev_wh=ev_wh+excluded.ev_wh`,
start, ArchiveResolutionMS, from, to, ms, 1, math.Max(0, p.GridW)*hours, math.Max(0, -p.GridW)*hours, -p.PVW*hours, math.Max(0, p.BatW)*hours, math.Max(0, -p.BatW)*hours, p.LoadW*hours, math.Max(0, p.GridW-p.BatW-p.PVW-p.LoadW)*hours)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Count each source interval only once

When a valid observation interval crosses one or more minute boundaries, this loop stores n=1 for every fragment rather than once for the original interval. aggregateDayEnergy later sums these values into DayEnergy.Intervals, whose contract is the number of integration intervals, and recentDailyLoadWh uses a threshold of 50 intervals as a data-quality gate; a handful of sparse 20-minute observations can therefore be reported as dozens of independent intervals and incorrectly influence the planner's recent-load floor.

Useful? React with 👍 / 👎.

@frahlg
frahlg merged commit b9d4ea0 into master Sep 17, 2026
16 checks passed
@frahlg
frahlg deleted the feat/dashboard-energy-aggregates-20260917 branch November 11, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

state: preserve energy and cost evidence before aggregating dashboard history

1 participant