From e7969685554f90398d24c85707228ac828b99721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sat, 6 Jun 2026 16:22:17 +0200 Subject: [PATCH] fix(insights): compute comprehensive BP-in-target through the canonical helper The comprehensive endpoint paired sys+dia on the 5-minute window only, so readings whose sys/dia timestamps drift past 5 minutes on Withings / Apple-Health imports were dropped, skewing the in-target share the Home Health Score renders. Route it through computeBpInTargetPct, which adds the same-Berlin-day pairing fallback and shares one definition with the dashboard tile, insight cards, AI snapshot and targets endpoint. Band (ESH-2023 ceiling + 90/50 hypotension floor) and the 90-day window are unchanged. --- src/app/api/insights/comprehensive/route.ts | 44 +++++++++------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/app/api/insights/comprehensive/route.ts b/src/app/api/insights/comprehensive/route.ts index 7884d9f2f..9a9042b28 100644 --- a/src/app/api/insights/comprehensive/route.ts +++ b/src/app/api/insights/comprehensive/route.ts @@ -4,14 +4,13 @@ import { apiSuccess } from "@/lib/api-response"; import type { DataPoint, DataSummary } from "@/lib/analytics/trends"; import { summarize } from "@/lib/analytics/trends"; import { getBpTargets } from "@/lib/analytics/bp-targets"; -import { isBpReadingInTarget } from "@/lib/analytics/bp-in-target"; +import { computeBpInTargetPct } from "@/lib/analytics/bp-in-target"; import { classifyBMI, classifyBP, generateAlerts, } from "@/lib/analytics/classifications"; import { - pairByTimestamp, pearsonCorrelation, type PairedPoint, } from "@/lib/analytics/correlations"; @@ -185,31 +184,26 @@ export async function buildComprehensiveResponse(user: AuthedUser) { ); } - // ── BP target adherence ────────────────────────────────── - // Keeps the 5-minute tolerance contract — sys + dia must pair on - // the same reading session. The aggregator pulled the bounded raw - // rows; we reuse `pairByTimestamp` against them so the output is - // byte-identical to the legacy path. + // ── BP target adherence (trailing 90 days) ─────────────── + // Route through the canonical `computeBpInTargetPct` so this figure + // shares ONE definition with the dashboard tile, the insight cards, the + // AI snapshot and the targets endpoint (per the bp-in-target.ts + // contract — the inline copy that used to live here drifted: it paired + // sys+dia on the 5-minute window ONLY, dropping readings whose sys/dia + // timestamps drift past 5 min on Withings / Apple-Health imports, which + // skewed the share iOS renders on the Home Health Score). The helper + // adds the same-Berlin-day pairing fallback so those readings pair, and + // keeps the ESH-2023 band + 90/50 hypotension floor (both sys AND dia at + // or below the age-band ceiling). Window is the aggregator's 90-day raw + // pull — a "recent control" figure, not the all-time headline. let bpPctInTarget: number | null = null; if (bpTargets) { - const sysData: DataPoint[] = aggregate.bpRawRows.sys.map((r) => ({ - date: r.measuredAt, - value: r.value, - })); - const diaData: DataPoint[] = aggregate.bpRawRows.dia.map((r) => ({ - date: r.measuredAt, - value: r.value, - })); - const sysPairs = pairByTimestamp(sysData, diaData, 5 * 60 * 1000); - - if (sysPairs.length > 0) { - // v1.4.16 A2 — one-sided ceiling semantics with hypotension - // floor. See lib/analytics/bp-in-target.ts. - const inTarget = sysPairs.filter((p) => - isBpReadingInTarget(p.a, p.b, bpTargets), - ).length; - bpPctInTarget = Math.round((inTarget / sysPairs.length) * 100); - } + bpPctInTarget = + computeBpInTargetPct( + aggregate.bpRawRows.sys, + aggregate.bpRawRows.dia, + bpTargets, + )?.pct ?? null; } // ── Correlations: weight × Sys BP ─────────────────────────