Files
faceclaw/app/health/health-phone-chart.ts
T
Chris LeeandClaude Opus 5 22c851a6b9 Health: on-device store, derivations and a glasses glance for ring data
The companion half of the ring protocol, offered less as finished UI than as
an executable answer to "what does this ring data actually mean" -- the part
that took two sessions to work out and that a future contributor would
otherwise have to redo.

- app/health/: wire->domain conversion, an append-only on-device store with
  deduping ingest, rollups, a per-day step-bucket ledger, sleep stage mapping
  (0=wake 1=REM 2=light 3=deep, verified against 34 real nights), and chart
  derivations.
- app/apps/health/: a glasses glance page over that store.
- tools/health-preview.cjs: renders the real chart code headlessly, so layout
  can be checked without an emulator.
- 26 new tests, all passing.

Three clock frames meet in this data and mixing them up causes real bugs, so
they are documented at each site. The ring's clock is deliberately set to
now + 4h (matching what a real Even write carries), so every timestamp it
reports is in that shifted frame and must be corrected on read. Sleep is the
exception: it needs -8h, twice the correction, confirmed against a known
night. Why the ring wants these quantities is unexplained -- the code says so
rather than inventing a tidy mechanism, because the tidy story was tried and
measurement falsified it.

Collection runs on a wall-clock-aligned tick at :01 and :31. Aligned because
the ring's step buckets close on 10-minute boundaries, and because an elapsed
timer drifts off the boundary it is meant to track; offset by a minute because
firing exactly on the boundary risks catching a bucket before it is finalised.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 20:14:09 -07:00

102 lines
3.1 KiB
TypeScript

/**
* The phone graph view's BITMAP, built without touching the phone.
*
* Same split, and for the same reason, as `health-glance.ts` is split out of
* the glasses app: `health-view-model.ts` owns the store reads, the measured
* box, the fold class and the `ImageSource` conversion - all NativeScript - and
* hands this file a size, a font and some already-selected data. What comes
* back is a `GrayImage`.
*
* The payoff is that `tools/health-preview.cjs` can render the phone chart at
* any width under plain node, which is how the narrow/wide layouts get checked
* without an emulator. It also means the chart the preview shows is built by
* the same function the phone calls, not a reimplementation of it.
*/
import { GrayImage, type UiFont } from "../graphics/image";
import {
drawMetricChart,
drawSleepLanes,
drawSleepStackChart,
INK,
} from "./health-chart";
import type { RollupPoint, SampleMetric, SleepNight } from "./health-types";
import type { SleepStageName } from "./sleep-stages";
/**
* What to draw. Three shapes, because the redesign gave sleep two charts of
* its own that are not "a series of numbers over time":
*
* - `metric` the five measured types, banded or barred (unchanged)
* - `nights` sleep over a week/month/quarter: the diverging stacked bar
* - `lanes` sleep over ONE day: the four stage lanes, which is the same
* view the glasses drill-down shows, so the two surfaces agree
* about what "sleep, today" looks like
*/
export type PhoneChartContent =
| {
kind: "metric";
metric: SampleMetric;
points: readonly RollupPoint[];
mode: "band" | "bars";
xLabel: (point: RollupPoint, index: number) => string;
}
| {
kind: "nights";
nights: readonly SleepNight[];
xLabel: (night: SleepNight, index: number) => string;
}
| {
kind: "lanes";
bands: readonly { stage: SleepStageName | null; seconds: number }[];
laneText: (stage: SleepStageName) => { label: string; value: string };
};
export type PhoneChartRequest = {
/** Pixels, already scaled by RENDER_SCALE. */
width: number;
height: number;
font: UiFont;
content: PhoneChartContent;
};
/** The inset between the bitmap's edge and the plot. */
const INSET = 8;
export function renderPhoneChart(request: PhoneChartRequest): GrayImage {
const { width, height, font, content } = request;
const image = new GrayImage(width, height, INK.background);
const rect = {
x: INSET,
y: INSET,
width: Math.max(8, width - INSET * 2),
height: Math.max(8, height - INSET * 2),
};
switch (content.kind) {
case "metric":
drawMetricChart(image, rect, {
metric: content.metric,
points: content.points,
font,
mode: content.mode,
xLabel: content.xLabel,
});
return image;
case "nights":
drawSleepStackChart(image, rect, {
nights: content.nights,
font,
xLabel: content.xLabel,
});
return image;
case "lanes":
drawSleepLanes(image, rect, {
bands: content.bands,
font,
laneText: content.laneText,
});
return image;
}
}