mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
fix(hrv): file Fitbit and Google Health RMSSD as HRV_RMSSD
Fitbit's `dailyRmssd` and Google Health's daily HRV average are both RMSSD, but the mappers wrote them into HEART_RATE_VARIABILITY, the SDNN series. The `hrv` ladder ranks both above Apple Health, so an account with an Apple Watch and a Fitbit had a series that switched estimator from day to day. The mappers now write HRV_RMSSD, and the `hrvRmssd` ladder carries both sources where they sat on the SDNN one. Migration 0420 moves the rows already written (tombstones included, with a sync-version bump so the change reaches the phone), drops their SDNN rollup buckets and the RMSSD buckets of every account whose rows moved, so the boot-time rollup discovery folds those accounts again, and deletes the SDNN personal records those rows had set. One DO block, so the move and its cleanup land together.
This commit is contained in:
1 parent
d21d29572d
commit
c5d2053e5a
11 files changed
+393
-43
No files matched your search
@@ -332,13 +332,13 @@ describe("metric mappers (classic shapes)", () => {
|
||||
expect(out[0]!.fieldTag).toBe("2026-05-10:spo2");
|
||||
});
|
||||
|
||||
it("maps HRV dailyRmssd into the canonical HEART_RATE_VARIABILITY slot", () => {
|
||||
it("maps HRV dailyRmssd into HRV_RMSSD, apart from the SDNN series", () => {
|
||||
const out = mapHeartRateVariability({
|
||||
hrv: [
|
||||
{ dateTime: "2026-05-10", value: { dailyRmssd: 42.7, deepRmssd: 50 } },
|
||||
],
|
||||
});
|
||||
expect(out[0]!.type).toBe("HEART_RATE_VARIABILITY");
|
||||
expect(out[0]!.type).toBe("HRV_RMSSD");
|
||||
expect(out[0]).toMatchObject({ value: 42.7, unit: "ms" });
|
||||
expect(out[0]!.fieldTag).toBe("2026-05-10:hrv");
|
||||
});
|
||||
|
||||
@@ -741,15 +741,16 @@ export function mapHeartRateVariability(
|
||||
body: unknown,
|
||||
tz?: string,
|
||||
): FitbitMappedMeasurement[] {
|
||||
// Fitbit's HRV summary is an RMSSD estimator (`value.dailyRmssd`). It lands in
|
||||
// the canonical `HEART_RATE_VARIABILITY` slot that FITBIT occupies in the
|
||||
// source-priority `hrv` ladder (alongside Apple / Oura), keeping cross-source
|
||||
// comparison on one axis. (`HRV_RMSSD` is reserved for the WHOOP-native slot.)
|
||||
// Fitbit's HRV summary is an RMSSD estimator (`value.dailyRmssd`), so it lands
|
||||
// in `HRV_RMSSD` beside WHOOP / Oura / Polar. `HEART_RATE_VARIABILITY` is the
|
||||
// SDNN series; the two estimators are not interchangeable, and a Fitbit night
|
||||
// ranked into the SDNN ladder used to splice RMSSD values into an Apple
|
||||
// Watch SDNN series day by day.
|
||||
const rows = readArray(body, "hrv") as FitbitDailyRow[];
|
||||
return mapDailySummary(
|
||||
rows,
|
||||
{
|
||||
type: "HEART_RATE_VARIABILITY",
|
||||
type: "HRV_RMSSD",
|
||||
unit: "ms",
|
||||
fieldTag: "hrv",
|
||||
leaves: ["dailyRmssd"],
|
||||
@@ -987,9 +988,9 @@ export const FITBIT_FIELD_MAP: Record<
|
||||
note: "spo2 summary value.avg",
|
||||
},
|
||||
heartRateVariability: {
|
||||
type: "HEART_RATE_VARIABILITY",
|
||||
type: "HRV_RMSSD",
|
||||
unit: "ms",
|
||||
note: "hrv summary value.dailyRmssd (RMSSD estimator); canonical HRV slot",
|
||||
note: "hrv summary value.dailyRmssd (RMSSD estimator), apart from SDNN",
|
||||
},
|
||||
restingHeartRate: {
|
||||
type: "RESTING_HEART_RATE",
|
||||
|
||||
@@ -26,14 +26,14 @@ metric (kg / km); the US default is imperial.
|
||||
Scope: `weight`, `oxygen_saturation`, `heartrate`, `respiratory_rate`. Verified
|
||||
against the dev.fitbit.com reference (response shapes quoted there).
|
||||
|
||||
| Endpoint | MeasurementType | Unit | Source field | Grain | Note |
|
||||
| ---------------------------------------------- | ------------------------ | ----------- | ------------------------------------------- | ----- | ----------------------------------------------------------- |
|
||||
| `/1/user/-/body/log/weight/date/{s}/{e}.json` | `WEIGHT` | kg | `weight[].weight` | log | externalId anchors on `logId`. Picker ranks Withings above. |
|
||||
| `/1/user/-/body/log/fat/date/{s}/{e}.json` | `BODY_FAT` | % | `fat[].fat` | log | externalId anchors on `logId`. |
|
||||
| `/1/user/-/spo2/date/{s}/{e}.json` | `OXYGEN_SATURATION` | % | `[].value.avg` (bare array) | daily | day-keyed externalId. |
|
||||
| `/1/user/-/hrv/date/{s}/{e}.json` | `HEART_RATE_VARIABILITY` | ms | `hrv[].value.dailyRmssd` | daily | RMSSD estimator → canonical HRV slot (not `HRV_RMSSD`). |
|
||||
| `/1/user/-/activities/heart/date/{s}/{e}.json` | `RESTING_HEART_RATE` | bpm | `activities-heart[].value.restingHeartRate` | daily | rows with no resting HR for the day are skipped. |
|
||||
| `/1/user/-/br/date/{s}/{e}.json` | `RESPIRATORY_RATE` | breaths/min | `br[].value.breathingRate` | daily | |
|
||||
| Endpoint | MeasurementType | Unit | Source field | Grain | Note |
|
||||
| ---------------------------------------------- | -------------------- | ----------- | ------------------------------------------- | ----- | ------------------------------------------------------------------- |
|
||||
| `/1/user/-/body/log/weight/date/{s}/{e}.json` | `WEIGHT` | kg | `weight[].weight` | log | externalId anchors on `logId`. Picker ranks Withings above. |
|
||||
| `/1/user/-/body/log/fat/date/{s}/{e}.json` | `BODY_FAT` | % | `fat[].fat` | log | externalId anchors on `logId`. |
|
||||
| `/1/user/-/spo2/date/{s}/{e}.json` | `OXYGEN_SATURATION` | % | `[].value.avg` (bare array) | daily | day-keyed externalId. |
|
||||
| `/1/user/-/hrv/date/{s}/{e}.json` | `HRV_RMSSD` | ms | `hrv[].value.dailyRmssd` | daily | RMSSD estimator, kept apart from the SDNN `HEART_RATE_VARIABILITY`. |
|
||||
| `/1/user/-/activities/heart/date/{s}/{e}.json` | `RESTING_HEART_RATE` | bpm | `activities-heart[].value.restingHeartRate` | daily | rows with no resting HR for the day are skipped. |
|
||||
| `/1/user/-/br/date/{s}/{e}.json` | `RESPIRATORY_RATE` | breaths/min | `br[].value.breathingRate` | daily | |
|
||||
|
||||
Each daily-summary value passes a finite + strictly-positive guard (a 0/NaN is a
|
||||
garbage/empty reading and is dropped). Each endpoint caps its own date range
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* - body/log/weight → WEIGHT (kg)
|
||||
* - body/log/fat → BODY_FAT (%)
|
||||
* - spo2 summary → OXYGEN_SATURATION (%)
|
||||
* - hrv summary → HEART_RATE_VARIABILITY (ms, dailyRmssd)
|
||||
* - hrv summary → HRV_RMSSD (ms, dailyRmssd)
|
||||
* - activities/heart → RESTING_HEART_RATE (bpm)
|
||||
* - br summary → RESPIRATORY_RATE (breaths/min)
|
||||
*
|
||||
|
||||
@@ -148,7 +148,7 @@ describe("mapOxygenSaturation — daily-oxygen-saturation summary", () => {
|
||||
});
|
||||
|
||||
describe("mapHeartRateVariability — daily-heart-rate-variability summary", () => {
|
||||
it("maps averageHeartRateVariabilityMilliseconds to the HRV slot in ms", () => {
|
||||
it("maps the RMSSD daily average to HRV_RMSSD in ms", () => {
|
||||
const rows = mapHeartRateVariability({
|
||||
dailyHeartRateVariability: {
|
||||
averageHeartRateVariabilityMilliseconds: 42.5,
|
||||
@@ -157,7 +157,7 @@ describe("mapHeartRateVariability — daily-heart-rate-variability summary", ()
|
||||
});
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]).toMatchObject({
|
||||
type: "HEART_RATE_VARIABILITY",
|
||||
type: "HRV_RMSSD",
|
||||
unit: "ms",
|
||||
value: 42.5,
|
||||
fieldTag: "2026-06-01:hrv",
|
||||
|
||||
@@ -511,16 +511,16 @@ export function mapHeartRateVariability(
|
||||
tz?: string,
|
||||
): GoogleHealthMappedMeasurement[] {
|
||||
const dt = GOOGLE_HEALTH_DATA_TYPES.heartRateVariability;
|
||||
// The daily field is an unlabelled "average HRV ms". Per the design decision
|
||||
// it lands in the SDNN-lineage `HEART_RATE_VARIABILITY` slot
|
||||
// (Apple-comparable), NOT WHOOP's `HRV_RMSSD` (reserved for the WHOOP-native
|
||||
// estimator). The per-sample type carries explicit RMSSD + SDNN fields —
|
||||
// re-confirm the estimator against live data and revisit if warranted.
|
||||
// Google documents the daily average as RMSSD ("average heart rate
|
||||
// variability calculated using the root mean square of successive
|
||||
// differences"), the same estimator the classic Fitbit API reports as
|
||||
// `dailyRmssd`. It lands in `HRV_RMSSD`, apart from the SDNN
|
||||
// `HEART_RATE_VARIABILITY` series.
|
||||
return mapSimple(
|
||||
point,
|
||||
dt,
|
||||
{
|
||||
type: "HEART_RATE_VARIABILITY",
|
||||
type: "HRV_RMSSD",
|
||||
unit: "ms",
|
||||
fieldTag: "hrv",
|
||||
valuePaths: [`${dt.key}.averageHeartRateVariabilityMilliseconds`],
|
||||
|
||||
@@ -81,19 +81,19 @@ The exercise civil bound is derived from the watermark in the USER'S zone.
|
||||
|
||||
Scope: `googlehealth.health_metrics_and_measurements.readonly`.
|
||||
|
||||
| Data type (path) | Payload value read | MeasurementType | Unit | fieldTag | Grain | Note |
|
||||
| ------------------------------------- | --------------------------------------------------------------------- | ------------------------ | ----------- | ------------ | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `weight` | `weight.weightGrams` ÷ 1000 | `WEIGHT` | kg | `weight` | sample | Grams on the wire. Picker ranks a real Withings scale above Google Health. |
|
||||
| `body-fat` | `bodyFat.percentage` | `BODY_FAT` | % | `body_fat` | sample | Union key is camelCase `bodyFat`. |
|
||||
| `daily-oxygen-saturation` | `dailyOxygenSaturation.averagePercentage` | `OXYGEN_SATURATION` | % | `spo2` | daily | The bare `oxygen-saturation` type is per-SAMPLE and rejects a `.date` filter. |
|
||||
| `daily-heart-rate-variability` | `dailyHeartRateVariability.averageHeartRateVariabilityMilliseconds` | `HEART_RATE_VARIABILITY` | ms | `hrv` | daily | **Decision:** SDNN slot (Apple-comparable), NOT `HRV_RMSSD`. The daily field is an unlabelled "average HRV ms" — re-confirm estimator live. |
|
||||
| `daily-resting-heart-rate` | `dailyRestingHeartRate.beatsPerMinute` (int64 string) | `RESTING_HEART_RATE` | bpm | `rhr` | daily | |
|
||||
| `daily-respiratory-rate` | `dailyRespiratoryRate.breathsPerMinute` (number) | `RESPIRATORY_RATE` | breaths/min | `resp_rate` | daily | `respiratory-rate` does NOT exist in the catalogue. Schema is `{date, breathsPerMinute}` — the earlier `dailyRespiratoryRateBpm` leaf never existed. |
|
||||
| `heart-rate` | `heartRate.beatsPerMinute` (int64 string) | `PULSE` | bpm | `hr` | sample | Intraday spot HR; per-minute volume — consider the 14-day-max rollup if PULSE should stay coarse. |
|
||||
| `blood-glucose` | `bloodGlucose.bloodGlucoseMilligramsPerDeciliter` (number) | `BLOOD_GLUCOSE` | mg/dL | `glucose` | sample | Already in the canonical mg/dL storage unit. Meal/timing/specimen enums not stored. |
|
||||
| `core-body-temperature` | `coreBodyTemperature.temperatureCelsius` (number) | `BODY_TEMPERATURE` | celsius | `core_temp` | sample | Core slot — distinct from SKIN_TEMPERATURE / WRIST_TEMPERATURE. |
|
||||
| `daily-sleep-temperature-derivations` | `dailySleepTemperatureDerivations.nightlyTemperatureCelsius` (number) | `WRIST_TEMPERATURE` | celsius | `wrist_temp` | daily | ABSOLUTE nightly skin temperature (mean over the night's samples) — the schema refutes the earlier "deviation-only" premise. Baseline/stddev derivations not stored. |
|
||||
| `height` | `height.heightMillimeters` (int64 string) ÷ 10 | `User.heightCm` | cm | — | sample | Profile seed — written ONLY when null; never a Measurement. List order is DESCENDING → the seed picks max(sampleTime) explicitly. The earlier `heightMeters` leaf never existed. |
|
||||
| Data type (path) | Payload value read | MeasurementType | Unit | fieldTag | Grain | Note |
|
||||
| ------------------------------------- | --------------------------------------------------------------------- | -------------------- | ----------- | ------------ | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `weight` | `weight.weightGrams` ÷ 1000 | `WEIGHT` | kg | `weight` | sample | Grams on the wire. Picker ranks a real Withings scale above Google Health. |
|
||||
| `body-fat` | `bodyFat.percentage` | `BODY_FAT` | % | `body_fat` | sample | Union key is camelCase `bodyFat`. |
|
||||
| `daily-oxygen-saturation` | `dailyOxygenSaturation.averagePercentage` | `OXYGEN_SATURATION` | % | `spo2` | daily | The bare `oxygen-saturation` type is per-SAMPLE and rejects a `.date` filter. |
|
||||
| `daily-heart-rate-variability` | `dailyHeartRateVariability.averageHeartRateVariabilityMilliseconds` | `HRV_RMSSD` | ms | `hrv` | daily | Google documents the daily average as RMSSD, so it lands in `HRV_RMSSD`, apart from the SDNN `HEART_RATE_VARIABILITY`. |
|
||||
| `daily-resting-heart-rate` | `dailyRestingHeartRate.beatsPerMinute` (int64 string) | `RESTING_HEART_RATE` | bpm | `rhr` | daily | |
|
||||
| `daily-respiratory-rate` | `dailyRespiratoryRate.breathsPerMinute` (number) | `RESPIRATORY_RATE` | breaths/min | `resp_rate` | daily | `respiratory-rate` does NOT exist in the catalogue. Schema is `{date, breathsPerMinute}` — the earlier `dailyRespiratoryRateBpm` leaf never existed. |
|
||||
| `heart-rate` | `heartRate.beatsPerMinute` (int64 string) | `PULSE` | bpm | `hr` | sample | Intraday spot HR; per-minute volume — consider the 14-day-max rollup if PULSE should stay coarse. |
|
||||
| `blood-glucose` | `bloodGlucose.bloodGlucoseMilligramsPerDeciliter` (number) | `BLOOD_GLUCOSE` | mg/dL | `glucose` | sample | Already in the canonical mg/dL storage unit. Meal/timing/specimen enums not stored. |
|
||||
| `core-body-temperature` | `coreBodyTemperature.temperatureCelsius` (number) | `BODY_TEMPERATURE` | celsius | `core_temp` | sample | Core slot — distinct from SKIN_TEMPERATURE / WRIST_TEMPERATURE. |
|
||||
| `daily-sleep-temperature-derivations` | `dailySleepTemperatureDerivations.nightlyTemperatureCelsius` (number) | `WRIST_TEMPERATURE` | celsius | `wrist_temp` | daily | ABSOLUTE nightly skin temperature (mean over the night's samples) — the schema refutes the earlier "deviation-only" premise. Baseline/stddev derivations not stored. |
|
||||
| `height` | `height.heightMillimeters` (int64 string) ÷ 10 | `User.heightCm` | cm | — | sample | Profile seed — written ONLY when null; never a Measurement. List order is DESCENDING → the seed picks max(sampleTime) explicitly. The earlier `heightMeters` leaf never existed. |
|
||||
|
||||
Daily-summary `date` fields are `{year,month,day}` objects, anchored at
|
||||
UTC-midday so a timezone shift can't roll the civil day. Every value passes a
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* - weight → WEIGHT
|
||||
* - body-fat → BODY_FAT
|
||||
* - daily-oxygen-saturation → OXYGEN_SATURATION
|
||||
* - daily-heart-rate-variability → HEART_RATE_VARIABILITY (SDNN slot; see mapper)
|
||||
* - daily-heart-rate-variability → HRV_RMSSD (RMSSD; see mapper)
|
||||
* - daily-resting-heart-rate → RESTING_HEART_RATE
|
||||
* - daily-respiratory-rate → RESPIRATORY_RATE
|
||||
* - heart-rate (intraday) → PULSE
|
||||
@@ -117,7 +117,7 @@ const METRIC_RESOURCES: MetricResource[] = [
|
||||
dataType: GOOGLE_HEALTH_DATA_TYPES.heartRateVariability,
|
||||
map: mapHeartRateVariability,
|
||||
verb: "fetchHrv",
|
||||
type: "HEART_RATE_VARIABILITY",
|
||||
type: "HRV_RMSSD",
|
||||
},
|
||||
{
|
||||
dataType: GOOGLE_HEALTH_DATA_TYPES.restingHeartRate,
|
||||
|
||||
@@ -404,8 +404,17 @@ export const DEFAULT_SOURCE_PRIORITY: Required<MetricPriority> = {
|
||||
// on now. Mirrors the recovery ladder's native-above-computed ordering.
|
||||
sleepDebt: ["WHOOP", "OURA", "POLAR", "COMPUTED"],
|
||||
// v1.42 (#1110) — the straps and rings that measure RMSSD overnight lead;
|
||||
// the HealthKit relay of a third-party reading is the fallback.
|
||||
hrvRmssd: ["WHOOP", "OURA", "POLAR", "APPLE_HEALTH"],
|
||||
// the HealthKit relay of a third-party reading is the fallback. Fitbit and
|
||||
// Google Health report RMSSD too and take the places they hold on the SDNN
|
||||
// ladder they used to be filed under.
|
||||
hrvRmssd: [
|
||||
"WHOOP",
|
||||
"FITBIT",
|
||||
"GOOGLE_HEALTH",
|
||||
"OURA",
|
||||
"POLAR",
|
||||
"APPLE_HEALTH",
|
||||
],
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user