Include measurements taken on the end day of a chart window

This commit is contained in:
Roland Geider
2026-08-10 09:24:16 +02:00
parent 8178d2bcaf
commit 7ac0a1ebeb
2 changed files with 56 additions and 11 deletions

View File

@@ -36,6 +36,12 @@ extension MeasurementChartEntryListExtensions on List<MeasurementChartEntry> {
final List<MeasurementChartEntry> sorted = [...this];
sorted.sort((a, b) => a.date.compareTo(b.date));
// The window is inclusive on both ends at day granularity: callers pass in
// plan dates, which carry no time of day, while entries are timestamps.
// Comparing instants would drop an entry recorded during the end day.
final startDay = DateTime(start.year, start.month, start.day);
final dayAfterEnd = end == null ? null : DateTime(end.year, end.month, end.day + 1);
// Initialize result list
final List<MeasurementChartEntry> result = [];
@@ -60,7 +66,7 @@ extension MeasurementChartEntryListExtensions on List<MeasurementChartEntry> {
lastBeforeEnd = entry;
}
if (entry.date.isBefore(start)) {
if (entry.date.isBefore(startDay)) {
lastBeforeStart = entry;
} else {
// insert interpolated start value if needed
@@ -69,15 +75,14 @@ extension MeasurementChartEntryListExtensions on List<MeasurementChartEntry> {
hasEntryOnStartDay = true;
}
if (end == null || entry.date.isBefore(end)) {
if (dayAfterEnd == null || entry.date.isBefore(dayAfterEnd)) {
result.add(entry);
}
if (end != null && entry.date.isAfter(end)) {
} else {
// insert interpolated end value if needed
// note: we only interpolate end if we have data going beyond end
// if let's say your plan ends in a week from now, we wouldn't want to fake data until next week.
if (!hasEntryOnEndDay && lastBeforeEnd != null) {
result.add(interpolateBetween(lastBeforeEnd, entry, end));
result.add(interpolateBetween(lastBeforeEnd, entry, end!));
hasEntryOnEndDay = true;
}
// we added all our values and did all interpolations

View File

@@ -18,8 +18,44 @@ void main() {
final result = entries.whereDateWithInterpolation(DateTime(2023, 1, 1), DateTime(2023, 1, 3));
// Entries on start and end date should be included if they exist
expect(result.map((e) => e.value), [10, 20, 30]);
});
test('includes an entry recorded during the end day', () {
final entries = [
entry(10, DateTime(2023, 1, 1)),
entry(30, DateTime(2023, 1, 3, 14, 30)),
];
// The plan ends on Jan 3rd, so a measurement taken that afternoon is
// still inside the window and no end value has to be interpolated
final result = entries.whereDateWithInterpolation(DateTime(2023, 1, 1), DateTime(2023, 1, 3));
expect(result.map((e) => e.value), [10, 30]);
expect(result.last.date.millisecond, isNot(INTERPOLATION_MARKER));
});
test('excludes an entry recorded the day after the end', () {
final entries = [
entry(10, DateTime(2023, 1, 1)),
entry(30, DateTime(2023, 1, 4, 0, 1)),
];
final result = entries.whereDateWithInterpolation(DateTime(2023, 1, 1), DateTime(2023, 1, 3));
// Only the Jan 1st entry plus the value interpolated for the end date
expect(result.length, 2);
expect(result.first.value, 10);
expect(result.last.value, 20);
expect(result.last.date.day, 3);
expect(result.last.date.millisecond, INTERPOLATION_MARKER);
});
test('includes an entry recorded during the start day', () {
final entries = [
entry(10, DateTime(2023, 1, 1, 8)),
entry(20, DateTime(2023, 1, 2)),
];
final result = entries.whereDateWithInterpolation(DateTime(2023, 1, 1), DateTime(2023, 1, 2));
expect(result.map((e) => e.value), [10, 20]);
});
// Test: Interpolates start if missing
@@ -30,11 +66,14 @@ void main() {
];
final result = entries.whereDateWithInterpolation(DateTime(2023, 1, 2), DateTime(2023, 1, 3));
// Only the interpolated value for 2nd Jan is included
expect(result.length, 1);
// An interpolated value for 2nd Jan, followed by the real entry on the
// end date
expect(result.length, 2);
expect(result.first.value, closeTo(20, 0.0001));
expect(result.first.date.millisecond, INTERPOLATION_MARKER);
expect(result.first.date.day, 2);
expect(result.last.value, 30);
expect(result.last.date.day, 3);
});
// Test: Interpolates end if missing
@@ -69,9 +108,10 @@ void main() {
entry(20, DateTime(2023, 1, 3)),
];
final result = entries.whereDateWithInterpolation(DateTime(2023, 1, 1), DateTime(2023, 1, 3));
// No interpolation possible for Jan 1, only entry for Jan 2 is included
expect(result.length, 1);
expect(result.first.date.day, 2);
// No interpolation possible for Jan 1, the window starts at the first
// real entry
expect(result.map((e) => e.date.day), [2, 3]);
expect(result.first.date.millisecond, isNot(INTERPOLATION_MARKER));
});
// Test: Only end interpolation if data exists before and after