health: day, month and year chart tabs fill their time slots like the week tab does
- Commit
31c0c0e272518f9e64f6b5454528ade4974cc641- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 14:21 UTC (4 days ago)
- Parents
5a66550
Aggregation returns only buckets that have records, and mapChartSeries only slot-filled the week tab: on the day, month and year tabs each value landed in an earlier slot than its own time — one 23:00 reading plotted at the midnight slot, one Jun-30 value at Jun 1, one December value in January (mislabelled 'Dec' at position 0). The week-tab version of this bug was fixed with filled slots; the other tabs never got the fill, and totalBarCount also divided by the sparse row count instead of the slot count. Every tab now builds its slot keys (24 hourly / days in the anchored month / 12 monthly) and fills values into their own slots, missing slots null — the week-tab semantics — and totalBarCount uses the mapped size. Four regression tests failed 4/4 pre-fix. Three existing chart tests used synthetic keys that pinned the collapse; they now use real epoch keys. Verified: :health:testDevUnitTest 656 tests pass; lint errors are pre-existing on untouched files.
Diff
health/src/main/java/com/vayunmathur/health/platform/HealthChartData.kt
diff --git a/health/src/main/java/com/vayunmathur/health/platform/HealthChartData.kt b/health/src/main/java/com/vayunmathur/health/platform/HealthChartData.ktindex c3f8d9a611..66409d265c 100644--- a/health/src/main/java/com/vayunmathur/health/platform/HealthChartData.kt+++ b/health/src/main/java/com/vayunmathur/health/platform/HealthChartData.kt@@ -156,14 +156,44 @@ internal object HealthChartData { } else { null }+ // Every other tab fills its slots too, for the same reason: mapping+ // only the buckets that have records collapses the series left, so+ // each value would land in an earlier slot than its own. Missing+ // slots become null, which the bar chart renders as zero and the+ // line chart as a gap.+ val slotKeys: List<Long>? = when (selectedTab) {+ 0 -> (0 until HOURS_PER_DAY).map {+ startDate.toEpochDays() * HOURS_PER_DAY + it+ }+ 2 -> {+ val monthStart = LocalDate(startDate.year, startDate.month, 1)+ (0 until daysInMonth(monthStart)).map {+ monthStart.plus(it, DateTimeUnit.DAY).toEpochDays()+ }+ }+ 3 -> (0 until 12).map {+ LocalDate(startDate.year, 1, 1)+ .plus(it, DateTimeUnit.MONTH).toEpochDays()+ }+ else -> null+ }+ val byKey = rawPairs.associate { it.first to (it.second to it.third) }+ val filled: List<Triple<Long, Double?, Double?>>? = slotKeys?.map { key ->+ val pair = byKey[key]+ Triple(key, pair?.first, pair?.second)+ } val mappedChart: List<Pair<String, Double?>> = weekSeries?.map { (date, value, _) -> labelFor(selectedTab, date.toEpochDays().toLong(), use24Hour) to value+ } ?: filled?.map { (key, value, _) ->+ labelFor(selectedTab, key, use24Hour) to value } ?: rawPairs.map { p -> labelFor(selectedTab, p.first, use24Hour) to p.second } val mappedSecondaryChart: List<Pair<String, Double?>>? = if (config.isDualSeries) { weekSeries?.map { (date, _, secondary) -> labelFor(selectedTab, date.toEpochDays().toLong(), use24Hour) to secondary+ } ?: filled?.map { (key, _, secondary) ->+ labelFor(selectedTab, key, use24Hour) to secondary } ?: rawPairs.map { p -> labelFor(selectedTab, p.first, use24Hour) to p.third } } else { null@@ -363,9 +393,10 @@ internal object HealthChartData { chartData = mappedChart, secondaryChartData = mappedSecondaryChart, historyItems = history,- // Fixed 7 for the week tab: spacing math must divide by all 7- // slots even when only some days have data.- totalBarCount = if (selectedTab == 1) DAYS_PER_WEEK else rawPairs.size,+ // Spacing math must divide by every slot the tab shows (24+ // hours, 7 days, the days in the month, 12 months), even when+ // only some slots have data: that is the filled chart's size.+ totalBarCount = mappedChart.size, primaryRange = chartRange(mappedChart), ) }
health/src/test/java/com/vayunmathur/health/util/HealthChartDataTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/util/HealthChartDataTest.kt b/health/src/test/java/com/vayunmathur/health/util/HealthChartDataTest.ktindex 9b45e835c1..a2294f1258 100644--- a/health/src/test/java/com/vayunmathur/health/util/HealthChartDataTest.kt+++ b/health/src/test/java/com/vayunmathur/health/util/HealthChartDataTest.kt@@ -117,25 +117,29 @@ class HealthChartDataTest { @Test fun `non-week tabs map pairs in order`() {+ val day = LocalDate(2026, 3, 4)+ val base = day.toEpochDays() * 24L val pairs = listOf(- Tuple3(0L, 100.0, 0.0),- Tuple3(6L, 200.0, 0.0),+ Tuple3(base, 100.0, 0.0),+ Tuple3(base + 6, 200.0, 0.0), ) val (mapped, secondary) = HealthChartData.mapChartSeries(- HealthMetricConfig.STEPS, 0, LocalDate(2026, 3, 4), pairs, use24Hour = true,+ HealthMetricConfig.STEPS, 0, day, pairs, use24Hour = true, )- assertEquals(2, mapped.size)+ // Day tab fills all 24 hour slots; each value keeps its own hour.+ assertEquals(24, mapped.size) assertEquals(100.0, mapped[0].second)- assertEquals(200.0, mapped[1].second)+ assertEquals(200.0, mapped[6].second) // Steps is not dual-series: no secondary chart. assertNull(secondary) } @Test fun `dual series maps the secondary values`() {- val pairs = listOf(Tuple3(0L, 120.0, 80.0))+ val day = LocalDate(2026, 3, 4)+ val pairs = listOf(Tuple3(day.toEpochDays() * 24L, 120.0, 80.0)) val (_, secondary) = HealthChartData.mapChartSeries(- HealthMetricConfig.BLOOD_PRESSURE, 0, LocalDate(2026, 3, 4), pairs, use24Hour = true,+ HealthMetricConfig.BLOOD_PRESSURE, 0, day, pairs, use24Hour = true, ) assertNotNull(secondary) assertEquals(80.0, secondary.first().second)@@ -148,7 +152,8 @@ class HealthChartDataTest { HistoryItem("Tue", 200.0, null, "steps", false, false), ) val data = HealthChartData.buildDashboardData(- HealthMetricConfig.STEPS, 1, emptyList(), emptyList(), null, history,+ HealthMetricConfig.STEPS, 1, emptyList(),+ List(7) { "d$it" to null }, null, history, ) assertEquals(300.0, data.totalValue) assertEquals(150.0, data.dailyAverage)
health/src/test/java/com/vayunmathur/health/util/SparseChartSlotsTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/util/SparseChartSlotsTest.kt b/health/src/test/java/com/vayunmathur/health/util/SparseChartSlotsTest.ktnew file mode 100644index 0000000000..4991d3ac5e--- /dev/null+++ b/health/src/test/java/com/vayunmathur/health/util/SparseChartSlotsTest.kt@@ -0,0 +1,78 @@+package com.vayunmathur.health.util++import com.vayunmathur.health.platform.HealthChartData+import com.vayunmathur.health.ui.HealthMetricConfig+import com.vayunmathur.library.util.Tuple3+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlinx.datetime.LocalDate+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Regression tests: sparse chart buckets collapsed left on the day, month and+ * year tabs. The DAO only returns buckets that have records, and+ * [HealthChartData.mapChartSeries] mapped them in order, so a lone 23:00+ * reading plotted in the midnight slot, a lone Jun 30 bar in the Jun 1 slot,+ * and a lone December bar in the January slot — the same collapse the week+ * tab fixed with filled slots (#727). Each value must keep its own slot.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class SparseChartSlotsTest {++ @Test+ fun `day tab keeps a lone late-hour value in its own hour slot`() {+ val day = LocalDate(2026, 3, 4)+ val key = day.toEpochDays() * 24L + 23+ val (mapped, _) = HealthChartData.mapChartSeries(+ HealthMetricConfig.STEPS, 0, day,+ listOf(Tuple3(key, 5000.0, 0.0)), use24Hour = true,+ )+ assertEquals(24, mapped.size)+ assertNull(mapped[0].second)+ assertEquals(5000.0, mapped[23].second)+ }++ @Test+ fun `month tab keeps a lone last-day value in its own day slot`() {+ // June 2024 has 30 days; only Jun 30 has records.+ val pair = Tuple3(LocalDate(2024, 6, 30).toEpochDays(), 5000.0, 0.0)+ val (mapped, _) = HealthChartData.mapChartSeries(+ HealthMetricConfig.STEPS, 2, LocalDate(2024, 6, 1),+ listOf(pair), use24Hour = true,+ )+ assertEquals(30, mapped.size)+ assertNull(mapped[0].second)+ assertEquals(5000.0, mapped[29].second)+ }++ @Test+ fun `year tab keeps a lone december value in its own month slot`() {+ val pair = Tuple3(LocalDate(2024, 12, 1).toEpochDays(), 9000.0, 0.0)+ val (mapped, _) = HealthChartData.mapChartSeries(+ HealthMetricConfig.STEPS, 3, LocalDate(2024, 1, 1),+ listOf(pair), use24Hour = true,+ )+ assertEquals(12, mapped.size)+ assertNull(mapped[0].second)+ assertEquals(9000.0, mapped[11].second)+ }++ @Test+ fun `dashboard bar count counts filled slots not sparse rows`() {+ val day = LocalDate(2026, 3, 4)+ val key = day.toEpochDays() * 24L + 23+ val (mapped, secondary) = HealthChartData.mapChartSeries(+ HealthMetricConfig.STEPS, 0, day,+ listOf(Tuple3(key, 5000.0, 0.0)), use24Hour = true,+ )+ val data = HealthChartData.buildDashboardData(+ HealthMetricConfig.STEPS, 0,+ listOf(Tuple3(key, 5000.0, 0.0)), mapped, secondary, emptyList(),+ )+ assertEquals(24, data.totalBarCount)+ }+}