Drop a health type whole when one of its windows fails

This commit is contained in:
Roland Geider
2026-08-06 21:10:47 +02:00
parent 52eab30a86
commit 459defa8f0
2 changed files with 55 additions and 6 deletions

View File

@@ -221,8 +221,10 @@ class HealthRepository {
for (final type in types) {
try {
// Collected per type and only handed over once every window of it
// returned, see the catch below
final readForType = <HealthDataPoint>[];
var windowStart = start;
var readForType = 0;
while (windowStart.isBefore(end)) {
final windowEnd = windowStart.add(window);
final batch = await _health.getHealthDataFromTypes(
@@ -235,16 +237,16 @@ class HealthRepository {
// reconstructed from GC lines afterwards
if (batch.isNotEmpty) {
_logger.finer('Read ${batch.length} ${type.name} records from $windowStart');
readForType += batch.length;
}
points.addAll(batch);
readForType.addAll(batch);
windowStart = windowEnd;
}
if (readForType > 0) {
_logger.fine('Read $readForType ${type.name} records in total');
if (readForType.isNotEmpty) {
_logger.fine('Read ${readForType.length} ${type.name} records in total');
}
points.addAll(readForType);
} catch (e) {
// The whole type counts as failed even when earlier windows delivered:
// The whole type is dropped even when earlier windows delivered:
// importing half a type would let the watermark move past the readings
// the failing windows never returned
failed++;

View File

@@ -25,6 +25,19 @@ import 'package:wger/features/health/providers/health_repository.dart';
import 'health_repository_test.mocks.dart';
HealthDataPoint dataPoint(HealthDataType type) => HealthDataPoint(
uuid: '${type.name}-1',
value: NumericHealthValue(numericValue: 60),
type: type,
unit: HealthDataUnit.MINUTE,
dateFrom: DateTime(2026, 1, 2),
dateTo: DateTime(2026, 1, 2, 1),
sourcePlatform: HealthPlatformType.googleHealthConnect,
sourceDeviceId: 'device',
sourceId: 'source',
sourceName: 'test',
);
@GenerateMocks([Health])
void main() {
late MockHealth health;
@@ -147,6 +160,40 @@ void main() {
).called(greaterThan(1));
});
test('a type that fails halfway is dropped whole, windows it did read included', () async {
// The watermark moves with what was imported, so half a type would put
// the windows that never returned out of reach for good
var call = 0;
when(
health.getHealthDataFromTypes(
types: [HealthDataType.SLEEP_DEEP],
startTime: anyNamed('startTime'),
endTime: anyNamed('endTime'),
),
).thenAnswer((_) async {
if (call++ > 0) {
throw Exception('boom');
}
return [dataPoint(HealthDataType.SLEEP_DEEP)];
});
when(
health.getHealthDataFromTypes(
types: [HealthDataType.SLEEP_REM],
startTime: anyNamed('startTime'),
endTime: anyNamed('endTime'),
),
).thenAnswer((_) async => [dataPoint(HealthDataType.SLEEP_REM)]);
final readings = await repository.read(
types: [HealthDataType.SLEEP_DEEP, HealthDataType.SLEEP_REM],
start: DateTime(2026, 1, 1),
end: DateTime(2026, 4, 1),
window: defaultReadWindow,
);
expect(readings.map((r) => r.type).toSet(), {HealthDataType.SLEEP_REM});
});
test('a wholesale failure still propagates', () async {
when(
health.getHealthDataFromTypes(