Count the chart range cutoffs in calendar days

This commit is contained in:
Roland Geider
2026-10-27 21:27:20 +01:00
parent 64ecba31bb
commit 70d6929a5c
4 changed files with 58 additions and 37 deletions

View File

@@ -16,7 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
import 'package:wger/features/measurements/charts/calendar.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
@@ -44,8 +46,7 @@ enum ChartRange {
if (days == null) {
return null;
}
final from = DateTime.now().subtract(Duration(days: days));
return DateTime(from.year, from.month, from.day);
return shiftDays(dayOf(clock.now()), -days);
}
/// Days read beyond [cutoff], so the moving average of the first days in
@@ -58,21 +59,21 @@ enum ChartRange {
///
/// Reaches [_averageLeadDays] beyond [cutoff], and is rounded down to
/// midnight like [countCutoff], for the same reason.
DateTime? get readCutoff => _atMidnight(Duration(days: _averageLeadDays));
DateTime? get readCutoff => _atMidnight(_averageLeadDays);
/// Oldest entry to count, null for the full history: the range itself, with
/// no lead, for the queries that summarise exactly what is on screen.
DateTime? get countCutoff => _atMidnight(Duration.zero);
DateTime? get countCutoff => _atMidnight(0);
/// [cutoff] minus [lead], rounded down to midnight.
/// [cutoff] moved back [leadDays] calendar days, at midnight.
///
/// The rounding is deliberate: these identify a provider and have to stay
/// the same across rebuilds, which a bound derived from the current instant
/// would not.
DateTime? _atMidnight(Duration lead) {
final from = cutoff?.subtract(lead);
DateTime? _atMidnight(int leadDays) {
final from = cutoff;
return from == null ? null : DateTime(from.year, from.month, from.day);
return from == null ? null : shiftDays(from, -leadDays);
}
/// Label for the selector, counted rather than one string per range, so a

View File

@@ -23,6 +23,7 @@ library;
import 'dart:math';
import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
import 'package:wger/features/measurements/charts/calendar.dart';
import 'package:wger/features/measurements/charts/series.dart';
@@ -93,7 +94,7 @@ class SparkWindow {
/// the heatmap follows it within its own, tighter legibility bounds. The
/// delta window gets one slot beyond its weeks for the running week.
SparkWindow sparkWindowFor(SparkKind kind, {required DateTime? cutoff, DateTime? today}) {
final now = dayOf(today ?? DateTime.now());
final now = dayOf(today ?? clock.now());
final days = cutoff == null ? null : daysBetween(dayOf(cutoff), now) + 1;
int weeksOf(int? days) => days == null ? sparkMaxWeeks : min(sparkMaxWeeks, (days / 7).ceil());

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:clock/clock.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:wger/features/measurements/charts/range.dart';
@@ -31,12 +32,25 @@ void main() {
});
test('the week covers today plus the six days before it', () {
// Seven calendar days in all: the count cutoff sits at midnight six
// days back, so a weekday axis gets one letter per day
final count = ChartRange.lastWeek.countCutoff!;
final now = DateTime.now();
// Seven calendar days, so a weekday axis gets one letter per day. Next to
// the European DST switches, where counting elapsed hours lands a day off
final weekStarts = {
DateTime(2026, 10, 26, 23, 30): DateTime(2026, 10, 20),
DateTime(2026, 3, 30, 0, 30): DateTime(2026, 3, 24),
};
expect(DateTime(now.year, now.month, now.day - 6), count);
for (final MapEntry(key: now, value: start) in weekStarts.entries) {
withClock(Clock.fixed(now), () {
expect(ChartRange.lastWeek.countCutoff, start, reason: 'at $now');
});
}
});
test('the read cutoff counts its lead in calendar days', () {
// The 30-day lead from 4 April reaches back across the European spring DST switch
withClock(Clock.fixed(DateTime(2026, 4, 10, 12)), () {
expect(ChartRange.lastWeek.readCutoff, DateTime(2026, 3, 5));
});
});
test('the cutoff sits at the same midnight as the count cutoff', () {

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:clock/clock.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -40,7 +41,7 @@ import 'measurement_tile_test.mocks.dart';
/// The calendar day [daysAgo] days back, at midnight like a day bucket.
DateTime _day(int daysAgo) {
final now = DateTime.now();
final now = clock.now();
return DateTime(now.year, now.month, now.day - daysAgo);
}
@@ -236,31 +237,35 @@ void main() {
});
testWidgets('the range filter decides between daily and weekly slots', (tester) async {
final steps = MeasurementCategory(
id: 'steps',
name: 'Steps',
metricType: MetricType.steps,
);
final entries = {
'steps': [_entry('steps', 8000, _day(1)), _entry('steps', 6000, _day(2))],
};
// Late evening the day after the European autumn DST switch, where counting the
// week in elapsed hours drops a day
await withClock(Clock.fixed(DateTime(2026, 10, 26, 23, 30)), () async {
final steps = MeasurementCategory(
id: 'steps',
name: 'Steps',
metricType: MetricType.steps,
);
final entries = {
'steps': [_entry('steps', 8000, _day(1)), _entry('steps', 6000, _day(2))],
};
// A week is seven daily slots and a month one per day; three months
// come as calendar weeks
await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastWeek));
await tester.pumpAndSettle();
var chart = tester.widget<SparkBarChart>(find.byType(SparkBarChart));
expect(chart.data.slotCount, 7);
// A week is seven daily slots and a month one per day; three months
// come as calendar weeks
await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastWeek));
await tester.pumpAndSettle();
var chart = tester.widget<SparkBarChart>(find.byType(SparkBarChart));
expect(chart.data.slotCount, 7);
await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastMonth));
await tester.pumpAndSettle();
chart = tester.widget<SparkBarChart>(find.byType(SparkBarChart));
expect(chart.data.slotCount, 31);
await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastMonth));
await tester.pumpAndSettle();
chart = tester.widget<SparkBarChart>(find.byType(SparkBarChart));
expect(chart.data.slotCount, 31);
await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.last3Months));
await tester.pumpAndSettle();
chart = tester.widget<SparkBarChart>(find.byType(SparkBarChart));
expect(chart.data.slotCount, 13);
await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.last3Months));
await tester.pumpAndSettle();
chart = tester.widget<SparkBarChart>(find.byType(SparkBarChart));
expect(chart.data.slotCount, 13);
});
});
testWidgets('blood pressure tile: the reading quoted high over low', (tester) async {