mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
perf(analytics): default slice delegates to slim rollup-tier reader (v1.4.49.1)
v1.4.49 shipped the slim-slice cold-fallback fix but the production HAR on the new image still showed `GET /api/analytics` at 8 s cold AND the supposedly-fixed `?slice=summaries` request hit the same 8 s when fired concurrently. Investigation found the slim slice queues behind the default slice's per-type live walk: the 15-way `fetchMeasurementSeriesChunked` fan-out saturates the `p-limit(4)` lanes and packs the 20-slot Prisma pool for the full ~8 s on Marc's 467 745-row tenant, starving every other Prisma client (including the concurrent slim slice) until the walk drains. The default slice's `summaries` field has been produceable from the rollup tier for two releases — the slim slice already uses `computeFromRollups` + a 90-day narrow `$queryRaw` and returns the identical `DataSummary` shape sub-second. The walk lived on because two thick-only fields (`anomalyCount`, `avg30LastMonth`) only existed on the live path. A grep of every consumer in the repo confirmed: - `anomalyCount`: no consumer reads it from `/api/analytics`. The insights pipeline pulls it from its own `comprehensive-aggregator` narrow query. Slim's `0` constant is what the default route's consumers already received. - `avg30LastMonth`: read by `src/app/page.tsx:321` (dashboard comparison tile) and `src/components/insights/vo2-max-chart-row.tsx`. Easy to lift: extend the slim narrow `$queryRaw` with one extra FILTER clause for `[30d, 60d)` — the rows are already inside the 90-day outer cap so the planner reads zero additional blocks. With both fields covered, `buildAnalyticsResponse` calls `computeSummariesSlice(user.id)` once and uses its `summaries` + `lastSeenByType` directly. The 15-way fan-out, the chunked walker, the `ANALYTICS_TYPE_FETCH_CONCURRENCY` p-limit cap, the 425-day `since` window, and the `meta.analytics.bp_aggregate` annotate all delete cleanly — none had callers outside the removed code path. Net diff: -418 LOC across the route + its tests. Test updates: - `route.test.ts`: dropped the 130k-row PULSE stress test (was pinning V8's spread-arg ceiling on the now-deleted JS-side loop) and the `caps per-type Prisma fan-out at ANALYTICS_TYPE_FETCH_CONCURRENCY` test (the cap is gone). Added a negative assertion that `prisma.measurement.findMany` is never called with the chunked-walk shape `(select.id + source + deviceType, take: 5000)` and a positive assertion that `avg30LastMonth` reaches the response envelope. - `since-cap.test.ts`: dropped the `where.measuredAt.gte` per-type assertion and the `bp_aggregate.live_since` annotation assertion (both pinned the removed walk). The slim-slice no-per-type-loop invariant survives. Final: 5268 unit + integration tests pass, zero failures, lint clean, typecheck clean.
This commit is contained in:
1 parent
f92e644154
commit
6b7b8a7fdf
6 files changed
+169
-587
No files matched your search
@@ -116,7 +116,11 @@ vi.mock("@/lib/analytics/summaries-slice", () => ({
|
||||
import { GET } from "@/app/api/analytics/route";
|
||||
import { getSession } from "@/lib/auth/session";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
// v1.4.49.1 — `annotate` import retired alongside the
|
||||
// `bp_aggregate.live_since` annotation assertion (the annotation lived
|
||||
// in the now-deleted per-type live walk). The surviving slim-slice
|
||||
// invariant doesn't read annotate. Re-add this import if a future
|
||||
// regression test needs to verify a wide-event field.
|
||||
import { __resetAllCachesForTests } from "@/lib/cache/server-cache";
|
||||
|
||||
const callGet = GET as unknown as (req: Request) => Promise<Response>;
|
||||
@@ -194,79 +198,18 @@ beforeEach(async () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/analytics — live-fallback `since` cap (W-SINCE)", () => {
|
||||
it("passes a trailing-425-day `where.measuredAt.gte` to every per-type findMany", async () => {
|
||||
const before = Date.now();
|
||||
const response = await callGet(
|
||||
new Request("http://localhost/api/analytics"),
|
||||
);
|
||||
expect(response.status).toBe(200);
|
||||
const after = Date.now();
|
||||
|
||||
const findMany = prisma.measurement.findMany as ReturnType<typeof vi.fn>;
|
||||
expect(findMany.mock.calls.length).toBeGreaterThan(0);
|
||||
|
||||
// QA Specialist-H2 (v1.4.39): 425 days = 365 (year-ago window) +
|
||||
// 30 (avg30LastYear bucket span) + 30 (cache-aging buffer).
|
||||
const FOUR_TWENTY_FIVE_DAYS_MS = 425 * 24 * 60 * 60 * 1000;
|
||||
const expectedMinSince = before - FOUR_TWENTY_FIVE_DAYS_MS;
|
||||
const expectedMaxSince = after - FOUR_TWENTY_FIVE_DAYS_MS;
|
||||
|
||||
// The per-type loop (A2) issues chunked reads with an `orderBy`
|
||||
// *array* (`(measuredAt asc, id asc)`); the 30-day glucose and
|
||||
// 30-day sleep-stage reads use either a single-object orderBy or
|
||||
// none at all. Filtering on the array shape isolates the chunked
|
||||
// helper's traffic from the unrelated narrow reads.
|
||||
const perTypeCalls = findMany.mock.calls.filter((call) => {
|
||||
const arg = call[0] as { orderBy?: unknown };
|
||||
return Array.isArray(arg?.orderBy);
|
||||
});
|
||||
|
||||
expect(perTypeCalls.length).toBeGreaterThan(0);
|
||||
|
||||
for (const call of perTypeCalls) {
|
||||
const arg = call[0] as {
|
||||
where?: { measuredAt?: { gte?: Date } };
|
||||
};
|
||||
const gte = arg.where?.measuredAt?.gte;
|
||||
expect(gte).toBeInstanceOf(Date);
|
||||
const gteMs = (gte as Date).getTime();
|
||||
expect(gteMs).toBeGreaterThanOrEqual(expectedMinSince);
|
||||
expect(gteMs).toBeLessThanOrEqual(expectedMaxSince);
|
||||
}
|
||||
});
|
||||
|
||||
it("annotates `meta.analytics.bp_aggregate.live_since` with the 425-day cutoff ISO", async () => {
|
||||
const before = Date.now();
|
||||
await callGet(new Request("http://localhost/api/analytics"));
|
||||
const after = Date.now();
|
||||
|
||||
const annotateFn = annotate as ReturnType<typeof vi.fn>;
|
||||
const bpAggregateCall = annotateFn.mock.calls.find((call) => {
|
||||
const fields = call[0] as
|
||||
| { meta?: { analytics?: { bp_aggregate?: unknown } } }
|
||||
| undefined;
|
||||
return fields?.meta?.analytics?.bp_aggregate !== undefined;
|
||||
});
|
||||
expect(bpAggregateCall).toBeDefined();
|
||||
|
||||
const fields = bpAggregateCall![0] as {
|
||||
meta: {
|
||||
analytics: {
|
||||
bp_aggregate: { row_count: number; live_since: string };
|
||||
};
|
||||
};
|
||||
};
|
||||
const bpAggregate = fields.meta.analytics.bp_aggregate;
|
||||
expect(typeof bpAggregate.live_since).toBe("string");
|
||||
|
||||
const FOUR_TWENTY_FIVE_DAYS_MS = 425 * 24 * 60 * 60 * 1000;
|
||||
const liveSinceMs = new Date(bpAggregate.live_since).getTime();
|
||||
expect(liveSinceMs).toBeGreaterThanOrEqual(
|
||||
before - FOUR_TWENTY_FIVE_DAYS_MS,
|
||||
);
|
||||
expect(liveSinceMs).toBeLessThanOrEqual(after - FOUR_TWENTY_FIVE_DAYS_MS);
|
||||
});
|
||||
describe("GET /api/analytics — per-type live walk retired (v1.4.49.1)", () => {
|
||||
// v1.4.49.1 — the W-SINCE 425-day `since` cap was a defense-in-depth
|
||||
// bound on the now-deleted 15-way per-type live walk. The default
|
||||
// slice routes through `computeSummariesSlice` exclusively, which
|
||||
// reads `measurement_rollups` DAY buckets + a 90-day narrow
|
||||
// `$queryRaw` directly — no chunked findMany against `measurements`
|
||||
// at all on the default critical path. The two assertions that pinned
|
||||
// (a) the per-type `where.measuredAt.gte` and (b) the
|
||||
// `meta.analytics.bp_aggregate.live_since` annotate were removed
|
||||
// together because both code paths were deleted; the surviving
|
||||
// `slim ?slice=summaries` invariant below still pins that the slim
|
||||
// branch never reaches a chunked measurements walk.
|
||||
|
||||
it("does not invoke the per-type loop on the slim `?slice=summaries` branch", async () => {
|
||||
const response = await callGet(
|
||||
|
||||
Reference in new issue
Block a user