perf(insights): batch the medication-compliance and per-type latest queries (#125)

Two N+1 patterns the v1.4 discovery audit caught — the same shape v1.3
fixed in /api/insights/comprehensive but in adjacent code paths:

1. **`extractFeatures` (used by every AI-insight route)** issued one
   `prisma.medicationIntakeEvent.findMany` per active medication,
   in-loop. Replaced with a single windowed query
   (`medicationId IN (...)` + `scheduledFor >= 90 days`) and an
   in-memory Map group, mirroring the V3 fix in `/api/insights/
   comprehensive`. Latency now scales with total recent intakes, not
   `medications × intakes-per-medication`. The 90-day cutoff matches
   the longest window `calculateCompliance` consults.

2. **`/api/insights/targets`** issued one `prisma.measurement.findFirst`
   per `MeasurementType` (×~10) to fetch each type's most recent
   value. Replaced with a single `findMany({ distinct: ["type"],
   orderBy: { measuredAt: "desc" } })`. One round-trip instead of N.

Behaviour identical, just fewer round-trips. Tests stay at 371/371.

Note: the eager Recharts import in `app/insights/page.tsx` (the audit
also flagged it as a bundle bloat that neutralises the existing
`dynamic()` wrappers) is its own refactor — every ScatterChart usage
inside the page needs to be lifted into its own component before
dynamic-loading helps. Tracked for a follow-up perf PR so this one
stays scoped to query-side wins.

Co-authored-by: Marc-André Bombeck <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandMarc-André Bombeck authored and GitHub committed 2026-05-08 12:11:57 +02:00
1 parent a6524721da
commit f9ff450e08
2 files changed
+136 -47

No files matched your search

+12 -8
View File
@@ -109,20 +109,24 @@ export const GET = apiHandler(async () => {
select: { type: true, value: true, measuredAt: true },
});
// Also get the absolute latest measurement per type (even if older than 30 days)
// Also get the absolute latest measurement per type (even if older than 30 days).
// Single grouped query replaces the previous N×findFirst loop — DISTINCT ON
// (Postgres) emulated via a windowed orderBy + take-1 per type would still be
// N round-trips, so we sort once on the wide query and pick per-type below.
const latestEverByType = await prisma.measurement.findMany({
where: { userId, type: { in: types } },
orderBy: { measuredAt: "desc" },
distinct: ["type"],
select: { type: true, value: true },
});
const latestByType: Record<string, number | null> = {};
const avg30ByType: Record<string, number | null> = {};
for (const t of types) {
// Latest value
const latest = await prisma.measurement.findFirst({
where: { userId, type: t },
orderBy: { measuredAt: "desc" },
select: { value: true },
});
const latest = latestEverByType.find((row) => row.type === t);
latestByType[t] = latest?.value ?? null;
// 30-day average
const recentOfType = recentMeasurements.filter((m) => m.type === t);
if (recentOfType.length > 0) {
const sum = recentOfType.reduce((acc, m) => acc + m.value, 0);