From 459defa8f0232feafdd38bfaf116d53b90025c9c Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 6 Aug 2026 21:10:47 +0200 Subject: [PATCH] Drop a health type whole when one of its windows fails --- .../health/providers/health_repository.dart | 14 +++--- .../providers/health_repository_test.dart | 47 +++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/features/health/providers/health_repository.dart b/lib/features/health/providers/health_repository.dart index 93aa2352d..177499aa3 100644 --- a/lib/features/health/providers/health_repository.dart +++ b/lib/features/health/providers/health_repository.dart @@ -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 = []; 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++; diff --git a/test/features/health/providers/health_repository_test.dart b/test/features/health/providers/health_repository_test.dart index 51b8bb959..cba49a53f 100644 --- a/test/features/health/providers/health_repository_test.dart +++ b/test/features/health/providers/health_repository_test.dart @@ -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(