mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
fix(api): stop /api/analytics 500 from Math.min/max spread overflow
Production GET /api/analytics returned HTTP 500 for users with very long measurement histories (request-id d86db8e5… 2026-05-16 14:39:51 UTC). The wide-event log pinned the failure at Promise.all index 3 (PULSE) with RangeError: Maximum call stack size exceeded — a power user's Apple-Health heart-rate stream crossed V8's ~125k function-arity ceiling on the Math.min(...values) / Math.max(...values) spread inside summarize(). Fold sum, min, and max into a single pass over values so the transient argument array goes away and the working set stays bounded at the array itself. Mean is now derived from the same fold's sum/length instead of a second reduce. No public API change; min/max semantics for an empty series remain null via the early-exit branch. Adds a 250 000-row regression in trends.test.ts that raises the exact production stacktrace before the fix and passes after, plus a new src/app/api/analytics/__tests__/route.test.ts that pins the route-entry contract (5 000-row PULSE + zero-row brand-new user) so the upcoming C1 architectural rewrite can't silently reintroduce a spread anywhere along the call chain. Touched-file lint and pnpm typecheck clean; broader src/lib + src/app/api/analytics sweep is 2671 / 1 skipped.
This commit is contained in:
1 parent
f9346e62a2
commit
61107e0c68
4 files changed
+347
-3
No files matched your search
@@ -131,4 +131,36 @@ describe("summarize", () => {
|
||||
expect(summary.slope7).toBeNull(); // window snaps to `now`, no points
|
||||
expect(summary.slope30).toBeNull(); // same
|
||||
});
|
||||
|
||||
// v1.4.33 P0 regression — production stacktrace
|
||||
// `RangeError: Maximum call stack size exceeded` at index 3 of the
|
||||
// analytics route's per-type `Promise.all` (PULSE).
|
||||
//
|
||||
// Root cause: `Math.min(...values)` / `Math.max(...values)` spread the
|
||||
// whole series as function arguments. V8 caps function arity at
|
||||
// ~125 000-130 000; an Apple-Health-synced PULSE series easily
|
||||
// exceeds that for a multi-year power user. The fix folds min/max via
|
||||
// a single pass instead, keeping the working set bounded.
|
||||
it("survives a multi-hundred-thousand-row series without blowing the stack", () => {
|
||||
// 250 000 points — well above V8's spread-arg ceiling. The
|
||||
// unrolled values give us deterministic min/max so we can assert
|
||||
// both the safety contract AND the correctness contract in one go.
|
||||
const N = 250_000;
|
||||
const now = Date.now();
|
||||
const data: DataPoint[] = new Array(N);
|
||||
for (let i = 0; i < N; i++) {
|
||||
data[i] = {
|
||||
date: new Date(now - (N - i) * 1000),
|
||||
// Wave between 40 and 199; min=40, max=199.
|
||||
value: 40 + (i % 160),
|
||||
};
|
||||
}
|
||||
// Must not throw — before the fix this raised
|
||||
// `RangeError: Maximum call stack size exceeded`.
|
||||
const summary = summarize(data);
|
||||
expect(summary.count).toBe(N);
|
||||
expect(summary.min).toBe(40);
|
||||
expect(summary.max).toBe(199);
|
||||
expect(summary.latest).toBe(data[N - 1].value);
|
||||
});
|
||||
});
|
||||
@@ -192,7 +192,24 @@ export function summarize(data: DataPoint[]): DataSummary {
|
||||
|
||||
const sorted = [...data].sort((a, b) => a.date.getTime() - b.date.getTime());
|
||||
const values = sorted.map((p) => p.value);
|
||||
const mean = values.reduce((s, v) => s + v, 0) / values.length;
|
||||
// v1.4.33 P0 — single-pass fold for sum/min/max replaces the previous
|
||||
// `Math.min(...values)` / `Math.max(...values)` spread. V8 caps
|
||||
// function arity at ~125k-130k; Apple-Health-synced PULSE series for
|
||||
// a multi-year power user routinely exceed that, and the spread
|
||||
// surfaced as `RangeError: Maximum call stack size exceeded` from the
|
||||
// `/api/analytics` route's per-type `Promise.all` aggregator. Folding
|
||||
// the three reductions into one walk keeps the working set bounded
|
||||
// (no transient argument array) and is also cheaper than three
|
||||
// separate passes.
|
||||
let sum = 0;
|
||||
let minVal = values[0];
|
||||
let maxVal = values[0];
|
||||
for (const v of values) {
|
||||
sum += v;
|
||||
if (v < minVal) minVal = v;
|
||||
if (v > maxVal) maxVal = v;
|
||||
}
|
||||
const mean = sum / values.length;
|
||||
|
||||
const now = Date.now();
|
||||
const DAY = 24 * 60 * 60 * 1000;
|
||||
@@ -222,8 +239,8 @@ export function summarize(data: DataPoint[]): DataSummary {
|
||||
return {
|
||||
count: data.length,
|
||||
latest: sorted[sorted.length - 1].value,
|
||||
min: Math.min(...values),
|
||||
max: Math.max(...values),
|
||||
min: minVal,
|
||||
max: maxVal,
|
||||
mean: Math.round(mean * 100) / 100,
|
||||
avg7:
|
||||
last7.length > 0
|
||||
|
||||
Reference in new issue
Block a user