Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(coach): weekly avg steps divides by days-with-data, not fixed 7 slots - #68

Merged
saksham2001 merged 3 commits into
saksham2001:mainfrom
rgvxsthi:fix/coach-weekly-avg-steps
Jul 12, 2026
Merged

saksham2001 merged 3 commits into
saksham2001:mainfrom
rgvxsthi:fix/coach-weekly-avg-steps

Conversation

@rgvxsthi

@rgvxsthi rgvxsthi commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Bug

CoachContextBuilder computes the week's average steps as weekSteps.reduce(0,+) / weekSteps.count. But summary.trends.steps7d is built (PulseServices.swift:330) as (0..<7).map { byDay[date]?.first ?? ActivityDaily(source: "none") }always 7 entries for the current Mon–Sun week, with missing days zero-filled. So the denominator is always 7, regardless of how many days actually have data.

Impact

The average is understated whenever the week isn't full of data (early in the week, sparse sync). Example: it's Monday, the user walked 8,000 steps, no other data → daysAvailable = 1, totalSteps = 8000, but avgSteps = 8000 / 7 = 1142. The coach is handed "average 1,142 steps/day" and reasons/advises on a wrong figure. It also contradicts the sibling daysAvailable and totalSteps fields in the same packet.

Fix

Divide by daysAvailable (days with steps > 0), guarding the zero case:

avgSteps: daysAvailable == 0 ? nil : weekSteps.reduce(0, +) / daysAvailable,

Now avgSteps, daysAvailable, and totalSteps are mutually consistent (average over days-with-data). One-line change; builds clean.

CoachContextBuilder built the week's average steps as total / weekSteps.count.
But steps7d is always 7 entries (current Mon-Sun week, missing days zero-filled
by PulseServices alignedRows), so the denominator was always 7 regardless of how
many days had data. Early in the week / with sparse sync this badly understates
the average fed to the coach — e.g. 8,000 steps on Monday reported as 1,142/day —
and contradicts the sibling daysAvailable / totalSteps fields.

Divide by daysAvailable (days with steps > 0) instead, guarding the zero case.
@rgvxsthi
rgvxsthi requested a review from saksham2001 as a code owner July 9, 2026 11:17
@saksham2001
saksham2001 merged commit 8353227 into saksham2001:main Jul 12, 2026
2 checks passed
@rgvxsthi
rgvxsthi deleted the fix/coach-weekly-avg-steps branch July 12, 2026 06:50
@rgvxsthi
rgvxsthi restored the fix/coach-weekly-avg-steps branch July 12, 2026 06:50
@rgvxsthi
rgvxsthi deleted the fix/coach-weekly-avg-steps branch July 12, 2026 09:15
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.

2 participants