Skip to content

fix(api): cap history and series points at 2000 - #2055

Merged
frahlg merged 1 commit into
masterfrom
1136-history-points-cap
Nov 21, 2026
Merged

frahlg merged 1 commit into
masterfrom
1136-history-points-cap

Conversation

@frahlg

@frahlg frahlg commented Nov 21, 2026

Copy link
Copy Markdown
Member

For #1136. Fredrik asked for this in today's Core beta (21 November 2026).

Problem

GET /api/history and GET /api/series took any points value:

  • handleHistory used any n > 0 and quietly fell back to 200 for anything else, including abc and -5.
  • handleSeries used any value that parsed, negatives included, and read abc as 0. A negative or 0 means "every stored row".

Since the issue was filed, the store has gained its own guards (maxSeriesBuckets = 10000 and maxRawSeriesPoints = 200000 in go/internal/state/series_archive.go). So memory is bounded, but:

  • ?points=1000000000 reaches the store, which refuses it, and the caller gets a 500.
  • Values up to 10000 are still served. That is five times the 2000-bucket budget /api/energy/history uses (energyHistoryMaxBuckets). /api/history is a public read; it is not in protectedReadPath.
  • A mistyped or negative points on /api/series turns a bucketed read into a full stored-rows read.

Change

Clamp, not refuse, for large values. A whole number above 2000 is cut to 2000, so an older client or agent that asks for more still gets an answer, at a coarser step. No client in Core's web UI asks for more than 800 today (dashboard 360 and 288, heating 120–800, car SoC 180). The native apps do not call these routes.

Refuse values that are not numbers. A value that is not a whole number, is below 0, or overflows int now gets a 400 (points must be a whole number, 0 or more), as the issue asks and as since is handled.

Defaults are unchanged:

  • /api/history: omitted or points=0 → 200 rows, the dashboard default.
  • /api/series: omitted or points=0 → stored rows. The store's 200000-row guard still applies, as before.

Files:

  • go/internal/api/api.go: new historyMaxPoints (= energyHistoryMaxBuckets, 2000) and parseHistoryPoints, used by handleHistory and handleSeries. The doc comments name the cap.
  • go/internal/api/api_agent_catalog.go: the agent route hints say "up to 2000" for points on both routes.
  • go/internal/api/api_history_points_test.go (new).
  • .changeset/history-points-cap.md: "ftw": patch.

Scope and safety

This changes the API handlers only. The store and store_ts.go stay as they are (#1129, which the issue mentions, has merged). A 400 replaces what was a silent fallback, but only for values no Core client sends.

Overlap: open PRs #2041, #2016 and #1789 also edit go/internal/api/api.go, in other hunks (imports, Deps, New, route table, handleSetPeakLimit). #2041 also edits api_agent_catalog.go near line 28; this change is at lines 37, 43 and 112. Neither touches handleHistory or handleSeries.

Not in scope: /api/series takes a comma-separated metric list, and each metric is read with its own points budget, so a long or repeated list multiplies the reply. /api/series is a protected read (protectedReadPath). A cap on the list would be a separate change.

Evidence

New tests in go/internal/api/api_history_points_test.go. Each stores 3000 history rows and 3000 meter_w samples one second apart:

  • TestHistoryAndSeriesClampLargePoints: points=5000 and points=1000000000 give 200 with 1..2000 rows on both routes.
  • TestHistoryAndSeriesRefuseBadPoints: -1, -5000, abc, 1.5 and 99999999999999999999 give 400 on both routes.
  • TestHistoryPointsZeroAndOmittedKeepDashboardDefault: history with points omitted or 0 gives at most 200 rows; points=360 gives more than 200 and at most 360.
  • TestSeriesPointsZeroAndOmittedReturnStoredRows: series with points omitted or 0 gives all 3000 stored rows.

Results:

  • Before the fix (master's api.go, with a one-line test shim for the constant): the clamp test fails (points=5000: 3000 rows, want 1..2000; points=1000000000: status 500) and the refuse test fails (all ten cases give 200). The two default tests pass; they hold existing behaviour.
  • go test ./internal/api/ -count=1: ok (61 s).
  • make verify, first run: failed only in internal/ocpp TestLaterRunsAfterTheSlotIsFree, the known flake Flaky test: ocpp TestLaterRunsAfterTheSlotIsFree under -race #1943. This PR does not touch internal/ocpp, and that test passed 20 of 20 runs alone.
  • make verify, second run: verify: vet + test + build clean.

Checklist

  • The change follows VISION.md and one selected scope.
  • I checked overlapping PRs and coordinated shared files/contracts.
  • Relevant checks cover the changed behaviour and failure paths.
  • A human reviewed changed UI in a browser, or no UI changed. (No UI changed.)
  • A Changeset is included, or the change is exempt.
  • Every commit has a DCO sign-off.

🤖 Generated with Claude Code

GET /api/history and GET /api/series took any points value. A huge one
reached the store, which refused it, so the caller got a 500; values up
to the store's 10000-bucket limit built replies five times the size of
the energy history's 2000-bucket budget. /api/series also read a
negative or non-numeric value as 0, which returns every stored row, and
/api/history quietly used 200.

Both routes now share parseHistoryPoints: omitted keeps each route's
default (200 rows for history, stored rows for series), a value above
2000 is clamped to 2000 so older clients still get an answer, and a
value that is not a whole number, or is below 0, gets a 400. points=0
keeps its meaning on both routes. The agent route hints name the cap.

For #1136.

Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T11:34:53.447101Z c3200bc 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.

@frahlg
frahlg merged commit 063d824 into master Nov 21, 2026
21 checks passed
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.

1 participant