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.
This commit is contained in:
Marc-André Bombeck committed 2026-06-03 19:48:48 +02:00
1 parent 3ca444d23f
commit 8806ce4e08
2 files changed
+18 -2

No files matched your search

@@ -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;
+10 -1
View File
@@ -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;