Skip to content

perf: optimize sleep session db storage calls#1466

Merged
CodeWithCJ merged 2 commits into
CodeWithCJ:mainfrom
apedley:sync-sleep-batches
Jun 7, 2026
Merged

perf: optimize sleep session db storage calls#1466
CodeWithCJ merged 2 commits into
CodeWithCJ:mainfrom
apedley:sync-sleep-batches

Conversation

@apedley

@apedley apedley commented Jun 7, 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?
Syncing large amounts of data will timeout due to a lot of db calls.

How did you implement the solution?
Sync smaller batches for sleep sessions and optimize db calls for sleep sync functions.

Linked Issue: Closes #1263

How to Test

  1. Sync a large amount of data

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 Jun 7, 2026
@github-actions

github-actions Bot commented Jun 7, 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 optimizes the synchronization of health data by separating sleep sessions from other session types. On the mobile client, sleep sessions are now chunked into smaller batches (SESSION_CHUNK_SIZE = 50) to prevent timeouts, while exercise and workout sessions remain grouped by source to support the server's range-delete behavior. On the server side, sleep session processing is optimized by pre-fetching and sharing the user profile and timezone context across the batch, eliminating redundant database queries. The reviewer suggested memoizing the context promise instead of the resolved value in getSleepContext to prevent potential race conditions and duplicate queries if the batch processing is refactored to run concurrently in the future.

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 SparkyFitnessServer/services/measurementService.ts
@apedley apedley force-pushed the sync-sleep-batches branch from b017d83 to d37770c Compare June 7, 2026 02:14
@CodeWithCJ CodeWithCJ merged commit e193512 into CodeWithCJ:main Jun 7, 2026
7 checks passed
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]: impossible to sync big amount of data

2 participants