Fix steps trends: bucket daily aggregate by local day and surface today's live total#231
Merged
Merged
Conversation
…ay live
The biometrics_daily continuous aggregate bucketed samples with
time_bucket('1 day', ts), which aligns to UTC midnight regardless of session
timezone, while the rest of the app defines a calendar day by SERVER_TIMEZONE.
For users in a negative-offset zone this split a day's steps across two UTC
buckets, so daily totals and the multi-day average diverged from Apple
Health's local-day numbers, and the right-most chart point reflected a UTC
day straddling the user's yesterday/today boundary.
- Add migration 0030 recreating biometrics_daily with the timezone-aware
time_bucket('1 day', ts, SERVER_TIMEZONE) so buckets align to local midnight,
and fully refresh so existing history re-buckets correctly.
- In the trends endpoint, extract the bucket date in SERVER_TIMEZONE and always
recompute today from raw biometrics, replacing a stale/partial aggregate
bucket so the latest point and headline show today's live local-day total.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133D6GfA6XvnH3oqXUBBBtP
…egate helper
Two remaining timezone gaps surfaced by the day-boundary audit:
- ldl_simulate / protein_simulate counted distinct days for their 7-day
averaging divisor via e.ts.date() on UTC-aware meal timestamps, so meals
near local midnight landed in the wrong day and could skew the divisor by
±1. Convert to SERVER_TIMEZONE before taking the date, matching the rest of
the day-boundary contract.
- Remove the unused create_continuous_aggregates() helper in db/timescale.py.
It defined biometrics_daily with a UTC time_bucket('1 day', ts) and was
never called (migrations use inline SQL); leaving it risked silently
reintroducing the UTC-bucketing bug fixed in migration 0030.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0133D6GfA6XvnH3oqXUBBBtP
Importing luma.config in the migration instantiated Settings() at Alembic load time, which requires app secrets (jwt_secret) absent in the migrations CI job, so `alembic upgrade head` failed with a pydantic ValidationError. Read the SERVER_TIMEZONE env var directly (the same value pydantic reads, defaulting to UTC) and validate it via ZoneInfo, matching how env.py reads DATABASE_URL. Also resolves the ruff I001 import-order failure. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0133D6GfA6XvnH3oqXUBBBtP
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.
Problem
Reported symptoms:
Root cause
The
biometrics_dailyTimescaleDB continuous aggregate bucketed samples withtime_bucket('1 day', ts). On atimestamptzcolumn with no timezone argument, buckets align to UTC midnight regardless of session timezone — but everywhere else in the app (/today, streaks, the Trends "live today" supplement) a calendar day is defined bySERVER_TIMEZONE(PR #219).For a user in a negative-offset zone, this splits each local day's step samples across two UTC buckets:
Changes
0030_biometrics_daily_local_tz.py— recreate the continuous aggregate with the timezone-awaretime_bucket('1 day', ts, SERVER_TIMEZONE)overload so buckets align to local midnight, and fully refresh so existing history re-buckets correctly. (The timezone is embedded as a literal because a continuous aggregate definition can't reference runtime params; it's read from validated config.)trends.py— extract the bucket date inSERVER_TIMEZONE(day AT TIME ZONE :tz), and always recompute today from rawbiometrics, replacing a stale/partial aggregate bucket (not just appending) so the latest point and headline always show today's live local-day total.This fix is only fully correct when
SERVER_TIMEZONEis set to the user's actual zone (e.g.America/Denver). It currently defaults toUTCinconfig.py. If the deployment is still onUTCwhile the user is on US time, the historical buckets and today's boundary will still be off — please confirm/setSERVER_TIMEZONEin the environment.Testing notes
No DB/deps available in this session, so the aggregate refresh and endpoint couldn't be exercised here. Recommend verifying after deploy: run
alembic upgrade head, then confirm/trends/stepsreturns today's local-day total as the last series point and that historical daily sums match Apple Health.🤖 Generated with Claude Code
Generated by Claude Code