Skip to content

Commit

Permalink
feat: add aggregateGroupByPeriod API
Browse files Browse the repository at this point in the history
  • Loading branch information
taisuke-j committed Sep 11, 2024
1 parent 3477a77 commit 2277ccc
Show file tree
Hide file tree
Showing 49 changed files with 1,017 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ class HealthConnectManager(private val applicationContext: ReactApplicationConte
}
}

fun aggregateGroupByPeriod(record: ReadableMap, promise: Promise) {
throwUnlessClientIsAvailable(promise) {
coroutineScope.launch {
try {
val recordType = record.getString("recordType") ?: ""
val response = healthConnectClient.aggregateGroupByPeriod(
ReactHealthRecord.getAggregateGroupByPeriodRequest(
recordType, record
)
)
promise.resolve(ReactHealthRecord.parseAggregationResultGroupedByPeriod(recordType, response))
} catch (e: Exception) {
promise.rejectWithException(e)
}
}
}
}

fun getChanges(options: ReadableMap, promise: Promise) {
throwUnlessClientIsAvailable(promise) {
coroutineScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class HealthConnectModule internal constructor(context: ReactApplicationContext)
return manager.aggregateRecord(record, promise)
}

@ReactMethod
override fun aggregateGroupByPeriod(record: ReadableMap, promise: Promise) {
return manager.aggregateGroupByPeriod(record, promise)
}

@ReactMethod
override fun getChanges(options: ReadableMap, promise: Promise) {
return manager.getChanges(options, promise)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.ActiveCaloriesBurnedRecord
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.bridge.WritableNativeArray
import dev.matinzd.healthconnect.utils.convertDataOriginsToJsArray
import dev.matinzd.healthconnect.utils.convertJsToDataOriginSet
import dev.matinzd.healthconnect.utils.convertMetadataFromJSMap
Expand All @@ -14,9 +17,12 @@ import dev.matinzd.healthconnect.utils.energyToJsMap
import dev.matinzd.healthconnect.utils.getEnergyFromJsMap
import dev.matinzd.healthconnect.utils.getTimeRangeFilter
import dev.matinzd.healthconnect.utils.toMapList
import dev.matinzd.healthconnect.utils.mapPeriodStringToPeriod
import java.time.Instant

class ReactActiveCaloriesBurnedRecord : ReactHealthRecordImpl<ActiveCaloriesBurnedRecord> {
private val aggregateMetrics = setOf(ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL)

override fun parseWriteRecord(records: ReadableArray): List<ActiveCaloriesBurnedRecord> {
return records.toMapList().map {
ActiveCaloriesBurnedRecord(
Expand All @@ -41,8 +47,17 @@ class ReactActiveCaloriesBurnedRecord : ReactHealthRecordImpl<ActiveCaloriesBurn

override fun getAggregateRequest(record: ReadableMap): AggregateRequest {
return AggregateRequest(
metrics = setOf(ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL),
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
return AggregateGroupByPeriodRequest(
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
timeRangeSlicer = mapPeriodStringToPeriod(record.getString("timeRangeSlicer")),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}
Expand Down Expand Up @@ -71,4 +86,17 @@ class ReactActiveCaloriesBurnedRecord : ReactHealthRecordImpl<ActiveCaloriesBurn
putArray("dataOrigins", convertDataOriginsToJsArray(record.dataOrigins))
}
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
val map = WritableNativeMap().apply {
putMap("result", parseAggregationResult(it.result))
putString("startTime", it.startTime.toString())
putString("endTime", it.endTime.toString())
}
pushMap(map)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BasalBodyTemperatureRecord
import androidx.health.connect.client.records.BodyTemperatureMeasurementLocation
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Temperature
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.AggregationNotSupported
import dev.matinzd.healthconnect.utils.InvalidTemperature
Expand Down Expand Up @@ -45,10 +48,18 @@ class ReactBasalBodyTemperatureRecord : ReactHealthRecordImpl<BasalBodyTemperatu
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}

private fun getTemperatureFromJsMap(temperatureMap: ReadableMap?): Temperature {
if (temperatureMap == null) {
throw InvalidTemperature()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BasalMetabolicRateRecord
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Power
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.InvalidPower
import dev.matinzd.healthconnect.utils.convertDataOriginsToJsArray
import dev.matinzd.healthconnect.utils.convertJsToDataOriginSet
import dev.matinzd.healthconnect.utils.convertMetadataFromJSMap
import dev.matinzd.healthconnect.utils.convertMetadataToJSMap
import dev.matinzd.healthconnect.utils.getTimeRangeFilter
import dev.matinzd.healthconnect.utils.mapPeriodStringToPeriod
import dev.matinzd.healthconnect.utils.toMapList
import java.time.Instant

class ReactBasalMetabolicRateRecord : ReactHealthRecordImpl<BasalMetabolicRateRecord> {
private val aggregateMetrics = setOf(BasalMetabolicRateRecord.BASAL_CALORIES_TOTAL)

override fun parseWriteRecord(records: ReadableArray): List<BasalMetabolicRateRecord> {
return records.toMapList().map {
BasalMetabolicRateRecord(
Expand All @@ -38,12 +44,34 @@ class ReactBasalMetabolicRateRecord : ReactHealthRecordImpl<BasalMetabolicRateRe

override fun getAggregateRequest(record: ReadableMap): AggregateRequest {
return AggregateRequest(
metrics = setOf(BasalMetabolicRateRecord.BASAL_CALORIES_TOTAL),
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
return AggregateGroupByPeriodRequest(
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
timeRangeSlicer = mapPeriodStringToPeriod(record.getString("timeRangeSlicer")),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
val map = WritableNativeMap().apply {
putMap("result", parseAggregationResult(it.result))
putString("startTime", it.startTime.toString())
putString("endTime", it.endTime.toString())
}
pushMap(map)
}
}
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
return WritableNativeMap().apply {
val map = WritableNativeMap().apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BloodGlucoseRecord
import androidx.health.connect.client.records.MealType.MEAL_TYPE_UNKNOWN
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.BloodGlucose
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.AggregationNotSupported
import dev.matinzd.healthconnect.utils.InvalidBloodGlucoseLevel
Expand Down Expand Up @@ -50,10 +53,18 @@ class ReactBloodGlucoseRecord : ReactHealthRecordImpl<BloodGlucoseRecord> {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}

private fun bloodGlucoseToJsMap(bloodGlucose: BloodGlucose): WritableNativeMap {
return WritableNativeMap().apply {
putDouble("inMillimolesPerLiter", bloodGlucose.inMillimolesPerLiter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BloodPressureRecord
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Pressure
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.*
import java.time.Instant

class ReactBloodPressureRecord : ReactHealthRecordImpl<BloodPressureRecord> {
private val aggregateMetrics = setOf(
BloodPressureRecord.SYSTOLIC_AVG,
BloodPressureRecord.SYSTOLIC_MIN,
BloodPressureRecord.SYSTOLIC_MAX,
BloodPressureRecord.DIASTOLIC_AVG,
BloodPressureRecord.DIASTOLIC_MIN,
BloodPressureRecord.DIASTOLIC_MAX
)

override fun parseWriteRecord(records: ReadableArray): List<BloodPressureRecord> {
return records.toMapList().map {
BloodPressureRecord(
Expand Down Expand Up @@ -40,19 +52,21 @@ class ReactBloodPressureRecord : ReactHealthRecordImpl<BloodPressureRecord> {

override fun getAggregateRequest(record: ReadableMap): AggregateRequest {
return AggregateRequest(
metrics = setOf(
BloodPressureRecord.SYSTOLIC_AVG,
BloodPressureRecord.SYSTOLIC_MIN,
BloodPressureRecord.SYSTOLIC_MAX,
BloodPressureRecord.DIASTOLIC_AVG,
BloodPressureRecord.DIASTOLIC_MIN,
BloodPressureRecord.DIASTOLIC_MAX
),
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
return AggregateGroupByPeriodRequest(
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
timeRangeSlicer = mapPeriodStringToPeriod(record.getString("timeRangeSlicer")),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
return WritableNativeMap().apply {

Expand Down Expand Up @@ -80,6 +94,19 @@ class ReactBloodPressureRecord : ReactHealthRecordImpl<BloodPressureRecord> {
}
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
val map = WritableNativeMap().apply {
putMap("result", parseAggregationResult(it.result))
putString("startTime", it.startTime.toString())
putString("endTime", it.endTime.toString())
}
pushMap(map)
}
}
}

private fun bloodPressureToJsMap(pressure: Pressure): WritableNativeMap {
return WritableNativeMap().apply {
putDouble("inMillimetersOfMercury", pressure.inMillimetersOfMercury)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BodyFatRecord
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Percentage
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.AggregationNotSupported
import dev.matinzd.healthconnect.utils.convertMetadataFromJSMap
Expand Down Expand Up @@ -37,6 +40,14 @@ class ReactBodyFatRecord : ReactHealthRecordImpl<BodyFatRecord> {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
throw AggregationNotSupported()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BodyTemperatureMeasurementLocation
import androidx.health.connect.client.records.BodyTemperatureRecord
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Temperature
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.AggregationNotSupported
import dev.matinzd.healthconnect.utils.InvalidTemperature
Expand Down Expand Up @@ -45,10 +48,18 @@ class ReactBodyTemperatureRecord : ReactHealthRecordImpl<BodyTemperatureRecord>
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}

private fun getTemperatureFromJsMap(temperatureMap: ReadableMap?): Temperature {
if (temperatureMap == null) {
throw InvalidTemperature()
Expand Down
Loading

0 comments on commit 2277ccc

Please sign in to comment.