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) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.8 committed 2026-06-05 13:58:28 +02:00
1 parent 005f4ba2be
commit 941947399a
2 files changed
+17 -1

No files matched your search

@@ -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),
},
+11 -1
View File
@@ -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,