fix(measurements): sum both starts of the repeated fall-back hour into its slot

This commit is contained in:
Marc-André Bombeck committed 2026-10-16 05:57:19 +02:00
1 parent f360d221e1
commit 617b87a583
3 files changed
+123 -31

No files matched your search

@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
classifyHourBucket,
combineHourWrites,
cumulativeFromIncrements,
HOUR_NOT_POSTED,
isStartOfLocalHour,
@@ -158,3 +159,38 @@ describe("cumulativeFromIncrements", () => {
expect(postedHours(increments)).toBe(3);
});
});
describe("combineHourWrites", () => {
const entry = (index: number, startIso: string, value: number) => ({
index,
type: "ACTIVITY_STEPS" as const,
hkIdentifier: "HKQuantityTypeIdentifierStepCount",
dateKey: "2026-10-25",
hour: 2,
value,
startMs: Date.parse(startIso),
});
it("sums the two starts of the hour a fall-back day repeats", () => {
// Berlin, 2026-10-25: 02:00 CEST is 00:00Z, 02:00 CET is 01:00Z.
const { writes, superseded } = combineHourWrites([
entry(0, "2026-10-25T00:00:00Z", 300),
entry(1, "2026-10-25T01:00:00Z", 120),
]);
expect(superseded).toEqual([]);
expect(writes).toHaveLength(1);
expect(writes[0].write.value).toBe(420);
expect(writes[0].contributors.sort()).toEqual([0, 1]);
});
it("lets the last entry of one start win, and marks the earlier one", () => {
const { writes, superseded } = combineHourWrites([
entry(0, "2026-10-25T00:00:00Z", 300),
entry(1, "2026-10-25T00:00:00Z", 310),
entry(2, "2026-10-25T01:00:00Z", 120),
]);
expect(superseded).toEqual([0]);
expect(writes[0].write.value).toBe(430);
expect(writes[0].write.index).toBe(2);
});
});
@@ -177,6 +177,59 @@ export interface HourBucketWrite {
value: number;
}
/** One hour as the batch reads it, with the instant it starts at. */
export interface HourBucketEntry extends HourBucketWrite {
/** `startDate` of the entry, in epoch milliseconds. */
startMs: number;
}
/**
* A batch's hours reduced to one write per slot.
*
* A slot is one local hour of one day. On the autumn day the clock falls
* back, one local hour happens twice, and the phone hands over both under
* the same key (`…T02`), each with its own `startDate`: they are two hours of
* activity and are summed into the slot rather than the second replacing the
* first. Entries that repeat the same start are the same hour posted twice,
* and the last one counts, as for every other slot.
*
* `contributors` are the entries whose values the write carries (they take
* its status); `superseded` are the earlier repeats of one start.
*
* The slot holds one number, so the sum is only as complete as the batch
* that carries it: both occurrences have to arrive together. The phone posts
* a day's hours in one batch, and a later batch that carries only one of
* them replaces the slot with that one, as a re-post of any slot does.
*/
export function combineHourWrites(entries: readonly HourBucketEntry[]): {
writes: Array<{ write: HourBucketWrite; contributors: number[] }>;
superseded: number[];
} {
const slots = new Map<string, Map<number, HourBucketEntry>>();
const superseded: number[] = [];
for (const entry of entries) {
const key = `${entry.type}::${entry.dateKey}::${entry.hour}`;
const byStart = slots.get(key) ?? new Map<number, HourBucketEntry>();
const earlier = byStart.get(entry.startMs);
if (earlier) superseded.push(earlier.index);
byStart.set(entry.startMs, entry);
slots.set(key, byStart);
}
const writes = [...slots.values()].map((byStart) => {
const occurrences = [...byStart.values()];
const last = occurrences.reduce((a, b) => (b.index > a.index ? b : a));
const { startMs: _start, ...base } = last;
return {
write: {
...base,
value: occurrences.reduce((sum, entry) => sum + entry.value, 0),
},
contributors: occurrences.map((entry) => entry.index),
};
});
return { writes, superseded };
}
/**
* Store a batch's hours, one transaction per (type, day). Returns the
* per-entry status: `inserted` for the first value of a slot, `updated` when