Salud: a card opens onto the day, hour by hour, and onto the days before it
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s

Reported as "le doy a cualquier actividad y solo me presenta los pasos de hoy… debiera
presentarme una medida por hora, y no me da el histórico". Two separate things, and only one
of them was the app's fault.

**The history was the automation's setting.** With Date Range on its default (`Today`) every
sync carries today and nothing else, so there is exactly one day in the file to draw. That is
why a card looked frozen: not a rendering bug, a range that was never sent. The setup steps
now say `Previous 7 Days` and say why.

**The hours were thrown away by us.** Even when the phone sends 288 readings a day, the
receiver folded every one of them into a single day aggregate, so "8,123 steps today" was all
there could ever be — a number you cannot look into. Worse, the recommended setting was Time
Grouping `Days`, which sends one point per metric per day: the intraday detail never arrived
in the first place. The recommendation is now `Hours`, and the storage keeps the hours.

The merge rule for them is the same shape as the day rule, for the same reason: a payload
whose datapoints all sit on the hour is HAE's own hour grouping, and the hour *in progress*
grows on every run of a five-minute automation, so it is replaced and never unioned — 500 +
900 + 1,340 steps in one hour is a number no watch recorded. The day is then recomputed as
the sum of its hours, which is what keeps the card and the detail from ever disagreeing, and
which also makes switching an automation from Days to Hours mid-day converge instead of
double-count. Raw samples (real timestamps) still union by instant plus numbers, and they now
land in their own hour, which is where intraday detail comes from when nothing is grouped.

Three things that would have been silent bugs, caught while writing it:

- The hour rule is decided from the **whole payload**, never from one timestamp. A five-minute
  automation produces samples that sit exactly on the hour, and reading one of those as an hour
  total wipes the hour it belongs to. There is a test with a 09:00:00 and a 09:05:00 sample in
  one payload, and it fails against the per-point version of the rule.
- A day that arrived as a day total keeps its hours beside it, and a day stored before this
  existed keeps its aggregate when the first hour shows up — recomputing it from hours it does
  not have would delete 2,000 steps. Both are mutation-checked (removing either guard fails a
  test).
- A payload grouped by hour writes the ledger row anyway, because a later payload with real
  minutes can describe the same instant and the union path can only see the ledger. Without it
  the raw-sample route test double-counts a single sample.

The hours are the bulky part of this data, so they are kept for a week (the day aggregates
still live the full 400 days), pruned when the night's file is written, and fetched one metric
and one day at a time — `GET /api/vitals/hours`. A screen listing forty cards downloads none
of them; the day view carries only a count per day, which is what tells the sheet whether it
is worth asking. The sheet draws the hours, the days before it (tapping one moves the hour
list to that day), and every field the watch sent; when the automation groups by Days it says
so instead of leaving a gap where a chart should be.

Coverage: 6 new tests in `api/test/vitals.test.js` (19 total) and a route test in
`api/test/server-vitals.test.js` (5), the hour/history model in `vitals-view.test.js` (31),
three render tests through the real sheet, the route documented in `api/openapi.yaml` and
`website/api.html`, and `docs/HEALTH.md` with the settings, the new merge row and the two
symptoms now in Troubleshooting.
This commit is contained in:
dsantillan committed 2026-09-23 13:28:58 -04:00
1 parent 34d28fa5c5
commit aa6ec8ef22
14 files changed
+839 -63

No files matched your search

