Salud: dos dispositivos, una caminata — el cubo guarda sus fuentes y muestra una
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

"En mi Apple Salud me dice 946 y en opengym 1700 y pico", y antes 499 contra 840. Ese factor —1,80 y
1,68— es la prueba: una suma de valores acumulados daría 12× o 24× al final del día, no 1,8×. Lo que
da 1,8× es **sumar dos dispositivos**. HealthKit de-duplica la misma caminata entre el Apple Watch y
el iPhone, la app Salud la cuenta una vez, y un payload que trae los dos como puntos separados no la
ha de-duplicado nadie. La tarjeta estaba leyendo dos cosas, exactamente como se sospechó.

Ahora cada cubo —una hora, y el día cuando el teléfono manda un total por día— **guarda sus fuentes
por separado y muestra una**: la que más reportó en ese cubo, después la más nueva, después la más
grande. No es una suma, no es un promedio y no es "el máximo": es el agregado propio de un
dispositivo, o sea el número que ese dispositivo muestra.

Y la elección es **por hora**, no por día, que es lo que hace correctas las dos situaciones a la vez:
una hora que solo registró el teléfono es del teléfono (no se pierde nada), y una hora que
registraron los dos se cuenta una vez (no se duplica nada). Con la elección por día, el caso
"caminata sin el reloj" se habría perdido.

Tres cosas que había que cuidar y están cubiertas por tests:

  · **Un payload sin `source`** —casi todos, y en iOS 27+ es lo normal para datos acumulados— vive en
    una sola fuente sin nombre, así que un cubo es el cubo y nada cambia para quien tiene un solo
    dispositivo. Los 22 tests que ya existían pasaron sin tocarlos, que es la evidencia de eso.
  · **Un cubo guardado antes de esto** se adopta como la fuente sin nombre en vez de tirarse: una
    actualización no pierde el día que ya tenía, y las dos compiten por cantidad de muestras.
  · **Reemplazar una hora borra solo la de esa fuente.** La hora en curso crece en cada envío de un
    automatismo de cinco minutos y por eso se reemplaza, pero reemplazar el cubo entero es cómo un
    dispositivo borraría en silencio el reporte del otro — que es lo contrario de lo que esto hace.

La hoja lo dice al final: `N días con datos · Apple Watch, iPhone · se usa Apple Watch`, para que el
número se pueda cotejar contra el dispositivo que lo produjo.

Cinco tests del merge más el orden de desempate. Mutation-check de las dos mitades: sin aplicar la
fuente ganadora fallan cuatro, y si reemplazar una hora borra la del otro dispositivo fallan tres.
API 267 en verde, frontend 1705, build OK.
This commit is contained in:
dsantillan committed 2026-09-23 18:29:56 -04:00
1 parent 309aba8766
commit f7b884be81
6 files changed
+317 -18

No files matched your search

+2
View File
@@ -69,6 +69,7 @@ configured for, so the rule is chosen per payload, from the payload's own shape:
| Every datapoint sits exactly on midnight (`00:00:00`) — HAE's **Days** grouping, i.e. a **total** per day | The day it names is **replaced** by it, in full. A day total only ever supersedes another day total, so re-sending the same day all afternoon converges on the day's total instead of accumulating. | Overlapping windows, disjoint windows, manual re-exports, first sync of a year of history. |
| Every datapoint sits exactly on the hour (`HH:00:00`) — HAE's **Hours** grouping, i.e. a **total** per hour | That **hour** is replaced, for the same reason: the hour in progress grows on every run. The day is then recomputed as the sum of its hours, so the day total and the hourly detail can never disagree. The hours are what the detail sheet draws (kept for 7 days; the day aggregates live on). | Overlapping windows, disjoint windows, and switching an automation from `Days` to `Hours` mid-day. |
| Anything else — real sample timestamps, or Minute grouping | Each sample is identified by its **instant plus its numbers** and stored once, in its own hour. A repeat is skipped and reported back as `duplicates`; a genuinely new sample is added. | Overlapping windows (repeats detected) and disjoint ones (new samples add). |
| **Two devices in one hour** (an Apple Watch and an iPhone both recording the walk) | The hour keeps each source's aggregates apart and shows **one** of them — the source that reported most in that hour, then the newest, then the largest. Not a sum: HealthKit de-duplicates the same walk, and adding both is what turns 946 steps into 1,700. The choice is per hour, which is what makes the disjoint case right too — an hour only the phone recorded is the phone's hour, and nothing competes with it. | A metric with a single source, or one whose payload has no `source` at all, behaves exactly as it did: one source, which is the bucket. |
The hour rule is decided from the **whole payload**, never from a single timestamp: a five-minute
automation also produces samples that land exactly on the hour, and reading one of those as an hour
@@ -234,6 +235,7 @@ and you have backed up your health file too.
| `200` with `received.points: 0` every time | Working, with nothing to add: bring your phone to Health Auto Export → Manual Sync → Export, and check Date Range covers a day that has data. |
| Nothing on the screen | Salud fetches 30 days by default — widen the range. A metric HAE has no samples for is skipped rather than shown as a zero. |
| Numbers that look doubled (steps, above all) | The export can carry the same walking twice — once from the Apple Watch and once from the iPhone. Apple Health de-duplicates that; [an export of individual samples does not](https://developer.apple.com/forums/thread/759709). A day total from HAE's own aggregation is the number the Health app shows, so `Time Grouping: Days` is the setting that keeps a card honest; grouping by Hours or Minutes reads the samples instead. |
| A card's number is bigger than the Health app's, by about 1.6–1.9× | Two devices recorded the same activity and the Health app de-duplicated it. Nothing to do: the hour keeps its sources apart and shows one of them, and the metric's sheet ends with `… · Apple Watch, iPhone · using Apple Watch` so the number can be checked against the device that produced it. A ratio that *grows* through the day instead means the payload carries running totals rather than per-hour values — see the row below. |
| A card's number is bigger than the Health app's (steps above all) | Open the metric's card: **What arrived last** lists how the last payload was stamped, from which sources, what it adds up to and its newest values. A run of *rising* numbers is the tell — those are running totals, not one hour each, and adding them inflates the day. The fix that covers both shapes is already in: a day total the phone declares is the day, and hours are only ever the shape of it (the sheet says so when the two differ). If the receipt shows **one total a day**, the export is already sending what the Health app shows. |
| A card shows one day, and its sheet has nothing before it | The automation's **Date Range** is `Today`, so yesterday was never sent. Set it to `Previous 7 Days`; the past cannot be invented server-side. |
| The detail sheet says "arrived as one total a day", with no hour list | **Time Grouping** is `Days`: the payload has one number per day and no hours to draw. Set it to `Hours` — the day stays the sum of its hours, so nothing is double-counted, and the hour list appears from the next sync on. |