Skip to content

fix: use configurable timezone for daily stats keys#1099

Open
Xio-Shark wants to merge 1 commit intohibiken:masterfrom
Xio-Shark:fix/timezone-daily-stats
Open

fix: use configurable timezone for daily stats keys#1099
Xio-Shark wants to merge 1 commit intohibiken:masterfrom
Xio-Shark:fix/timezone-daily-stats

Conversation

@Xio-Shark
Copy link
Copy Markdown

Fixes #671

Root Cause

ProcessedKey() and FailedKey() in internal/base/base.go called .UTC() before formatting the date string for Redis keys. For users in UTC+8 (Asia/Shanghai), tasks processed before 08:00 local time would fall on the previous UTC day, causing daily stats mismatch.

Changes

  • Removed .UTC() from ProcessedKey() / FailedKey() — the caller now passes time in the desired timezone
  • Added location *time.Location field to RDB struct (defaults to time.UTC for backward compatibility)
  • Added SetLocation() method on RDB for timezone configuration
  • Updated all stats-recording methods (Done, MarkAsComplete, Retry, Archive) to convert time to configured location
  • Updated CurrentStats and HistoricalStats query methods in inspect.go
  • Added timezone-specific test cases for Asia/Shanghai

Backward Compatibility

  • Default location is time.UTCno breaking change for existing users
  • Existing Redis keys remain valid
  • Users who configure Location will start generating timezone-aware stats keys

ProcessedKey() and FailedKey() in base.go previously hardcoded .UTC()
conversion, causing tasks processed before 8 AM in UTC+8 to be counted
towards the previous day's stats.

This change:
- Removes .UTC() from ProcessedKey/FailedKey (caller decides timezone)
- Adds location field to RDB struct (defaults to time.UTC)
- Adds SetLocation() method for timezone configuration
- Updates all stats-recording methods to use configured location
- Adds timezone-specific test cases for Asia/Shanghai
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

❌ Patch coverage is 76.47059% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.29%. Comparing base (4f00f52) to head (9a92c1f).
⚠️ Report is 36 commits behind head on master.

Files with missing lines Patch % Lines
internal/rdb/rdb.go 69.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1099      +/-   ##
==========================================
- Coverage   67.13%   63.29%   -3.85%     
==========================================
  Files          29       29              
  Lines        4300     4989     +689     
==========================================
+ Hits         2887     3158     +271     
- Misses       1135     1553     +418     
  Partials      278      278              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

// By default, RDB uses time.UTC. Setting this to a different location
// (e.g., time.Local, or a location loaded via time.LoadLocation("Asia/Shanghai"))
// will cause daily stats to be aggregated according to that timezone's date boundary.
func (r *RDB) SetLocation(loc *time.Location) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure how someone can use this because it is inside internal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] tasks before 8 AM are being counted towards the previous day

2 participants