mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
fix(sleep): reconstruct an ordered WHOOP hypnogram timeline
WHOOP's v2 API returns only per-stage sleep duration totals — no onset timestamps, no hypnogram endpoint. The mapper stamped all five stage totals on the one sleep-END instant, so the hypnogram reconstructed every stage as a span touching the night's right edge: bars stacked right-aligned with no clock times whenever WHOOP was the sole source. Lay the asleep stages back-to-back from sleep onset in a fixed physiological order (AWAKE lead, then CORE → DEEP → REM), emitting one timed row per segment with measuredAt at that segment's end and a distinct indexed externalId. IN_BED stays a single envelope row over the whole window. The existing reader and renderer are untouched — they now see distinct per-segment instants and place each block at its own time. The order is synthesised, not measured, so every reconstructed row is flagged and the night DTO carries a per-session `reconstructed` boolean (GET /api/sleep/night, regenerated OpenAPI). The client renders the hypnogram but labels it an approximate layout and never recomputes; real-series sources (Apple Health, Withings, Fitbit) stay false.
This commit is contained in:
1 parent
94fd837d48
commit
bc12633569
8 files changed
+246
-30
No files matched your search
@@ -8982,6 +8982,11 @@ components:
|
||||
type: integer
|
||||
minimum: 0
|
||||
maximum: 9007199254740991
|
||||
reconstructed:
|
||||
type: boolean
|
||||
description: True when the source has no per-stage onset timestamps and the server synthesised a contiguous timeline in
|
||||
a fixed physiological order (WHOOP). The client renders the hypnogram but labels it an approximate
|
||||
layout and never recomputes. False for real-series sources (Apple Health / Withings / Fitbit).
|
||||
stages:
|
||||
type: object
|
||||
propertyNames:
|
||||
@@ -9047,6 +9052,7 @@ components:
|
||||
- inBedMinutes
|
||||
- awakeMinutes
|
||||
- awakenings
|
||||
- reconstructed
|
||||
- stages
|
||||
- segments
|
||||
additionalProperties: false
|
||||
@@ -9102,6 +9108,11 @@ components:
|
||||
type: integer
|
||||
minimum: 0
|
||||
maximum: 9007199254740991
|
||||
reconstructed:
|
||||
type: boolean
|
||||
description: True when the source has no per-stage onset timestamps and the server synthesised a contiguous timeline in
|
||||
a fixed physiological order (WHOOP). The client renders the hypnogram but labels it an approximate
|
||||
layout and never recomputes. False for real-series sources (Apple Health / Withings / Fitbit).
|
||||
stages:
|
||||
type: object
|
||||
propertyNames:
|
||||
@@ -9167,6 +9178,7 @@ components:
|
||||
- inBedMinutes
|
||||
- awakeMinutes
|
||||
- awakenings
|
||||
- reconstructed
|
||||
- stages
|
||||
- segments
|
||||
additionalProperties: false
|
||||
|
||||
@@ -12,6 +12,11 @@
|
||||
* priority ladder — so two sources' timelines never overlay. A view over the
|
||||
* existing per-stage `SLEEP_DURATION` rows; no schema, no new type.
|
||||
*
|
||||
* `reconstructed` (per session) is true when the winning source has no
|
||||
* per-stage onset timestamps and the server synthesised a contiguous timeline
|
||||
* (WHOOP) — the client renders the hypnogram but labels it an approximate
|
||||
* layout. A real-series source (Apple Health / Withings / Fitbit) is false.
|
||||
*
|
||||
* `date` omitted → the most recent night in the trailing-year window.
|
||||
*
|
||||
* Auth: cookie session OR Bearer token (`requireAuth`). Soft-delete-filtered.
|
||||
@@ -64,6 +69,10 @@ function serializeSession(s: SleepSession) {
|
||||
inBedMinutes: s.inBedMinutes === null ? null : Math.round(s.inBedMinutes),
|
||||
awakeMinutes: s.awakeMinutes === null ? null : Math.round(s.awakeMinutes),
|
||||
awakenings: s.awakenings,
|
||||
// iOS #18 — `reconstructed` is true when the source has no per-stage onset
|
||||
// timestamps and the server synthesised a contiguous timeline (WHOOP). iOS
|
||||
// labels the hypnogram as an approximate layout; it never recomputes.
|
||||
reconstructed: s.reconstructed,
|
||||
// iOS #18 — round the per-stage map too; the underlying totals sum
|
||||
// second-precision segments and otherwise serialise as e.g. 88.4999.
|
||||
stages: Object.fromEntries(
|
||||
|
||||
@@ -380,6 +380,48 @@ describe("reconstructSleepSessions", () => {
|
||||
expect(sessions[0].asleepMinutes).toBe(420);
|
||||
});
|
||||
|
||||
it("flags a WHOOP-won night as reconstructed (synthetic stage order) with clock-time-distinct segments", () => {
|
||||
// The ingest mapper reconstructs an ordered, contiguous WHOOP timeline.
|
||||
// The reconstructed segments carry distinct measuredAt instants, so the
|
||||
// hypnogram lays them at distinct clock times (no shared right edge).
|
||||
const rows: SleepStageRow[] = [
|
||||
srcRow("2026-06-04T01:00:00.000Z", "CORE", 240, "WHOOP"), // 21:00→01:00
|
||||
srcRow("2026-06-04T03:00:00.000Z", "DEEP", 120, "WHOOP"), // 01:00→03:00
|
||||
srcRow("2026-06-04T05:00:00.000Z", "REM", 120, "WHOOP"), // 03:00→05:00
|
||||
];
|
||||
const sessions = reconstructSleepSessions(rows, "UTC");
|
||||
expect(sessions).toHaveLength(1);
|
||||
const s = sessions[0];
|
||||
expect(s.source).toBe("WHOOP");
|
||||
expect(s.reconstructed).toBe(true);
|
||||
// No two segments end on the same instant — not stacked on the right edge.
|
||||
const ends = s.segments.map((seg) => seg.end.getTime());
|
||||
expect(new Set(ends).size).toBe(ends.length);
|
||||
});
|
||||
|
||||
it("does NOT flag an Apple-Health night as reconstructed (measured timeline)", () => {
|
||||
const rows: SleepStageRow[] = [
|
||||
srcRow("2026-06-04T01:00:00.000Z", "CORE", 240, "APPLE_HEALTH"),
|
||||
srcRow("2026-06-04T03:00:00.000Z", "DEEP", 90, "APPLE_HEALTH"),
|
||||
srcRow("2026-06-04T04:30:00.000Z", "REM", 90, "APPLE_HEALTH"),
|
||||
];
|
||||
const sessions = reconstructSleepSessions(rows, "UTC");
|
||||
expect(sessions).toHaveLength(1);
|
||||
expect(sessions[0].source).toBe("APPLE_HEALTH");
|
||||
expect(sessions[0].reconstructed).toBe(false);
|
||||
});
|
||||
|
||||
it("does NOT flag a Withings night as reconstructed (real per-segment series)", () => {
|
||||
const rows: SleepStageRow[] = [
|
||||
srcRow("2026-06-04T01:00:00.000Z", "CORE", 240, "WITHINGS"),
|
||||
srcRow("2026-06-04T03:00:00.000Z", "DEEP", 90, "WITHINGS"),
|
||||
];
|
||||
const sessions = reconstructSleepSessions(rows, "UTC");
|
||||
expect(sessions).toHaveLength(1);
|
||||
expect(sessions[0].source).toBe("WITHINGS");
|
||||
expect(sessions[0].reconstructed).toBe(false);
|
||||
});
|
||||
|
||||
it("drops the bare ASLEEP aggregate from segments + stages when granular exists (HIGH H1, v1.11.5)", () => {
|
||||
// Apple-Health shape: granular CORE/DEEP/REM PLUS the unspecified ASLEEP
|
||||
// aggregate covering the same period, plus IN_BED. The hypnogram must not
|
||||
|
||||
@@ -281,6 +281,19 @@ export interface SleepNight {
|
||||
/** Sentinel for rows that carry no source — collapses to one bucket. */
|
||||
const NO_SOURCE = "__none__";
|
||||
|
||||
/**
|
||||
* Sources whose per-segment stage ORDER is synthesised, not measured. WHOOP's
|
||||
* v2 API exposes only per-stage duration totals (no onset timestamps), so the
|
||||
* ingest mapper reconstructs a contiguous timeline in a fixed physiological
|
||||
* order (`whoop/client.ts` `mapSleep`). A session won by such a source carries
|
||||
* `reconstructed: true` so the UI labels it as an approximate layout. Apple
|
||||
* Health / Withings / Fitbit all store a real per-segment series and stay off
|
||||
* this set.
|
||||
*/
|
||||
const RECONSTRUCTED_TIMELINE_SOURCES: ReadonlySet<string> = new Set<string>([
|
||||
"WHOOP",
|
||||
]);
|
||||
|
||||
/**
|
||||
* Separator between the source and device-type parts of a writer key.
|
||||
* ` | ||||