From 8806ce4e08eb44f31aa7e7c646bc6ffbc586b059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Wed, 3 Jun 2026 19:45:04 +0200 Subject: [PATCH] refactor(insights): name the latest-day-mean row cap on the derived readers The readiness + coincident-deviation latest-day-mean reads cap the row scan on a dense intra-day day. Lift the bare 50 into a named MAX_LATEST_DAY_ROWS constant and document the dense-intraday-retention reasoning at both call sites so the bound is intentional rather than a loose literal. --- src/lib/insights/derived/coincident-deviation.ts | 9 ++++++++- src/lib/insights/derived/readiness.ts | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/insights/derived/coincident-deviation.ts b/src/lib/insights/derived/coincident-deviation.ts index 518857dda..e347f975b 100644 --- a/src/lib/insights/derived/coincident-deviation.ts +++ b/src/lib/insights/derived/coincident-deviation.ts @@ -42,6 +42,13 @@ import type { Derived, DerivedProvenanceSource } from "./types"; const MS_PER_DAY = 24 * 60 * 60 * 1000; const DEFAULT_WINDOW_DAYS = 30; +/** + * Row cap for the latest-day mean read — see `readiness.ts`. A dense intra-day + * day can hold hundreds of rows; the latest-day mean only needs a bounded + * sample of the most-recent rows (the dense-intraday retention reasoning). The + * common single-reading-per-day case is unaffected. + */ +const MAX_LATEST_DAY_ROWS = 50; /** ≥ this many out-of-band vitals on a day fires the flag. */ export const COINCIDENT_FIRE_THRESHOLD = 2; /** Need ≥ this many banded vitals before the flag can even coincide. */ @@ -118,7 +125,7 @@ async function readLatestDayMean( const rows = await prisma.measurement.findMany({ where: { userId, type, deletedAt: null, measuredAt: { gte: since } }, orderBy: { measuredAt: "desc" }, - take: 50, + take: MAX_LATEST_DAY_ROWS, select: { value: true, measuredAt: true }, }); if (rows.length === 0) return null; diff --git a/src/lib/insights/derived/readiness.ts b/src/lib/insights/derived/readiness.ts index e1908a9c7..88ce49b7b 100644 --- a/src/lib/insights/derived/readiness.ts +++ b/src/lib/insights/derived/readiness.ts @@ -52,6 +52,15 @@ import type { Derived, DerivedProvenanceSource } from "./types"; const MS_PER_DAY = 24 * 60 * 60 * 1000; const DEFAULT_WINDOW_DAYS = 30; +/** + * Row cap for the latest-day mean read. A dense intra-day day (high-frequency + * RHR/HRV) can hold hundreds of rows; the deviation only needs the latest day's + * mean, so a bounded sample of the most-recent rows is enough — mirrors the + * dense-intraday retention reasoning (`measurements/dense-intraday-retention.ts`) + * that keeps the recent window dense but caps the read. The common single- + * reading-per-day case is unaffected. + */ +const MAX_LATEST_DAY_ROWS = 50; /** Minimum present components before a headline is produced (no 1-of-N). */ export const READINESS_MIN_COMPONENTS = 2; @@ -167,7 +176,7 @@ async function readLatestDayMean( const rows = await prisma.measurement.findMany({ where: { userId, type, deletedAt: null, measuredAt: { gte: since } }, orderBy: { measuredAt: "desc" }, - take: 50, + take: MAX_LATEST_DAY_ROWS, select: { value: true, measuredAt: true }, }); if (rows.length === 0) return null;