From 941947399ab4203e7baefe907f1f495b5594c3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Fri, 5 Jun 2026 13:57:21 +0200 Subject: [PATCH] perf(insights): bound the medication-intake reads to the rendered window general-status and blood-pressure-status read the full medication-intake history with no time floor, scanning every row for accounts with years of data. Both only consume the trailing ~365 days (the adherence fold and the continuity series / 7-30 day compliance gate), so add a scheduledFor floor at now - 365d. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/lib/insights/blood-pressure-status.ts | 6 ++++++ src/lib/insights/general-status.ts | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/insights/blood-pressure-status.ts b/src/lib/insights/blood-pressure-status.ts index 1eceae9ab..ad0c8bde0 100644 --- a/src/lib/insights/blood-pressure-status.ts +++ b/src/lib/insights/blood-pressure-status.ts @@ -276,6 +276,12 @@ export async function generateBloodPressureStatusForUser( userId, // v1.7.0 sync — exclude tombstoned rows. deletedAt: null, + // v1.12.7 — bound to the ~365-day window the continuity series + // and the 7/30-day compliance gate consume; this read was + // otherwise unbounded over all intake history. + scheduledFor: { + gte: new Date(now.getTime() - 365 * 24 * 60 * 60 * 1000), + }, medicationId: { in: bpMedications.map((medication) => medication.id), }, diff --git a/src/lib/insights/general-status.ts b/src/lib/insights/general-status.ts index 6f3a9b07f..703b776a2 100644 --- a/src/lib/insights/general-status.ts +++ b/src/lib/insights/general-status.ts @@ -178,9 +178,19 @@ export async function generateGeneralStatusForUser( }), ); + // v1.12.7 — bound the intake read to the ~365-day window the adherence + // series renders. Without a `scheduledFor` floor this scanned all intake + // history; the adherence fold below only consumes the trailing year. + const intakeWindowStart = new Date( + now.getTime() - 365 * 24 * 60 * 60 * 1000, + ); const intakeEvents = await prisma.medicationIntakeEvent.findMany({ // v1.7.0 sync — exclude tombstoned rows. - where: { userId, deletedAt: null }, + where: { + userId, + deletedAt: null, + scheduledFor: { gte: intakeWindowStart }, + }, orderBy: { scheduledFor: "asc" }, select: { scheduledFor: true,