From 7032cb1b76a4a8d34300c2faf9ffa136075d0e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sun, 17 May 2026 14:33:58 +0200 Subject: [PATCH] refactor(analytics): rename ninetyFiveDaysAgo + document tz day-key --- src/lib/analytics/bp-in-target-fast-path.ts | 6 +++--- src/lib/analytics/correlations-fast-path.ts | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/analytics/bp-in-target-fast-path.ts b/src/lib/analytics/bp-in-target-fast-path.ts index a9b6539de..cbb20ade5 100644 --- a/src/lib/analytics/bp-in-target-fast-path.ts +++ b/src/lib/analytics/bp-in-target-fast-path.ts @@ -147,21 +147,21 @@ async function computeFromRollups( // days ago. We read 396 days so the boundary day rolls cleanly into // the prior-year bucket without being dropped. const DAY_MS = 24 * 60 * 60 * 1000; - const ninetyFiveDaysAgo = new Date(now.getTime() - 396 * DAY_MS); + const readSince = new Date(now.getTime() - 396 * DAY_MS); const [sysBuckets, diaBuckets] = await Promise.all([ readRollupBuckets( userId, "BLOOD_PRESSURE_SYS", "DAY", - ninetyFiveDaysAgo, + readSince, now, ), readRollupBuckets( userId, "BLOOD_PRESSURE_DIA", "DAY", - ninetyFiveDaysAgo, + readSince, now, ), ]); diff --git a/src/lib/analytics/correlations-fast-path.ts b/src/lib/analytics/correlations-fast-path.ts index 8b5cc78d0..3bd2a7456 100644 --- a/src/lib/analytics/correlations-fast-path.ts +++ b/src/lib/analytics/correlations-fast-path.ts @@ -137,6 +137,17 @@ export async function computeCorrelationHypothesesFastPath( if (measurementsOnRollups) { // Rollup-fast-path. One indexed read per type against // `measurement_rollups`, projecting the per-day mean directly. + // + // The rollup table buckets at UTC midnight (`date_trunc('day', …)` + // in Postgres). For a user whose display tz sits more than a few + // hours from UTC the bucket's `bucketStart` can land on the + // previous local calendar day relative to the same wall-clock + // reading on the live path. Today the production tenant is + // Berlin-centric (UTC+1/+2 → midnight UTC = 01:00/02:00 Berlin, + // same calendar day) so the shift is benign. A v1.5 follow-up + // could mint per-user-tz buckets if the discrepancy materialises + // on a non-Berlin account; the n >= 20 surface gate absorbs the + // single-day phase shift either way. const [sysBuckets, pulseBuckets, weightBuckets] = await Promise.all([ readRollupBuckets(userId, "BLOOD_PRESSURE_SYS", "DAY", since, now), readRollupBuckets(userId, "PULSE", "DAY", since, now),