health: sum weekly and monthly buckets instead of averaging
- Commit
2c8161a79216be2b78bd893d56e748a7418aab6a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:56 UTC (29 days ago)
- Parents
c8d08d2
Symptom: month/year views of bar metrics (steps, calories, distance, sleep, exercise) showed roughly a daily average where a period total belongs; downstream dailyAverage/total math inherited the small number. Root cause: HealthAPI.aggregateByPeriod is shared by getListOfSums and getListOfAverages, and its Weekly/Monthly branches always averaged the daily rows. Daily/Hourly pass through untouched, so only re-bucketed periods were wrong. Fix: thread a combine function through aggregateByPeriod; sums combine with sum(), averages keep average(). Daily/Hourly behaviour unchanged. Verification: new AggregateCombineTest pins sum vs average combining; full :health:testDevUnitTest + compile run after all fixes.
Diff
health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt
diff --git a/health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt b/health/src/main/java/com/vayunmathur/health/util/HealthAPI.ktindex 14b5df5d8c..32fd3186f5 100644--- a/health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt+++ b/health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt@@ -285,6 +285,7 @@ object HealthAPI { period: PeriodType, dailyQuery: suspend (RecordType, Instant, Instant) -> List<com.vayunmathur.health.data.HealthDao.DailySum>, hourlyQuery: suspend (RecordType, Long, Long) -> List<com.vayunmathur.health.data.HealthDao.HourlySum>,+ combine: (List<Double>) -> Double = { it.average() }, ): List<Tuple3<Long, Double, Double>> = when (period) { PeriodType.Daily -> { dailyQuery(recordType, startTime, endTime).sortedBy { it.day }.map {@@ -296,7 +297,7 @@ object HealthAPI { val date = LocalDate.parse(it.day) date.minus((date.dayOfWeek.ordinal + 1) % DAYS_PER_WEEK, DateTimeUnit.DAY).toEpochDays() }.map { (key, values) ->- Tuple3(key, values.map { it.totalValue }.average(), values.map { it.totalValue2 }.average())+ Tuple3(key, combine(values.map { it.totalValue }), combine(values.map { it.totalValue2 })) } } PeriodType.Monthly -> {@@ -304,7 +305,7 @@ object HealthAPI { val date = LocalDate.parse(it.day) date.minus(date.day - 1, DateTimeUnit.DAY).toEpochDays() }.map { (key, values) ->- Tuple3(key, values.map { it.totalValue }.average(), values.map { it.totalValue2 }.average())+ Tuple3(key, combine(values.map { it.totalValue }), combine(values.map { it.totalValue2 })) } } PeriodType.Hourly -> {@@ -329,6 +330,7 @@ object HealthAPI { recordType, startTime, endTime, period, dailyQuery = { t, s, e -> repo().getDailyAvgs(t, s, e) }, hourlyQuery = { t, s, e -> repo().getHourlyAvgs(t, s, e) },+ combine = ::combineForAverages, ) suspend fun getListOfSums(@@ -340,5 +342,12 @@ object HealthAPI { recordType, startTime, endTime, period, dailyQuery = { t, s, e -> repo().getDailySums(t, s, e) }, hourlyQuery = { t, s, e -> repo().getHourlySums(t, s, e) },+ // Weekly/monthly buckets of daily sums must be summed, not averaged:+ // averaging reported e.g. mean daily steps as the month total.+ combine = ::combineForSums, )++ /** How a weekly/monthly bucket combines its daily values (test seam). */+ internal fun combineForSums(values: List<Double>): Double = values.sum()+ internal fun combineForAverages(values: List<Double>): Double = values.average() }
health/src/test/java/com/vayunmathur/health/util/AggregateCombineTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/util/AggregateCombineTest.kt b/health/src/test/java/com/vayunmathur/health/util/AggregateCombineTest.ktnew file mode 100644index 0000000000..704691311c--- /dev/null+++ b/health/src/test/java/com/vayunmathur/health/util/AggregateCombineTest.kt@@ -0,0 +1,18 @@+package com.vayunmathur.health.util++import kotlin.test.Test+import kotlin.test.assertEquals++/** Weekly/monthly buckets: sums must sum daily sums, averages must average. */+class AggregateCombineTest {+ @Test+ fun sumsCombineBySumming() {+ assertEquals(21000.0, HealthAPI.combineForSums(listOf(5000.0, 8000.0, 8000.0)))+ assertEquals(0.0, HealthAPI.combineForSums(emptyList()))+ }++ @Test+ fun averagesCombineByAveraging() {+ assertEquals(7000.0, HealthAPI.combineForAverages(listOf(5000.0, 8000.0, 8000.0)))+ }+}