+95 -5
View File
@@ -658,7 +658,7 @@ paths:
step_count:
units: count
sources: [Apple Watch]
days: [{ day: "2026-09-03", n: 1, lastTs: 1756900000000, f: { qty: { n: 1, sum: 12481, min: 12481, max: 12481, last: 12481, avg: 12481 } } }]
days: [{ day: "2026-09-03", n: 1, lastTs: 1756900000000, hours: 0, f: { qty: { n: 1, sum: 12481, min: 12481, max: 12481, last: 12481, avg: 12481 } } }]
heart_rate:
units: bpm
sources: [Apple Watch]
@@ -682,6 +682,84 @@ paths:
properties: { ok: { type: boolean } }
'401': { $ref: '#/components/responses/Unauthorized' }
/api/vitals/hours:
get:
tags: [health]
operationId: vitalsHours
summary: One metric's hours on one day
description: |
The detail behind a card: the hourly buckets the phone sent for a single metric on a single
day, oldest first, each carrying the same per-field aggregates a day does.
This is a separate request on purpose. The hours are the bulky part of health data (24
buckets per metric per day), and they are only interesting once somebody taps a card, so a
screen listing forty metrics fetches none of them.
A day the automation grouped by Day has no hours and comes back as an empty list rather
than a fabricated one: the honest answer to "what did the hours look like" for a payload
that never carried hours is that the automation does not send them. Set Time Grouping to
**Hours** in Health Auto Export to keep them — the day remains the sum of its hours, so
nothing is counted twice.
Session only, like `GET /api/vitals`: the ingest key is write-only.
parameters:
- name: metric
in: query
required: true
schema: { type: string }
description: The metric name exactly as HAE wrote it (`step_count`, `heart_rate`…).
- name: day
in: query
required: true
schema: { type: string, example: "2026-09-03" }
description: A calendar day, in the phone's own date, as `YYYY-MM-DD`.
responses:
'200':
description: The hours stored for that metric and day (empty when there are none).
content:
application/json:
schema:
type: object
properties:
ok: { type: boolean }
metric:
type: string
nullable: true
description: Echoed when the profile has that metric, null otherwise.
day:
type: string
nullable: true
description: Echoed when the day exists for that metric, null otherwise.
units: { type: string }
hours:
type: array
items:
type: object
properties:
h:
type: string
description: "The hour as the phone wrote it: `00`–`23`."
n: { type: integer, description: Samples folded into this hour. }
ts: { type: integer, description: Epoch ms of the hour's newest sample. }
f:
type: object
additionalProperties: { $ref: '#/components/schemas/VitalsField' }
example:
ok: true
metric: step_count
day: "2026-09-03"
units: count
hours:
- h: "08"
n: 12
ts: 1756900000000
f: { qty: { n: 12, sum: 512, min: 3, max: 120, last: 44, avg: 42.7 } }
- h: "09"
n: 12
ts: 1756903600000
f: { qty: { n: 12, sum: 1340, min: 8, max: 310, last: 96, avg: 111.7 } }
'401': { $ref: '#/components/responses/Unauthorized' }
/api/vitals/ingest:
post:
tags: [health]
@@ -691,16 +769,21 @@ paths:
The endpoint an iOS automation posts to. In Health Auto Export: Automation Type **REST
API**, URL = this endpoint, header `Authorization: Bearer <ingest key>` (from
`POST /api/vitals/key`), Export Format **JSON**, Export Version **2**, Summarize Data
**On**, Time Grouping **Days**.
**On**, Time Grouping **Hours** (or **Days** for totals with nothing behind them), Date
Range **Previous 7 Days** — with the default range every sync carries today only, and the
past is never in the file.
The merge is idempotent, which is what makes a five-minute cadence safe:
* A payload whose datapoints all sit on midnight is HAE's own day grouping, and a day total
*replaces* the day it names — so the same day re-sent all afternoon converges on the
day's total instead of adding up.
* Anything else is a real sample (or an hour/minute grouping): a sample is identified by its
instant plus its numbers and is counted once, so overlapping and disjoint windows both
come out right. Repeats come back as `duplicates`.
* A payload whose datapoints all sit on the hour is its hour grouping: the *hour* is
replaced for the same reason (the hour in progress grows on every run), and the day is
recomputed as the sum of its hours, so the day and the hourly detail can never disagree.
* Anything else is a real sample: it is identified by its instant plus its numbers and is
counted once, and it also lands in its own hour, which is where intraday detail comes
from. Repeats come back as `duplicates`.
CSRF-exempt: the request carries its own credential in a header and has no session to
borrow.
@@ -1749,6 +1832,13 @@ components:
day: { type: string, example: "2026-09-03" }
n: { type: integer, description: Samples stored for this day. }
lastTs: { type: integer, description: Epoch ms of the day's newest sample. }
hours:
type: integer
description: >-
How many hourly buckets this day has (0 or 1 when the automation grouped by Day, or
when the samples arrived without an hour of their own). The buckets themselves are not
in this response — `GET /api/vitals/hours` answers one metric and one day, because 24
buckets × every metric × every day is not something a grid of cards should download.
f:
type: object
description: Field name as HAE wrote it — `qty`, `Avg`, `asleep`, `systolic`…