Files
HealthLog/src/lib/insights/__tests__
Marc-André BombeckandClaude Opus 4.7 0cc9f55a4c 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>
2026-05-17 17:22:15 +02:00
..