feat: unify sleep reports and implement industry-standard "Wake-up Rule"#784
feat: unify sleep reports and implement industry-standard "Wake-up Rule"#784CodeWithCJ merged 3 commits intomainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request primarily enhances the user experience in the sleep analytics section by standardizing and improving the readability of sleep duration displays. It achieves this by refactoring existing time formatting utilities to present durations in a more intuitive "Hh Mm" format, ensuring consistency across the application. Additionally, a minor robustness improvement was made to a related component. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the time format in sleep charts to a more readable 'h m' format, which is a good user experience improvement. The changes involve replacing a local formatting function with a centralized utility function.
I've found a bug in the new formatMinutesToHHMM utility function that can lead to incorrect time displays like '1h 60m'. I've also noticed code duplication between formatMinutesToHHMM and formatSecondsToHHMM and suggested a refactoring to make the code more maintainable. Please see my comments for details.
| const absMinutes = Math.abs(totalMinutes); | ||
| const hours = Math.floor(absMinutes / 60); | ||
| const minutes = Math.round(absMinutes % 60); |
There was a problem hiding this comment.
There's a potential bug in this calculation. If totalMinutes is a value like 59.6, Math.round(absMinutes % 60) will result in 60, leading to an output like 0h 60m. The total minutes should be rounded before calculating hours and minutes to prevent this. The logic in formatSecondsToHHMM correctly handles this by rounding first.
| const absMinutes = Math.abs(totalMinutes); | |
| const hours = Math.floor(absMinutes / 60); | |
| const minutes = Math.round(absMinutes % 60); | |
| const roundedTotalMinutes = Math.round(Math.abs(totalMinutes)); | |
| const hours = Math.floor(roundedTotalMinutes / 60); | |
| const minutes = roundedTotalMinutes % 60; |
…ther than bedtime
…ule" - Attribution: Updated sleep entry logic to attribute data to the wake-up date, aligning with Apple Health and Fitbit standards. - Formatting: Unified all sleep durations across the app to use the consistent 7h 15m (or 15m) format. - Accuracy: Aligned legacy charts and tables to use your Personalized Sleep Need baseline instead of a hardcoded 8 hours. - Fixes: Resolved a date-shifting bug caused by UTC parsing and synchronized the manual entry editor with the saving logic to prevent 24-hour mismatches. - UI Consolidation: Cleaned up the Check-in page by organizing sleep stages and health metrics into a structured grid and fixing misleading title dates.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a significant and welcome change by aligning sleep entry attribution with the "wake-up rule", which is an industry standard. The changes are comprehensive, touching date parsing, frontend logic, backend services, and even a database migration to correct historical data. The unification of duration formatting and the use of personalized sleep need instead of hardcoded values are great improvements for consistency and accuracy.
My review has identified one critical issue in the database migration script that could lead to data corruption for certain sleep entries. I've also provided a minor suggestion to improve code conciseness in one of the frontend components. Overall, this is a solid pull request that makes important enhancements to the sleep tracking feature.
| UPDATE sleep_entries | ||
| SET entry_date = entry_date + 1; |
There was a problem hiding this comment.
This migration logic is too simplistic and may lead to incorrect data. The entry_date = entry_date + 1 statement assumes every sleep entry crosses midnight, which isn't always true (e.g., daytime naps or sleep sessions that start and end on the same calendar day). This will incorrectly shift the entry_date for such entries.
To correctly implement the "wake-up rule", the entry_date should be derived from the wake_time. A more robust approach would be to set the entry_date to the date part of the wake_time.
| UPDATE sleep_entries | |
| SET entry_date = entry_date + 1; | |
| UPDATE sleep_entries | |
| SET entry_date = DATE(wake_time); |
| {entryDetails.deepSleepSeconds !== undefined && | ||
| entryDetails.deepSleepSeconds !== null && ( |
There was a problem hiding this comment.
Description
Provide a brief summary of your changes.
Related Issue
PR type [x] Issue [ ] New Feature [ ] Documentation
Linked Issue: # #664
Checklist
Please check all that apply:
npm run lintandnpm run format(especially for Frontend).en) translation file (if applicable).rls_policies.sqlfor any new user-specific tables.Screenshots (if applicable)
Before
[Insert screenshot/GIF here]
After
[Insert screenshot/GIF here]