perf(insights): consolidated BP sys+dia read and per-sub-query timings

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) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-17 17:22:15 +02:00
1 parent 6cbdad5f58
commit 0cc9f55a4c
2 files changed
+130 -80

No files matched your search

@@ -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 }]);
+110 -69
View File
@@ -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<ComprehensiveAggregate> {
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<string, number> = {};
// 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_<label>_ms`. The helper itself never throws — the
* builder's rejection propagates unchanged so the existing error
* handling stays intact.
*/
function timeSubquery<T>(
timings: Record<string, number>,
label: string,
builder: () => Promise<T>,
): Promise<T> {
const t0 = Date.now();
return builder().then((value) => {
timings[`insights.sub_${label}_ms`] = Date.now() - t0;
return value;
});
}
/**
* v1.4.38 W-F — fetch the trailing-90d sys + dia raw rows in a single
* `findMany` (`type: { in: [...] }`) rather than two parallel queries.
* Saves one round-trip + lets Postgres consolidate the index scan.
* Partitioning by type in JS preserves the byte-shape of the legacy
* sys / dia arrays (ASC by measuredAt — already enforced by the SQL
* ORDER BY).
*/
async function fetchBpRawRows(
userId: string,
ninetyDaysAgo: Date,
): Promise<{
sys: Array<{ measuredAt: Date; value: number }>;
dia: Array<{ measuredAt: Date; value: number }>;
}> {
const rows = await prisma.measurement.findMany({
where: {
userId,
type: { in: ["BLOOD_PRESSURE_SYS", "BLOOD_PRESSURE_DIA"] },
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { type: true, measuredAt: true, value: true },
});
const sys: Array<{ measuredAt: Date; value: number }> = [];
const dia: Array<{ measuredAt: Date; value: number }> = [];
for (const r of rows) {
if (r.type === "BLOOD_PRESSURE_SYS") {
sys.push({ measuredAt: r.measuredAt, value: r.value });
} else if (r.type === "BLOOD_PRESSURE_DIA") {
dia.push({ measuredAt: r.measuredAt, value: r.value });
}
}
return buildFromLiveAggregate(userId, ninetyDaysAgo);
return { sys, dia };
}
/**
@@ -267,9 +334,12 @@ export async function buildComprehensiveAggregate(
async function buildFromRollups(
userId: string,
ninetyDaysAgo: Date,
timings: Record<string, number>,
): Promise<ComprehensiveAggregate> {
const [narrows, latests, dayBuckets, sysRaw, diaRaw, firstRows] = await Promise.all([
prisma.$queryRaw<NarrowAggregateRow[]>`
// v1.4.38 W-F — bp sys + dia consolidated into one `findMany`
// (`type: { in: [...] }`) plus per-sub-query wall-clock timings.
const [narrows, latests, dayBuckets, bpRawRows, firstRows] = await Promise.all([
timeSubquery(timings, "narrow", () => prisma.$queryRaw<NarrowAggregateRow[]>`
WITH window_stats AS (
SELECT
m."type",
@@ -345,8 +415,8 @@ async function buildFromRollups(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
GROUP BY m."type", ws.stddev_value
`,
prisma.$queryRaw<LatestRow[]>`
`),
timeSubquery(timings, "latests", () => prisma.$queryRaw<LatestRow[]>`
SELECT DISTINCT ON (m."type")
m."type"::text AS type,
m."value"::double precision AS value,
@@ -355,45 +425,32 @@ async function buildFromRollups(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
ORDER BY m."type", m."measured_at" DESC
`,
`),
// v1.4.35 — DAY rollup buckets for the trailing 90 days, every
// type. Drives both the per-type `count / min / max / mean`
// composition (via `aggregateBuckets`) and the `dailyByType`
// correlation feed below. Replaces the legacy
// `date_trunc('day', m."measured_at")` GROUP BY query and the
// per-type AVG/MIN/MAX/COUNT columns the live aggregate kept.
prisma.measurementRollup.findMany({
timeSubquery(timings, "buckets", () => prisma.measurementRollup.findMany({
where: {
userId,
granularity: "DAY",
bucketStart: { gte: ninetyDaysAgo },
},
orderBy: [{ type: "asc" }, { bucketStart: "asc" }],
}),
prisma.measurement.findMany({
where: {
userId,
type: "BLOOD_PRESSURE_SYS",
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
prisma.measurement.findMany({
where: {
userId,
type: "BLOOD_PRESSURE_DIA",
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
prisma.$queryRaw<Array<{ first_at: Date | null }>>`
})),
// v1.4.38 W-F — bp sys + dia merged into one round-trip. Halves
// the bp-raw cost on accounts whose iOS Health source spams BP
// readings; the per-type partition in `fetchBpRawRows` preserves
// the legacy `bpRawRows.sys` / `bpRawRows.dia` byte-shape.
timeSubquery(timings, "bpRaw", () => fetchBpRawRows(userId, ninetyDaysAgo)),
timeSubquery(timings, "firstAt", () => prisma.$queryRaw<Array<{ first_at: Date | null }>>`
SELECT MIN(m."measured_at") AS first_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
`,
`),
]);
const latestByType = new Map<string, { value: number; measuredAt: Date }>();
@@ -447,10 +504,7 @@ async function buildFromRollups(
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
bpRawRows,
dailyByType: buildDailyByType(bucketsByType),
firstMeasurementAt,
totalMeasurements,
@@ -467,9 +521,13 @@ async function buildFromRollups(
async function buildFromLiveAggregate(
userId: string,
ninetyDaysAgo: Date,
timings: Record<string, number>,
): Promise<ComprehensiveAggregate> {
const [aggregates, latests, dayBuckets, sysRaw, diaRaw] = await Promise.all([
prisma.$queryRaw<HeavyAggregateRow[]>`
// v1.4.38 W-F — same sub-query timing + consolidated bp-raw read as
// the rollup-fresh path so the prod observability covers cold mounts
// too.
const [aggregates, latests, dayBuckets, bpRawRows] = await Promise.all([
timeSubquery(timings, "heavy", () => prisma.$queryRaw<HeavyAggregateRow[]>`
WITH window_stats AS (
SELECT
m."type",
@@ -545,8 +603,8 @@ async function buildFromLiveAggregate(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
GROUP BY m."type", ws.stddev_value
`,
prisma.$queryRaw<LatestRow[]>`
`),
timeSubquery(timings, "latests", () => prisma.$queryRaw<LatestRow[]>`
SELECT DISTINCT ON (m."type")
m."type"::text AS type,
m."value"::double precision AS value,
@@ -555,37 +613,21 @@ async function buildFromLiveAggregate(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
ORDER BY m."type", m."measured_at" DESC
`,
`),
// Buckets may exist for some types even when the COUNT probe came
// back zero (race window between the probe and a sibling
// populator); the cold-path read still honours them for the
// `dailyByType` feed.
prisma.measurementRollup.findMany({
timeSubquery(timings, "buckets", () => prisma.measurementRollup.findMany({
where: {
userId,
granularity: "DAY",
bucketStart: { gte: ninetyDaysAgo },
},
orderBy: [{ type: "asc" }, { bucketStart: "asc" }],
}),
prisma.measurement.findMany({
where: {
userId,
type: "BLOOD_PRESSURE_SYS",
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
prisma.measurement.findMany({
where: {
userId,
type: "BLOOD_PRESSURE_DIA",
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
})),
// v1.4.38 W-F — bp sys + dia in one round-trip (see `fetchBpRawRows`).
timeSubquery(timings, "bpRaw", () => fetchBpRawRows(userId, ninetyDaysAgo)),
]);
const latestByType = new Map<string, { value: number; measuredAt: Date }>();
@@ -624,12 +666,14 @@ async function buildFromLiveAggregate(
}
if (totalMeasurements > 0) {
const firstRows = await prisma.$queryRaw<Array<{ first_at: Date | null }>>`
SELECT MIN(m."measured_at") AS first_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
`;
const firstRows = await timeSubquery(timings, "firstAt", () =>
prisma.$queryRaw<Array<{ first_at: Date | null }>>`
SELECT MIN(m."measured_at") AS first_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
`,
);
firstMeasurementAt = firstRows[0]?.first_at
? new Date(firstRows[0].first_at)
: null;
@@ -637,10 +681,7 @@ async function buildFromLiveAggregate(
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
bpRawRows,
dailyByType: buildDailyByType(bucketsByType),
firstMeasurementAt,
totalMeasurements,