You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Health Connect中aggregateGroupByDuration与aggregateGroupByPeriod的差异解析

Health Connect聚合函数aggregateGroupByDuration与aggregateGroupByPeriod的差异疑问

我无法理解Health Connect提供的这两个聚合函数的差异。以下是分别使用两个函数的实现及运行结果,二者均设置1天的时间切片,但返回的统计数值不一致,希望得到逻辑上的解释。


1. 使用aggregateGroupByDuration的实现与结果

代码实现

private suspend fun aggregateDataByBucketBasedOnDuration(
        healthConnectClient: HealthConnectClient,
        startTime: Instant,
        endTime: Instant
    ) {
        try {

            val response = healthConnectClient.aggregateGroupByDuration(
                AggregateGroupByDurationRequest(
                    metrics = setOf(
                        StepsRecord.COUNT_TOTAL,
                        DistanceRecord.DISTANCE_TOTAL,
                        TotalCaloriesBurnedRecord.ENERGY_TOTAL,
                    ),
                    timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
                    timeRangeSlicer = Duration.ofDays(1)
                )

            )

            response.forEach { result: AggregationResultGroupedByDuration ->
                Timber.d(
                    "steps total: ${result.result[StepsRecord.COUNT_TOTAL]} " +
                            ",distance total: ${result.result[DistanceRecord.DISTANCE_TOTAL]} ," +
                            "calorie total: ${result.result[TotalCaloriesBurnedRecord.ENERGY_TOTAL]} ," +
                            "startTime:${result.startTime} ," +
                            "endTime: ${result.endTime}"
                )
            }


        } catch (e: Exception) {
            // Run error handling here
        }
    }

运行日志

/** aggregateDataByBucketBasedOnDuration result: 
 * steps total: 6602 ,distance total: 3784.0138429299027 meters ,calorie total: 1714.9387687454107 kcal ,startTime:2024-08-16T18:30:00Z ,endTime: 2024-08-17T18:30:00Z
 * steps total: 2153 ,distance total: 946.9942807499685 meters ,calorie total: 1544.0814161167207 kcal ,startTime:2024-08-17T18:30:00Z ,endTime: 2024-08-18T18:30:00Z
 * steps total: 2181 ,distance total: 1217.345914414582 meters ,calorie total: 1534.8714048548898 kcal ,startTime:2024-08-18T18:30:00Z ,endTime: 2024-08-19T18:30:00Z
 * steps total: 1028 ,distance total: 414.3971366935903 meters ,calorie total: 1477.0066373640257 kcal ,startTime:2024-08-19T18:30:00Z ,endTime: 2024-08-20T18:30:00Z
 * steps total: 7444 ,distance total: 6496.074988999614 meters ,calorie total: 1941.2634706186748 kcal ,startTime:2024-08-20T18:30:00Z ,endTime: 2024-08-21T18:29:59.999Z
 */

2. 使用aggregateGroupByPeriod的实现与结果

代码实现

private suspend fun aggregateDataByBucketBasedOnPeriod(
        healthConnectClient: HealthConnectClient,
        startTime: Instant,
        endTime: Instant
    ) {
        try {

            val response = healthConnectClient.aggregateGroupByPeriod(
                AggregateGroupByPeriodRequest(
                    metrics = setOf(
                        StepsRecord.COUNT_TOTAL,
                        DistanceRecord.DISTANCE_TOTAL,
                        TotalCaloriesBurnedRecord.ENERGY_TOTAL,
                    ),
                    timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
                    timeRangeSlicer = Period.ofDays(1)
                )

            )

            response.forEach { result: AggregationResultGroupedByPeriod ->
                Timber.d(
                    "steps total: ${result.result[StepsRecord.COUNT_TOTAL]} " +
                            ",distance total: ${result.result[DistanceRecord.DISTANCE_TOTAL]} ," +
                            "calorie total: ${result.result[TotalCaloriesBurnedRecord.ENERGY_TOTAL]} ," +
                            "startTime: ${result.startTime} ," +
                            "endTime: ${result.endTime}"
                )
            }


        } catch (e: Exception) {
            // Run error handling here
        }
    }

运行日志

/** aggregateDataByBucketBasedOnPeriod
 * steps total: 3771 ,distance total: 2075.2305739754383 meters ,calorie total: 1613.5464919679039 kcal ,startTime: 2024-08-16T18:30 ,endTime: 2024-08-17T18:30
 * steps total: 6607 ,distance total: 3612.4783093918018 meters ,calorie total: 1715.328791728457 kcal ,startTime: 2024-08-17T18:30 ,endTime: 2024-08-18T18:30
 * steps total: 2150 ,distance total: 1203.0787563831184 meters ,calorie total: 1532.6618118549654 kcal ,startTime: 2024-08-18T18:30 ,endTime: 2024-08-19T18:30
 * steps total: 549 ,distance total: 234.74553567468138 meters ,calorie total: 1474.9617246142407 kcal ,startTime: 2024-08-19T18:30 ,endTime: 2024-08-20T18:30
 * steps total: 7916 ,distance total: 6671.767744403136 meters ,calorie total: 1945.517986367616 kcal ,startTime: 2024-08-20T18:30 ,endTime: 2024-08-21T18:29:59.999
 */

核心疑问

我原本以为两个函数在设置1天的时间切片后会返回相同的统计结果,且aggregateGroupByDuration的结果与Health Connect应用内数据完全一致。但实际运行后两者数值存在明显差异,查阅官方文档后仍无法理解背后的运行逻辑,希望得到清晰的解释。


内容的提问来源于stack exchange,提问作者Chinmay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:17:05