Skip to content

fix: resolve sleep stage overlap and correct time asleep#1722

Merged
CodeWithCJ merged 1 commit into
CodeWithCJ:mainfrom
apedley:fix-ios-sleep-dupe
Jul 4, 2026
Merged

fix: resolve sleep stage overlap and correct time asleep#1722
CodeWithCJ merged 1 commit into
CodeWithCJ:mainfrom
apedley:fix-ios-sleep-dupe

Conversation

@apedley

@apedley apedley commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Tip

Help us review and merge your PR faster!
Please ensure you have completed the Checklist below.
For Frontend changes, please run pnpm run validate to check for any errors.
PRs that include tests and clear screenshots are highly preferred!
Note: AI-generated descriptions must be manually edited for conciseness. Do not paste raw AI summaries.

Description

What problem does this PR solve?
Multiple sources of sleep data were causing duplicate data when syncing through healthkit.

How did you implement the solution?
Merge the overlapping samples that were duplicates. When two sources disagree, the more specific stage wins. Existing data will need to be resync'ed.

Linked Issue: Closes #1379

How to Test

  1. Sync sleep data
  2. See no overlap

PR Type

  • Issue (bug fix)
  • New Feature
  • Refactor
  • Documentation

Checklist

All PRs:

  • [MANDATORY - ALL] Integrity & License: I certify this is my own work, free of malicious code, and I agree to the License terms.

New features only:

  • [MANDATORY for new feature] Alignment: I have raised a GitHub issue and it was reviewed/approved by maintainers or it was approved on Discord.

Frontend changes (SparkyFitnessFrontend/):

  • [MANDATORY for Frontend changes] Quality: I have run pnpm run validate and it passes.
  • [MANDATORY for Frontend changes] Translations: I have only updated the English (en) translation file.

Backend changes (SparkyFitnessServer/):

  • [MANDATORY for Backend changes] Code Quality: I have run typecheck, lint, and tests. New files use TypeScript, new endpoints have Zod schemas, and new endpoints include tests.
  • [MANDATORY for Backend changes] Database Security: I have updated rls_policies.sql for any new user-specific tables.

UI changes (components, screens, pages):

  • [MANDATORY for UI changes] Screenshots: I have attached Before/After screenshots below.

Mobile changes (SparkyFitnessMobile/):

  • [MANDATORY for Mobile changes] Tested on device or emulator: I have verified the changes work on iOS or Android.

Screenshots

Click to expand

Before

before

After

after

Notes for Reviewers

Optional — use this for anything that doesn't fit above: known tradeoffs, areas you'd like specific feedback on, qustions you have or context that helps reviewers.

@github-actions github-actions Bot added backend bug Something isn't working mobile labels Jul 4, 2026
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

PR Validation Results

Change Detection

  • ⚙️ Backend changes detected
  • 📱 Mobile changes detected

✅ All checks passed. Thank you!

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses issue #1379 by implementing a sweep-line flattening algorithm on the mobile client to resolve overlapping sleep data sources into a single, non-overlapping timeline based on stage priority. It also updates the server-side sleep duration and 'time asleep' calculations to consistently count only deep, light, and rem stages, excluding awake, in-bed, and unknown stages. Feedback was provided to add defensive null/undefined checks in the server's sumAsleepSeconds function to prevent TypeErrors, and to use shared workspace time utilities for date parsing and validation on the mobile client.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread SparkyFitnessServer/services/measurementService.ts
Comment thread SparkyFitnessMobile/src/services/healthkit/dataAggregation.ts
@CodeWithCJ CodeWithCJ merged commit fc0c429 into CodeWithCJ:main Jul 4, 2026
9 checks passed
@apedley apedley deleted the fix-ios-sleep-dupe branch July 5, 2026 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working mobile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Apple Health 'Core' (light) sleep overlaps with REM/Deep, inflating total sleep time and stage breakdowns

2 participants