Skip to content

Fix timezone handling in quantised activity - #1659

Open
skyfallwastaken wants to merge 1 commit into
mainfrom
fix/quantized-activity-timezone-20260901022447
Open

skyfallwastaken wants to merge 1 commit into
mainfrom
fix/quantized-activity-timezone-20260901022447

Conversation

@skyfallwastaken

Copy link
Copy Markdown
Member

Summary of the problem

The quantised activity endpoint used UTC month bounds and day buckets for every user. This misplaced activity near local month boundaries and produced incorrect calendar days across daylight saving transitions.

Describe your changes

Use the target user's validated timezone for local month bounds, SQL day grouping and quantisation. Group returned points by the same local date and build the response by calendar date while preserving the existing duration cap and response shape.

Screenshots / Media

Not applicable. This changes the JSON activity calculation only.

Copilot AI lite review requested due to automatic review settings September 1, 2026 02:36
@greptile-apps

greptile-apps Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR makes quantized admin activity use the target user’s timezone consistently for month boundaries, daily SQL grouping, point grouping, and returned day timestamps.

  • Uses half-open local-month epoch bounds to exclude adjacent-month activity.
  • Groups heartbeat points and duration totals by local calendar date.
  • Builds response days from calendar dates, preserving correct 23- and 25-hour DST boundaries.
  • Adds an integration test covering month boundaries and the spring DST transition.
  • Updates the API documentation to define day timestamps as local-day boundaries.

Confidence Score: 5/5

The PR appears safe to merge with no actionable correctness or security failures identified.

The controller now uses one validated timezone across query bounds, SQL grouping, Ruby grouping, and response generation, while the added test exercises the principal month-boundary and DST behavior.

Important Files Changed

Filename Overview
app/controllers/api/admin/v1/admin_controller.rb Consistently applies the validated target-user timezone to month bounds, SQL day grouping, point grouping, and calendar-day response construction.
test/controllers/api/admin/v1/admin_controller_test.rb Adds coverage for local month exclusions, local-midnight grouping, spring DST day length, point placement, and duration totals.
spec/requests/api/admin/v1/admin_misc_spec.rb Updates the documented response-field description to reflect local-day timestamps.
swagger/admin/swagger.yaml Regenerates the admin API documentation with the revised local-day timestamp contract.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Requested year and month] --> B[Resolve target user timezone]
  B --> C[Compute local month epoch bounds]
  C --> D[Select active heartbeats]
  D --> E[Convert timestamps to local time in SQL]
  E --> F[Group and quantize by local day]
  E --> G[Calculate capped daily durations]
  F --> H[Group returned points by local Date]
  G --> I[Build each calendar day response]
  H --> I
Loading

Reviews (1): Last reviewed commit: "Fix quantized activity time zones" | Re-trigger Greptile

Copilot AI 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.

Pull request overview

This PR fixes the admin “quantized visualization” endpoint so month bounds, day bucketing, and returned date_timestamp_s values are computed in the target user’s timezone, preventing mis-bucketed activity near local month/day boundaries and across DST transitions.

Changes:

  • Compute month start/end epochs from the user’s timezone and build the response days by local calendar Date rather than stepping UTC seconds.
  • Bucket/quantize heartbeats in SQL using local-time day boundaries and group returned points by the same local date in Ruby.
  • Update OpenAPI/Rswag descriptions and add a regression test covering DST + month-boundary behaviour.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
app/controllers/api/admin/v1/admin_controller.rb Switches quantized visualization month bounds, SQL grouping, and response assembly to the target user’s timezone/local dates.
test/controllers/api/admin/v1/admin_controller_test.rb Adds an integration test asserting correct local-day bucketing across DST and month boundaries.
swagger/admin/swagger.yaml Updates the OpenAPI description for date_timestamp_s to reflect user-local start-of-day epoch semantics.
spec/requests/api/admin/v1/admin_misc_spec.rb Updates the Rswag schema description for date_timestamp_s to match the new semantics.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 43 to +47
begin
start_epoch = Time.utc(year, month, 1).to_i
end_epoch = month == 12 ? Time.utc(year + 1, 1, 1).to_i : Time.utc(year, month + 1, 1).to_i
timezone = Time.find_zone!(user.timezone)
start_date = Date.new(year, month, 1)
end_date = start_date.next_month
start_epoch = timezone.local(start_date.year, start_date.month, start_date.day).to_i

This branch has not been deployed

No deployments
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