Implement sending typical sleep to the watch#194
Draft
aveao wants to merge 11 commits into
Draft
Conversation
Adds file-private constants (bin count, bin width, history window, minimum-history threshold, unknown sentinel) and the weekday→key map that the upcoming typical-step writer will use. No behavior change yet.
Adds the helper signature and empty-input fast path. Subsequent commits will fill in the binning algorithm test-by-test.
Implements buildWeekdayTypicalsFromData: single-pass binning of HealthDataEntity rows into 7×96 (weekday × 15-min slot) aggregates, with per-slot day-count tracking so a slot that was uncovered on some matching days isn't underweighted. Weekdays below the 2-day minimum-history threshold are omitted; per-slot gaps emit the 0xFFFF sentinel the watch firmware sum-skips. Tests cover empty input, below-threshold skip, sentinel emission, and the per-slot-vs-per-day averaging distinction.
Wires computeAllWeekdayTypicalSteps into updateHealthStatsInDatabase so monday_steps..sunday_steps rows land in BlobDatabase.HealthStats alongside the existing *_movementData / *_sleepData rows. The @GenerateRoomEntity infrastructure on HealthStat pushes them to the watch on next sync. This fixes the missing percentage in the end-of-day activity summary notification on PebbleOS watches (firmware activity_insights.c prv_calc_percent_tier returns OnAverage when the typical sums to 0, and the corresponding copy variants have no "%d%%" placeholder).
Surfaces the seven <weekday>_steps typical totals computed by computeAllWeekdayTypicalSteps in the Health Debug Stats dialog so the values pushed to the watch can be sanity-checked from the phone. Each row sums the 96 15-min bins of that weekday's payload (matching the firmware's prv_cur_step_avg sum semantics), or shows "--" for weekdays below the 2-day minimum-history threshold. New decodeTypicalStepTotal helper in HealthStatsSync encapsulates the wire-format knowledge; HealthDebugStats gains a weekdayTypicalSteps field; Health.getHealthDebugStats and FakeLibPebble.getHealthDebugStats populate it; the iOS healthDebugStatsToJson includes it.
Adds the four-field data class and file-private constants (history weeks, min-history threshold, min-session-seconds filter, seconds-per-day) that the upcoming typical-sleep writer will use. No behavior change yet.
Adds the helper signature, empty-input fast path, and test fixtures (session, nightSleepMulti, nightSleep) that subsequent tests will use. Subsequent commits fill in the per-weekday reducer test-by-test.
Implements buildWeekdaySleepTypicalsFromData: filters sessions ≥30min, partitions by weekday, computes arithmetic-mean durations and circular-mean bedtime/wake (so 23:00+01:00 averages to ~00:00, not noon). Weekdays below the 2-day minimum-history threshold are omitted. Adds the secondsOfDay and circularMeanSecondsOfDay helpers. Tests cover: empty input, below-threshold skip, two-day basic case, nap-only filtering, the critical circular-mean midnight-wrap case, per-weekday independence, split-sleep last-session-wake, and mixed-validity within a single day.
Wires computeAllWeekdayTypicalSleep into updateHealthStatsInDatabase so the four typical_* fields of each <weekday>_sleepData blob (which have been hardcoded to 0u since the writer was first added) now carry real values: arithmetic-mean total/deep sleep durations and circular-mean bedtime/wake, computed over the user's past 7 weeks of same-weekday history. This populates the firmware fields read by health_db_get_typical_value (blob_db/health_db.c) and consumed by the sleep summary card (apps/system/health/data.c) and the activity_insights sleep notification path.
The existing daily 6-day loop deliberately skips today (incomplete current-day daily fields shouldn't overwrite the watch's accelerometer-tracked values), but the firmware reads `<today's weekday>_sleepData` typicals on the sleep summary card. Without this extra write the typicals for today's weekday stay stale for a day after the new typical-sleep code starts running. After the daily loop, add a typicals-only blob for today's weekday with last_processed_timestamp=0. Firmware's prv_notify_health_listeners gates the in-memory daily-metric update on a valid timestamp and bails out, so daily fields=0 don't corrupt today's tracked values, but health_db_insert still stores the blob — making the typicals readable via health_db_get_typical_value. This brings sleep typicals in line with the step typicals, which already cover all 7 weekdays via their separate per-weekday loop.
Mirrors the typical-steps debug section added on fix/typical-steps, showing the four values the watch reads from each <weekday>_sleepData blob's typical tail: duration / deep duration / bedtime → wakeup. Weekdays below the 2-day minimum-history threshold show "--". WeekdaySleepTypicals visibility bumped from internal to public so the new HealthDebugStats field can expose it across modules. The data class is just four Ints and was always going to be reachable by the debug dialog by design.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See #78
Draft as this builds on #192, that should be merged first