fix(analytics): collapse overlapping sources on the cold BP path

The raw BP pulls in the comprehensive aggregator and the bp-in-target live
fallback read every source's row for BLOOD_PRESSURE_SYS/DIA. BP is a
multi-source metric — a Withings reading mirrored into Apple Health is two
rows for one event — so each shared reading was counted twice in the
in-target denominator and twice in the recency-weighted grade. The rollup
branch of the same fast-path reads source-collapsed measurement_rollups, so a
rollup-warm account and a rollup-cold account returned different BP
percentages and grades for identical data — a web/iOS and cold/warm parity
break.

Route both raw pulls through canonicalMeasurementsFrom so the cold/live path
picks the ladder-winning source per (type, day), matching the warm/rollup
path. Per-event rows of the winning source survive the collapse, so the
downstream 5-minute pairing still sees individual readings.
This commit is contained in:
Marc-André Bombeck committed 2026-06-20 16:52:00 +02:00
1 parent fb4e3921e2
commit 013241ba74
4 files changed
+215 -92

No files matched your search

@@ -15,6 +15,10 @@ vi.mock("@/lib/db", () => ({
// v1.11.1 — readRollupBuckets lazy-loads the source-priority blob; null
// (default findUnique) falls back to the default ladders.
user: { findUnique: vi.fn() },
// v1.18.10 I-1 — the live BP fallback now reads source-collapsed rows via
// `$queryRawUnsafe` (`canonicalMeasurementsFrom`) instead of a chunked
// `findMany`, so cold == warm. Mock it to feed the SYS / DIA series.
$queryRawUnsafe: vi.fn(),
},
}));
@@ -39,6 +43,7 @@ const MEASUREMENT_FIND_MANY = prisma.measurement
.findMany as unknown as ReturnType<typeof vi.fn>;
const ROLLUP_FIND_MANY = prisma.measurementRollup
.findMany as unknown as ReturnType<typeof vi.fn>;
const QUERY_RAW = prisma.$queryRawUnsafe as unknown as ReturnType<typeof vi.fn>;
const PROBE = probeRollupCoverage as unknown as ReturnType<typeof vi.fn>;
const FULLY_COVERED = isFullyCovered as unknown as ReturnType<typeof vi.fn>;
const ANNOTATE = annotate as unknown as ReturnType<typeof vi.fn>;
@@ -53,6 +58,7 @@ const TARGETS_UNDER_65 = {
beforeEach(() => {
MEASUREMENT_FIND_MANY.mockReset();
ROLLUP_FIND_MANY.mockReset();
QUERY_RAW.mockReset();
PROBE.mockReset();
FULLY_COVERED.mockReset();
ANNOTATE.mockReset();
@@ -215,7 +221,7 @@ describe("computeBpInTargetFastPath", () => {
});
describe("live fallback — partial or no coverage", () => {
it("falls back to the chunked findMany when SYS bucket coverage is missing", async () => {
it("falls back to the source-collapsed live read when SYS bucket coverage is missing", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", false],
["BLOOD_PRESSURE_DIA", true],
@@ -223,17 +229,16 @@ describe("computeBpInTargetFastPath", () => {
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
// First findMany = SYS rows, second = DIA rows.
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([
// First raw query = SYS rows, second = DIA rows. Rows arrive in the raw
// SQL snake_case shape (`measured_at` + `value`).
QUERY_RAW.mockResolvedValueOnce([
{
id: "1",
measuredAt: new Date("2026-05-15T08:00:00.000Z"),
measured_at: new Date("2026-05-15T08:00:00.000Z"),
value: 122,
},
]).mockResolvedValueOnce([
{
id: "2",
measuredAt: new Date("2026-05-15T08:00:00.000Z"),
measured_at: new Date("2026-05-15T08:00:00.000Z"),
value: 76,
},
]);
@@ -249,7 +254,8 @@ describe("computeBpInTargetFastPath", () => {
// One paired in-target reading.
expect(result.last30Days).toEqual({ pct: 100, pairs: 1 });
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(2);
expect(MEASUREMENT_FIND_MANY).not.toHaveBeenCalled();
expect(QUERY_RAW).toHaveBeenCalledTimes(2);
});
it("pins the path:live annotate", async () => {
@@ -259,7 +265,7 @@ describe("computeBpInTargetFastPath", () => {
]);
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
QUERY_RAW.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
await computeBpInTargetFastPath({
userId: "user-partial",
@@ -275,11 +281,51 @@ describe("computeBpInTargetFastPath", () => {
expect(bpCall).toBeDefined();
});
it("collapses overlapping sources on the live path (v1.18.10 I-1 — cold == warm)", async () => {
// Two sources mirror the SAME reading on the same day (Withings + its
// Apple-Health mirror). The source-collapse picks one canonical source
// per (type, day) BEFORE pairing, so the in-target denominator counts the
// shared reading once — matching the rollup path. The collapse runs in
// SQL (`canonicalMeasurementsFrom`), so the mock just confirms the live
// query routes through it and the result reflects ONE pair, not two.
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", false],
["BLOOD_PRESSURE_DIA", false],
]);
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
// The canonical subquery has already collapsed the duplicate source — it
// returns ONE SYS row and ONE DIA row for the day.
QUERY_RAW.mockResolvedValueOnce([
{ measured_at: new Date("2026-05-15T08:00:00.000Z"), value: 122 },
]).mockResolvedValueOnce([
{ measured_at: new Date("2026-05-15T08:00:00.000Z"), value: 76 },
]);
const result = await computeBpInTargetFastPath({
userId: "user-dual-source",
targets: TARGETS_UNDER_65,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
expect(result.path).toBe("live");
expect(result.last30Days).toEqual({ pct: 100, pairs: 1 });
// Both live reads must route through the canonical-source subquery so the
// collapse fires in SQL — pin the marker so a regression that reverts to
// a raw `findMany` (double-counting) fails here.
for (const call of QUERY_RAW.mock.calls) {
const sql = String(call[0]);
expect(sql).toContain("DISTINCT ON");
expect(sql).toContain("date_trunc('day'");
}
});
it("probes coverage when the caller omits the map", async () => {
const coverage = new Map<string, boolean>([]);
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
QUERY_RAW.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-fresh",
@@ -331,7 +377,7 @@ describe("computeBpInTargetFastPath", () => {
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
QUERY_RAW.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-honolulu",
@@ -343,7 +389,7 @@ describe("computeBpInTargetFastPath", () => {
expect(result.path).toBe("live");
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(2);
expect(QUERY_RAW).toHaveBeenCalledTimes(2);
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const bpCall = calls.find(
@@ -362,7 +408,7 @@ describe("computeBpInTargetFastPath", () => {
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
QUERY_RAW.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-tokyo",
+60 -26
View File
@@ -54,6 +54,10 @@ import { prisma } from "@/lib/db";
import { annotate } from "@/lib/logging/context";
import { readRollupBuckets } from "@/lib/rollups/measurement-rollups";
import { loadUserSourcePriority } from "@/lib/rollups/measurement-read";
import {
buildSourceRankCase,
canonicalMeasurementsFrom,
} from "@/lib/analytics/source-rank-sql";
import {
probeRollupCoverage,
type RollupCoverageMap,
@@ -454,9 +458,33 @@ async function computeFromLive(
const DAY_MS = 24 * 60 * 60 * 1000;
const bpInTargetSince = new Date(now.getTime() - 365 * DAY_MS);
// v1.18.10 I-1 — collapse overlapping sources before windowing. BP is a
// multi-source metric (a Withings reading mirrored into Apple Health is two
// rows for one event). Without the collapse the live/cold path double-counts
// the shared reading in both the in-target denominator and the
// recency-weighted grade, returning a different number than the rollup/warm
// path (which reads source-collapsed `measurement_rollups`). Pick the
// ladder-winning source per (type, day) so cold == warm (web↔iOS parity).
const priorityJson = await loadUserSourcePriority(userId);
const rankUnqualified = buildSourceRankCase(
priorityJson,
'"type"',
'"source"',
);
const [sysData, diaData] = await Promise.all([
fetchSeriesChunked(userId, "BLOOD_PRESSURE_SYS", bpInTargetSince),
fetchSeriesChunked(userId, "BLOOD_PRESSURE_DIA", bpInTargetSince),
fetchSeriesChunked(
userId,
"BLOOD_PRESSURE_SYS",
bpInTargetSince,
rankUnqualified,
),
fetchSeriesChunked(
userId,
"BLOOD_PRESSURE_DIA",
bpInTargetSince,
rankUnqualified,
),
]);
const rowCount = sysData.length + diaData.length;
@@ -499,34 +527,40 @@ async function computeFromLive(
}
/**
* Cursor-paged read of one BP type. Mirrors the route's
* `fetchMeasurementSeriesChunked` shape but pruned to the only two
* fields the in-target helper consumes (`measuredAt` + `value`) so the
* Prisma round-trip stays minimal on the cold fallback path.
* Source-collapsed read of one BP type over the trailing 365 days.
*
* v1.18.10 I-1 — routes through `canonicalMeasurementsFrom` so a reading that
* exists under two sources (Withings + its Apple-Health mirror) is counted
* once per (type, day), matching the rollup fast-path. Pruned to the two
* fields the in-target helper consumes (`measuredAt` + `value`). Per-event
* rows of the winning source survive the collapse, so the downstream ±5-min
* pairing still sees individual readings. BP is low-volume even on multi-year
* accounts, so one source-collapsed query replaces the prior cursor paging.
*/
const CHUNK = 5000;
async function fetchSeriesChunked(
userId: string,
type: "BLOOD_PRESSURE_SYS" | "BLOOD_PRESSURE_DIA",
// The 365-day window is expressed as a SQL interval inside the canonical
// subquery; the bound `Date` arg is retained for call-site symmetry.
since: Date,
rankUnqualified: string,
): Promise<BpReading[]> {
const out: BpReading[] = [];
let cursorId: string | undefined;
for (let page = 0; page < 1000; page++) {
const chunk = await prisma.measurement.findMany({
where: { userId, type, measuredAt: { gte: since }, deletedAt: null },
orderBy: [{ measuredAt: "asc" }, { id: "asc" }],
select: { id: true, measuredAt: true, value: true },
take: CHUNK,
...(cursorId ? { cursor: { id: cursorId }, skip: 1 } : {}),
});
if (chunk.length === 0) break;
for (const row of chunk) {
out.push({ measuredAt: row.measuredAt, value: row.value });
}
if (chunk.length < CHUNK) break;
cursorId = chunk[chunk.length - 1].id;
}
return out;
void since;
const rows = await prisma.$queryRawUnsafe<
Array<{ measured_at: Date; value: number }>
>(
`
SELECT
m."measured_at" AS measured_at,
m."value"::double precision AS value
FROM ${canonicalMeasurementsFrom(rankUnqualified, "365 days")}
WHERE m."type" = '${type}'
ORDER BY m."measured_at" ASC
`,
userId,
);
return rows.map((r) => ({
measuredAt: new Date(r.measured_at),
value: Number(r.value),
}));
}