Repository navigation
fix(tracearr): use the device's timezone for today's stats and the weekly activity #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
services/service_tracearr/lib/src/overview/daily_plays.dart
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import '../models/tracearr_models.dart'; | ||
|
|
||
| /// Tracearr's weekly plays as seven local calendar days, oldest first, | ||
| /// ending on the day the range ends (or [now] when there is no range). | ||
| /// | ||
| /// Tracearr sends a week as 6-hour buckets, one row per server, and only | ||
| /// for buckets with plays. Bucket starts carry no zone and are already in | ||
| /// the timezone the request named, so they read as local times. Every | ||
| /// bucket in a day is added up, days without plays count 0, and the partial | ||
| /// eighth day at the start of the range is left out. | ||
| List<TracearrActivityBucket> dailyPlays( | ||
| TracearrActivityTrend trend, { | ||
| DateTime? now, | ||
| }) { | ||
| final DateTime end = (trend.rangeEnd ?? now ?? DateTime.now()).toLocal(); | ||
| // Built from calendar fields, so a daylight saving change cannot shift a | ||
| // day off midnight. | ||
| final List<DateTime> days = <DateTime>[ | ||
| for (int back = 6; back >= 0; back--) | ||
| DateTime(end.year, end.month, end.day - back), | ||
| ]; | ||
| final Map<DateTime, int> counts = <DateTime, int>{ | ||
| for (final DateTime day in days) day: 0, | ||
| }; | ||
| for (final TracearrActivityBucket bucket in trend.plays) { | ||
| final DateTime? start = bucket.date?.toLocal(); | ||
| if (start == null) { | ||
| continue; | ||
| } | ||
| final DateTime day = DateTime(start.year, start.month, start.day); | ||
| final int? sofar = counts[day]; | ||
| if (sofar != null) { | ||
| counts[day] = sofar + bucket.count; | ||
| } | ||
| } | ||
| return <TracearrActivityBucket>[ | ||
| for (final DateTime day in days) | ||
| TracearrActivityBucket(date: day, count: counts[day]!), | ||
| ]; | ||
| } | ||
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
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
127 changes: 127 additions & 0 deletions
127
services/service_tracearr/test/overview/daily_plays_test.dart
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:service_tracearr/src/models/tracearr_models.dart'; | ||
| import 'package:service_tracearr/src/overview/daily_plays.dart'; | ||
|
|
||
| TracearrActivityBucket _b(DateTime date, int count) => | ||
| TracearrActivityBucket(date: date, count: count); | ||
|
|
||
| List<int> _counts(List<TracearrActivityBucket> days) => | ||
| days.map((TracearrActivityBucket d) => d.count).toList(); | ||
|
|
||
| void main() { | ||
| // Tracearr sends bucket starts without a zone, already in the requested | ||
| // timezone, so they parse as local times. | ||
| final DateTime end = DateTime(2026, 11, 26, 21, 40); | ||
|
|
||
| test('one entry per day for the seven days ending on the range end', () { | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend(period: 'week', rangeEnd: end), | ||
| ); | ||
|
|
||
| expect(days.map((TracearrActivityBucket d) => d.date), <DateTime>[ | ||
| for (int day = 20; day <= 26; day++) DateTime(2026, 11, day), | ||
| ]); | ||
| expect(_counts(days), <int>[0, 0, 0, 0, 0, 0, 0]); | ||
| }); | ||
|
|
||
| test('adds up the 6-hour buckets and every server in a day', () { | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend( | ||
| period: 'week', | ||
| rangeEnd: end, | ||
| plays: <TracearrActivityBucket>[ | ||
| _b(DateTime(2026, 11, 25), 1), | ||
| _b(DateTime(2026, 11, 25, 6), 2), | ||
| // A second server's row for the same bucket. | ||
| _b(DateTime(2026, 11, 25, 6), 4), | ||
| _b(DateTime(2026, 11, 25, 18), 8), | ||
| _b(DateTime(2026, 11, 26, 18), 3), | ||
| ], | ||
| ), | ||
| ); | ||
|
|
||
| expect(_counts(days), <int>[0, 0, 0, 0, 0, 15, 3]); | ||
| }); | ||
|
|
||
| test('a late-night play stays on its own day', () { | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend( | ||
| period: 'week', | ||
| rangeEnd: end, | ||
| plays: <TracearrActivityBucket>[_b(DateTime(2026, 11, 26, 18), 1)], | ||
| ), | ||
| ); | ||
|
|
||
| expect(days.last.date, DateTime(2026, 11, 26)); | ||
| expect(days.last.count, 1); | ||
| }); | ||
|
|
||
| test('drops the partial eighth day at the start of the range', () { | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend( | ||
| period: 'week', | ||
| rangeEnd: end, | ||
| plays: <TracearrActivityBucket>[ | ||
| _b(DateTime(2026, 11, 19, 18), 9), | ||
| _b(DateTime(2026, 11, 20), 1), | ||
| ], | ||
| ), | ||
| ); | ||
|
|
||
| expect(_counts(days), <int>[1, 0, 0, 0, 0, 0, 0]); | ||
| }); | ||
|
|
||
| test('reads a UTC range end in local time', () { | ||
| final DateTime utcEnd = DateTime.utc(2026, 11, 26, 23, 30); | ||
| final DateTime localEnd = utcEnd.toLocal(); | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend(period: 'week', rangeEnd: utcEnd), | ||
| ); | ||
|
|
||
| expect( | ||
| days.last.date, | ||
| DateTime(localEnd.year, localEnd.month, localEnd.day), | ||
| ); | ||
| }); | ||
|
|
||
| test('without a range, ends today', () { | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| const TracearrActivityTrend(period: 'week'), | ||
| now: end, | ||
| ); | ||
|
|
||
| expect(days.last.date, DateTime(2026, 11, 26)); | ||
| expect(days, hasLength(7)); | ||
| }); | ||
|
|
||
| test('buckets with no date are ignored', () { | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend( | ||
| period: 'week', | ||
| rangeEnd: end, | ||
| plays: const <TracearrActivityBucket>[ | ||
| TracearrActivityBucket(count: 5), | ||
| ], | ||
| ), | ||
| ); | ||
|
|
||
| expect(_counts(days), <int>[0, 0, 0, 0, 0, 0, 0]); | ||
| }); | ||
|
|
||
| test('days stay whole across a daylight saving change', () { | ||
| // Seven calendar days, whatever the device's zone does in between. | ||
| final List<TracearrActivityBucket> days = dailyPlays( | ||
| TracearrActivityTrend(period: 'week', rangeEnd: DateTime(2026, 11, 3, 12)), | ||
| ); | ||
|
|
||
| expect(days.map((TracearrActivityBucket d) => d.date), <DateTime>[ | ||
| DateTime(2026, 10, 28), | ||
| DateTime(2026, 10, 29), | ||
| DateTime(2026, 10, 30), | ||
| DateTime(2026, 10, 31), | ||
| DateTime(2026, 11), | ||
| DateTime(2026, 11, 2), | ||
| DateTime(2026, 11, 3), | ||
| ]); | ||
| }); | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read zone-less bucket dates in the zone the request used.
When a request has no
timezone, Tracearr buckets plays in its default zone. The PR summary and linked issue identify that default as UTC. The bucket starts come back without an offset, such as"2026-02-08 00:00:00".DateTime.tryParsereads these as device-local time, sotoLocal()atdaily_plays.dart:26does not shift them. Each bucket then lands on the wrong local day by the device's UTC offset. On an America/Los_Angeles device, the bucket labelled2026-11-27 00:00:00covers the evening of November 26 local. It is grouped under November 27. WithrangeEndat2026-11-27T01:30:00Z, the chart ends on November 26, so that bucket is dropped and the plays do not appear. If it is the only nonzero bucket, the chart shows the no-activity message.The tests build bucket dates with local constructors and the mapper fixtures use Z-suffixed strings. Neither exercises an offset-free UTC response, so they cannot catch this.
Zone-less bucket dates must be interpreted in the zone the request used. When no zone was sent, that zone is Tracearr's default. Choose where to apply it, either by passing the effective zone from the provider or by treating zone-less bucket dates as the default zone in the mapper. Either approach must cover the fallback path.