From 0cc9f55a4ca9a968b54306b6de77cba546306b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sun, 17 May 2026 17:22:15 +0200 Subject: [PATCH] perf(insights): consolidated BP sys+dia read and per-sub-query timings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comprehensive aggregator's Promise.all dispatched two separate `prisma.measurement.findMany` calls for BLOOD_PRESSURE_SYS and BLOOD_PRESSURE_DIA over the trailing 90 days, even though both reads hit the same `(user_id, type, measured_at)` index path and feed the same `pairByTimestamp` consumer in the route. Merge them into a single `findMany` with `type: { in: [...] }` and partition by type in `fetchBpRawRows`. Saves one round-trip per request on both the rollup-fresh happy path and the cold fallback; the byte-shape of `bpRawRows.sys` / `bpRawRows.dia` is preserved because the SQL `ORDER BY measuredAt ASC` already produces the same within-type ordering the legacy two-query path emitted. Add per-sub-query wall-clock timings via a small `timeSubquery` helper. Six labels land on the wide-event meta: - `insights.sub_coverage_ms` — per-type rollup coverage probe - `insights.sub_narrow_ms` — windowed/regression aggregate - `insights.sub_heavy_ms` — cold-path full aggregate - `insights.sub_latests_ms` — DISTINCT ON latest per type - `insights.sub_buckets_ms` — DAY rollup buckets read - `insights.sub_bpRaw_ms` — bp sys+dia consolidated read - `insights.sub_firstAt_ms` — earliest measurement timestamp The next perf-verify can attribute any future regression to a single aggregate without re-instrumenting the route. The annotation is a no-op outside an `apiHandler` context so direct callers of the aggregator (tests, scripts) carry no overhead beyond a single `Date.now()` per slot. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../comprehensive-aggregator.test.ts | 31 +-- src/lib/insights/comprehensive-aggregator.ts | 179 +++++++++++------- 2 files changed, 130 insertions(+), 80 deletions(-) diff --git a/src/lib/insights/__tests__/comprehensive-aggregator.test.ts b/src/lib/insights/__tests__/comprehensive-aggregator.test.ts index e3d2ac15e..86d87dc4f 100644 --- a/src/lib/insights/__tests__/comprehensive-aggregator.test.ts +++ b/src/lib/insights/__tests__/comprehensive-aggregator.test.ts @@ -116,15 +116,18 @@ describe("buildComprehensiveAggregate", () => { }, ]); - FIND_MANY.mockResolvedValueOnce([]) // sys - .mockResolvedValueOnce([]); // dia + // v1.4.38 W-F — sys + dia merged into a single `findMany` + // (`type: { in: [...] }`) → one mock call carrying both rows + // (empty here since the rollup-fresh fixture doesn't exercise + // the BP pairing). The aggregator partitions by type in JS so + // the bpRawRows.sys / .dia byte-shape is preserved. + FIND_MANY.mockResolvedValueOnce([]); const result = await buildComprehensiveAggregate("user-rollup-fresh"); const weight = result.summaries.WEIGHT; // count/min/max/mean composed from buckets — the test asserts the // bucket-derived values are present even though we never wired up - // a heavy aggregate row for the parity check. expect(weight.count).toBe(42); expect(weight.min).toBe(79.2); expect(weight.max).toBe(84.1); @@ -154,7 +157,8 @@ describe("buildComprehensiveAggregate", () => { // count + by checking that the narrow projection (no min_value / // max_value / mean_value cols) is what landed. expect(RAW).toHaveBeenCalledTimes(4); - expect(FIND_MANY).toHaveBeenCalledTimes(2); + // v1.4.38 W-F — sys + dia merged into a single round-trip. + expect(FIND_MANY).toHaveBeenCalledTimes(1); expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1); }); }); @@ -168,8 +172,8 @@ describe("buildComprehensiveAggregate", () => { RAW.mockResolvedValueOnce([]) .mockResolvedValueOnce([]) .mockResolvedValueOnce([]); - FIND_MANY.mockResolvedValueOnce([]) // sys - .mockResolvedValueOnce([]); // dia + // v1.4.38 W-F — sys + dia merged into one `findMany`. + FIND_MANY.mockResolvedValueOnce([]); const result = await buildComprehensiveAggregate("user-empty"); @@ -180,7 +184,8 @@ describe("buildComprehensiveAggregate", () => { expect(result.firstMeasurementAt).toBeNull(); expect(result.totalMeasurements).toBe(0); expect(RAW).toHaveBeenCalledTimes(3); - expect(FIND_MANY).toHaveBeenCalledTimes(2); + // v1.4.38 W-F — sys + dia merged → single findMany. + expect(FIND_MANY).toHaveBeenCalledTimes(1); // The cold path's rollup.findMany still fires (in case some // buckets exist for a subset of types post-race), but returns []. expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1); @@ -219,8 +224,8 @@ describe("buildComprehensiveAggregate", () => { .mockResolvedValueOnce([ { first_at: new Date(now.getTime() - 86400000) }, ]); - FIND_MANY.mockResolvedValueOnce([]) // sys - .mockResolvedValueOnce([]); // dia + // v1.4.38 W-F — sys + dia merged into one round-trip. + FIND_MANY.mockResolvedValueOnce([]); const result = await buildComprehensiveAggregate("user-cold"); const weight = result.summaries.WEIGHT; @@ -293,9 +298,13 @@ describe("buildComprehensiveAggregate", () => { }, ]) .mockResolvedValueOnce([{ first_at: measuredAt }]); + // v1.4.38 W-F — single merged `findMany` returning both sys + dia + // rows tagged by `type`. The aggregator partitions in JS, so the + // bpRawRows.sys / .dia shape stays byte-identical. FIND_MANY.mockResolvedValueOnce([ - { measuredAt, value: 120 }, - ]).mockResolvedValueOnce([{ measuredAt, value: 80 }]); + { type: "BLOOD_PRESSURE_SYS", measuredAt, value: 120 }, + { type: "BLOOD_PRESSURE_DIA", measuredAt, value: 80 }, + ]); const result = await buildComprehensiveAggregate("user-bp"); expect(result.bpRawRows.sys).toEqual([{ measuredAt, value: 120 }]); diff --git a/src/lib/insights/comprehensive-aggregator.ts b/src/lib/insights/comprehensive-aggregator.ts index a9e6f07ff..b0123789f 100644 --- a/src/lib/insights/comprehensive-aggregator.ts +++ b/src/lib/insights/comprehensive-aggregator.ts @@ -99,6 +99,7 @@ */ import { prisma } from "@/lib/db"; import type { DataSummary } from "@/lib/analytics/trends"; +import { annotate } from "@/lib/logging/context"; import { ensureUserRollupsFresh } from "@/lib/measurements/rollups"; import { aggregateBuckets } from "@/lib/measurements/rollup-read"; import { @@ -233,6 +234,12 @@ export async function buildComprehensiveAggregate( ): Promise { const ninetyDaysAgo = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000); + // v1.4.38 W-F — per-sub-query wall-clock timing. Emitted under + // `meta.insights.sub_*_ms` so the next perf-verify can attribute + // a regression to a specific aggregate (narrow, latests, day-buckets, + // bp-raw, first-at, coverage-probe) without re-instrumenting. + const timings: Record = {}; + // v1.4.37.1 hotfix — fire-and-forget. See `src/app/api/analytics/route.ts` // for the full rationale: awaiting this on the read path can stall // the event loop for 30–60 s on power-user accounts whose iOS step @@ -252,11 +259,71 @@ export async function buildComprehensiveAggregate( // and only take the rollup path when EVERY type the user has logged // is covered. Partial coverage falls back to the live aggregate so // a fresh-type measurement doesn't break its summary card. + const probeStart = Date.now(); const coverage = await probeRollupCoverage(userId); - if (isFullyCovered(coverage)) { - return buildFromRollups(userId, ninetyDaysAgo); + timings["insights.sub_coverage_ms"] = Date.now() - probeStart; + + const result = isFullyCovered(coverage) + ? await buildFromRollups(userId, ninetyDaysAgo, timings) + : await buildFromLiveAggregate(userId, ninetyDaysAgo, timings); + + annotate({ meta: timings }); + return result; +} + +/** + * v1.4.38 W-F — tiny timing helper. Wraps a promise-returning sub-query + * builder so each Promise.all slot lands a wall-clock observation under + * `meta.insights.sub_