From 0810285061f395ced44b96a22231a6a3c20a9b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Mon, 2 Nov 2026 20:06:15 +0100 Subject: [PATCH] fix(series): keep the repeated hour of a fall-back night apart The hour grain of the dense series read truncated the local wall clock, so the hour a fall-back night runs twice folded both instances into one bucket: on that day a sensor stream showed one mean for two hours of readings. An hour bucket now starts at the reading's instant less its local minutes and seconds, which keeps the two hours apart and still starts a half-hour zone's buckets on the half hour. The new integration test pins a New York fall-back night and a half-hour zone to fixed dates, so the case runs every day instead of only when the rolling window of the existing tests covers one. --- src/lib/measurements/dense-series-read.ts | 24 ++++-- .../integration/dense-series-dst-hour.test.ts | 76 +++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 tests/integration/dense-series-dst-hour.test.ts diff --git a/src/lib/measurements/dense-series-read.ts b/src/lib/measurements/dense-series-read.ts index 0160924c4..8ce27379d 100644 --- a/src/lib/measurements/dense-series-read.ts +++ b/src/lib/measurements/dense-series-read.ts @@ -90,19 +90,31 @@ export function denseBucketsSql(hourWeighted: boolean, from: string): string { m."value", m."value_min", m."value_max", - date_trunc( - $2, - (m."measured_at" AT TIME ZONE 'UTC') AT TIME ZONE $3 - ) AS local_day + -- An hour bucket starts at the reading's instant less its minutes + -- and seconds on the local clock. Truncating the local wall clock + -- instead merged the two hours a fall-back night repeats into one + -- bucket. + CASE + WHEN $2 = 'hour' THEN ( + m."measured_at" - ( + ((m."measured_at" AT TIME ZONE 'UTC') AT TIME ZONE $3) + - date_trunc('hour', (m."measured_at" AT TIME ZONE 'UTC') AT TIME ZONE $3) + ) + ) AT TIME ZONE 'UTC' + ELSE date_trunc( + $2, + (m."measured_at" AT TIME ZONE 'UTC') AT TIME ZONE $3 + ) AT TIME ZONE $3 + END AS bucket_start FROM ${from} ) SELECT - local_day AT TIME ZONE $3 AS bucket_start, + bucket_start, AVG("value")::double precision AS mean, MIN(COALESCE("value_min", "value"))::double precision AS min_value, MAX(COALESCE("value_max", "value"))::double precision AS max_value FROM localized - GROUP BY local_day`; + GROUP BY bucket_start`; } /** The figures aggregate over the CTE `src`, in the original SQL. */ diff --git a/tests/integration/dense-series-dst-hour.test.ts b/tests/integration/dense-series-dst-hour.test.ts new file mode 100644 index 000000000..6ac5e89a0 --- /dev/null +++ b/tests/integration/dense-series-dst-hour.test.ts @@ -0,0 +1,76 @@ +/** + * Hour buckets of a dense series across a clock change, pinned to fixed + * dates so the case runs every day rather than only when the rolling window + * happens to cover one. A fall-back night repeats a local hour: both of its + * instances are their own bucket, each the mean of its own readings. A zone + * half an hour off UTC starts its buckets on the half hour. + */ +import { beforeAll, describe, expect, it } from "vitest"; + +import { readDenseSeries } from "@/lib/measurements/dense-series-read"; +import { seriesRowsFrom } from "@/lib/measurements/series-canonical"; + +import { makeUser } from "../kit/factories"; +import { getPrismaClient, truncateAllTables } from "./setup"; + +const prisma = getPrismaClient(); + +let userId = ""; + +// New York falls back on 2026-11-01 at 06:00 UTC: 01:00-02:00 local runs +// once as EDT (05:00-06:00 UTC) and once as EST (06:00-07:00 UTC). +const READINGS: Array<[string, number]> = [ + ["2026-11-01T04:30:00.000Z", 5], + ["2026-11-01T05:10:00.000Z", 6], + ["2026-11-01T05:50:00.000Z", 8], + ["2026-11-01T06:10:00.000Z", 10], + ["2026-11-01T06:40:00.000Z", 12], + ["2026-11-01T07:20:00.000Z", 9], +]; + +async function read(userTz: string) { + const since = new Date("2026-11-01T00:00:00.000Z"); + const { bucketRows } = await readDenseSeries({ + userId, + type: "BLOOD_GLUCOSE", + grain: "hour", + userTz, + since, + seriesRows: seriesRowsFrom(null, "BLOOD_GLUCOSE", since), + }); + return bucketRows.map((r) => [r.bucket_start.toISOString(), r.mean]); +} + +describe("dense-series hour buckets across a clock change", () => { + beforeAll(async () => { + await truncateAllTables(prisma); + userId = (await makeUser({ timezone: "America/New_York" })).id; + await prisma.measurement.createMany({ + data: READINGS.map(([at, value]) => ({ + userId, + type: "BLOOD_GLUCOSE" as const, + source: "NIGHTSCOUT" as const, + value, + unit: "mg/dL", + measuredAt: new Date(at), + })), + }); + }); + + it("keeps both instances of the repeated local hour apart", async () => { + expect(await read("America/New_York")).toEqual([ + ["2026-11-01T04:00:00.000Z", 5], + ["2026-11-01T05:00:00.000Z", 7], + ["2026-11-01T06:00:00.000Z", 11], + ["2026-11-01T07:00:00.000Z", 9], + ]); + }); + + it("starts the hours of a half-hour zone on the half hour", async () => { + expect(await read("Asia/Kolkata")).toEqual([ + ["2026-11-01T04:30:00.000Z", 5.5], + ["2026-11-01T05:30:00.000Z", 9], + ["2026-11-01T06:30:00.000Z", 10.5], + ]); + }); +});