Skip to content

fix(import): store wearable and Mi Band efficiency as a 0-1 fraction - #2941

Open
matthiasthomas wants to merge 1 commit into
ryanbr:mainfrom
matthiasthomas:fix/imported-efficiency-fraction
Open

matthiasthomas wants to merge 1 commit into
ryanbr:mainfrom
matthiasthomas:fix/imported-efficiency-fraction

Conversation

@matthiasthomas

Copy link
Copy Markdown

What this PR does

dailyMetric.efficiency and sleepSession.efficiency hold the 0–1 fraction the native pipeline writes, and the WHOOP CSV and Oura cloud importers convert to it (#376). The Oura-export, Fitbit, Garmin and Mi Band importers still wrote a percentage.

Effect: Rest clamps efficiency to 0–1, so an imported night scored a perfect efficiency component: 1.6 points too high at 92%. Charge takes Rest as its sleep-quality term. In practice this reaches scored nights mainly through Oura and Garmin exports, because scoring needs HRV.

Fix: both platforms convert at the write boundary, as the WHOOP importer does, and compute the asleep ÷ in-bed fallback as a fraction. Parsed values stay verbatim. The demo seeders write the same scale.

Readers: two places printed the raw column and would have shown "1%": the Mi Band card, and Today's Rest caption when a night has an efficiency but no duration. Both now read a fraction, and a percentage from a night stored before this change. The other readers I checked already accept both scales.

Not included: rows already stored are not migrated or re-scored. Re-importing the export rewrites them. A repeat of the > 1.5 heal would need a new GRDB and Room version, so it is left to a separate decision.

Type of change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Documentation
  • CI / tooling

How it was tested

  • New tests: ImportedEfficiencyUnitTests (Swift) and ImportedEfficiencyUnitTest (Kotlin) run the same synthetic Oura and Fitbit exports through each importer's store step and expect the same literals, including a Rest value that no longer saturates. They also cover the Mi Band helpers, and on Apple the Mi Band card.
  • Without the fix: every new test fails on both platforms. With it all pass.
  • Full StrandTests (macOS): 2,273 tests. The only failures are the two timezone tests fix(dates): show day-keyed dates on their own day in every time zone #2723 fixes; they also fail without this change. NOOPiOS builds.
  • Full testFullDebugUnitTest: 6,642 tests, 2 failures, both in RecoveryDriversTest, which also fail locally on the unmodified base.
  • Checks: doc-comment lint, i18n audit, parity ledger and ratchet pass.
  • Not verified: not tried in a running app or on a real export. Garmin has no fixture of its own; it shares the fallback path. No BLE path is touched.

Checklist

  • Swift package tests pass for any package I touched (swift test in Packages/<name>): n/a, no package touched
  • Android unit tests pass if I touched android/: all but the 2 RecoveryDriversTest failures above
  • No new build warnings introduced
  • UI changes use only StrandDesign tokens: n/a, no styling change
  • No hardcoded hex frame bytes; protocol facts live in the schema / decoders
  • Follows the conventions in docs/CONTRIBUTING.md
  • I did not commit generated output (Strand.xcodeproj/) or any secrets/keystores

Related issues

Refs #376

🤖 Generated with Claude Code

dailyMetric.efficiency and sleepSession.efficiency hold the 0-1 fraction the
native pipeline writes, and the WHOOP CSV and Oura cloud importers convert
to it. The Oura-export, Fitbit, Garmin and Mi Band importers still wrote a
percentage. Rest clamps efficiency to 0-1, so an imported night scored a
perfect efficiency component: 1.6 points too high at 92%, carried into
Charge through its sleep-quality term.

Both platforms now convert at the write boundary, as the WHOOP importer
does, and compute the fallback as a fraction. Parsed values stay verbatim.
The demo seeders write the same scale.

Two readers printed the raw column: the Mi Band card, and Today's Rest
caption when a night has an efficiency but no duration. They now read a
fraction, and a percentage from a night stored before this change.

Rows already stored are not migrated or re-scored here; re-importing the
export rewrites them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant