Repository navigation
feat(state): store bounded scalar history aggregates - #1290
Conversation
Signed-off-by: Fredrik Ahlgren <[email protected]>
Signed-off-by: Fredrik Ahlgren <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 737c77cb48
ℹ️ 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".
| out[i] = SeriesPoint{TsMs: sm.TsMs, V: sm.Value, Min: sm.Value, Max: sm.Value, N: 1} | ||
| } | ||
| return out, nil | ||
| return s.mergedSeries(ctx, s.coldDir, driver, metric, sinceMs, untilMs, 0) |
There was a problem hiding this comment.
Expose aggregation metadata in CSV exports
When GET /api/series?...&points=0&format=csv spans data older than 24 hours, this path now returns one-minute or five-minute summaries rather than raw samples. Although JSON exposes resolution_ms, first_ms, and last, the CSV branch in go/internal/api/api.go still writes only ts_ms,driver,metric,v,min,max,n and documents points=0 as raw data, so spreadsheet and ML consumers silently interpret an aggregate mean at the bucket's last timestamp as an actual measurement. Add the aggregation metadata to CSV or prevent this raw-export mode from returning summaries.
Useful? React with 👍 / 👎.
| SELECT ts_ms,value FROM ( | ||
| SELECT s.ts_ms,s.value FROM ts_samples s JOIN identity i USING(driver_id,metric_id) | ||
| UNION ALL SELECT b.last_ms,b.last_value FROM ts_buckets b JOIN identity i USING(driver_id,metric_id) WHERE b.resolution_ms=10000 | ||
| ) ORDER BY ts_ms DESC LIMIT 1`, driver, metric).Scan(&sm.TsMs, &sm.Value) |
There was a problem hiding this comment.
Retain latest readings after hot buckets are archived
For a metric that stops reporting for more than 24 hours, maintenance publishes its minute summary and deletes both the minute row and its 10-second source buckets, while aggregate-mode writes never enter ts_samples. This query searches only ts_samples and 10-second buckets, so LatestSample then returns sql.ErrNoRows even though retained Parquet history still contains the last observation; for example, InferDerKinds consequently forgets a device kind that it documents as having ever emitted telemetry. Persist or query a durable latest observation rather than limiting this lookup to the hot tier.
Useful? React with 👍 / 👎.
Signed-off-by: Fredrik Ahlgren <[email protected]>
Signed-off-by: Fredrik Ahlgren <[email protected]>
Core currently writes every scalar poll and later copies those same rows to Parquet. This grows the live database and makes archiving and backup compete with useful EMS work. This change stores scalar measurements at 10-second resolution for 24 hours, one-minute Parquet for 30 days and five-minute Parquet for two years. It preserves original energy observations and the current driver/control cadence.
Each summary keeps count, sum, min/max, last value and observed time bounds. Reads report source resolution and weight means by count. Timestamp identities and existing commit receipts prevent duplicate contributions across batches, uncertain commits and restarts. Latest-value reads still return an actual reading. Missing intervals stay missing; bounds do not assert continuous coverage.
Archives publish only after a full readback matches their staged contents. SQLite wins during publication/prune overlap; pruning removes only matching complete minutes. Admission cannot overtake a pending tick at the archive boundary. Old Parquet conversion saves its source cursor with each staging transaction and independently checks counts, sums, extrema and time bounds before removing raw data. Separate aggregate hour contributions survive legacy hour rebuilding. Backups include all summary tables and files, omit mutable scratch, and preserve empty BLOBs during copying.
Schema 5 protects the new storage format from image-only rollback. A rollback needs a verified full backup and its matching Core. Stored
cold_retention_daysvalues no longer steer this policy; startup reports them. Hourly gauge summaries and existing energy totals remain after detailed history expires.This is the scalar part of #1287. Dashboard rows and the older daily-energy/cost readers stay on their current path: those consumers require a separate original-interval integration change before their rows can be aggregated safely. Live-backup latency #1246 and control-loop IO isolation #1247 remain separate gates. This PR does not deploy or release a beta.
Validation: local
make verify; state and backup packages; aggregate race tests repeated three times. Regressions cover irregular timestamps, short spikes, duplicates, out-of-order data, restart mid-bucket, pending admission at cutoff, partial-day archives, source changes, corrupt files, interrupted prune, resumed legacy conversion, mixed old/new hourly evidence, unchanged energy-ledger output and full backup/verify/restore with duplicate identities preserved.