Skip to content

Commit f0ad26a

Browse files
committed
Fix null value handling in sample converters
1 parent 39ee8fa commit f0ad26a

File tree

6 files changed

+163
-198
lines changed

6 files changed

+163
-198
lines changed

oura-library/src/main/kotlin/org/radarbase/oura/converter/OuraDailyActivityMetConverter.kt

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.time.OffsetDateTime
99

1010
class OuraDailyActivityMetConverter(
1111
private val topic: String = "connect_oura_met",
12+
private val sampleKey: String = "met",
1213
) : OuraDataConverter {
1314

1415
@Throws(IOException::class)
@@ -18,15 +19,15 @@ class OuraDailyActivityMetConverter(
1819
): Sequence<Result<TopicData>> {
1920
val array = root.get("data")
2021
?: return emptySequence()
21-
try {
22-
return array.asSequence()
23-
.flatMap {
22+
return array.asSequence()
23+
.flatMap {
24+
runCatching {
2425
it.processSamples(user)
26+
}.getOrElse {
27+
logger.error("Error processing records", it.message)
28+
emptySequence()
2529
}
26-
} catch (e: Exception) {
27-
logger.error("Error processing records", e)
28-
return emptySequence()
29-
}
30+
}
3031
}
3132

3233
private fun JsonNode.processSamples(
@@ -36,32 +37,25 @@ class OuraDailyActivityMetConverter(
3637
val startTimeEpoch = startTime.toInstant().toEpochMilli() / 1000.0
3738
val timeReceivedEpoch = System.currentTimeMillis() / 1000.0
3839
val id = this.get("id").textValue()
39-
val interval = this.get("met")?.get("interval")?.intValue()
40-
?: throw IOException(
41-
"Unable to get sample interval. " +
42-
this.get("met").toString(),
43-
)
44-
val items = this.get("met")?.get("items")
45-
return if (items == null) {
46-
emptySequence()
47-
} else {
48-
items.asSequence()
49-
.mapIndexedCatching { index, value ->
50-
val offset = interval * index
51-
val time = startTimeEpoch + offset
52-
TopicData(
53-
key = user.observationKey,
54-
topic = topic,
55-
offset = time.toLong(),
56-
value = toMet(
57-
time,
58-
timeReceivedEpoch,
59-
id,
60-
value.floatValue(),
61-
),
62-
)
63-
}
64-
}
40+
val interval = this.get(sampleKey)?.get("interval")?.intValue()
41+
?: throw IOException("Unable to get sample interval.")
42+
val items = this.get(sampleKey)?.get("items") ?: throw IOException("Unable to get items.")
43+
return items.asSequence()
44+
.mapIndexedCatching { index, value ->
45+
val offset = interval * index
46+
val time = startTimeEpoch + offset
47+
TopicData(
48+
key = user.observationKey,
49+
topic = topic,
50+
offset = time.toLong(),
51+
value = toMet(
52+
time,
53+
timeReceivedEpoch,
54+
id,
55+
value.floatValue(),
56+
),
57+
)
58+
}
6559
}
6660

6761
private fun toMet(

oura-library/src/main/kotlin/org/radarbase/oura/converter/OuraSessionHeartRateConverter.kt

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.time.OffsetDateTime
1010

1111
class OuraSessionHeartRateConverter(
1212
private val topic: String = "connect_oura_heart_rate",
13+
private val sampleKey: String = "heart_rate",
1314
) : OuraDataConverter {
1415

1516
@Throws(IOException::class)
@@ -19,15 +20,15 @@ class OuraSessionHeartRateConverter(
1920
): Sequence<Result<TopicData>> {
2021
val array = root.get("data")
2122
?: return emptySequence()
22-
try {
23-
return array.asSequence()
24-
.flatMap {
23+
return array.asSequence()
24+
.flatMap {
25+
runCatching {
2526
it.processSamples(user)
27+
}.getOrElse {
28+
logger.error("Error processing records", it.message)
29+
emptySequence()
2630
}
27-
} catch (e: Exception) {
28-
logger.error("Error processing records", e)
29-
return emptySequence()
30-
}
31+
}
3132
}
3233

3334
private fun JsonNode.processSamples(
@@ -37,32 +38,26 @@ class OuraSessionHeartRateConverter(
3738
val startTimeEpoch = startTime.toInstant().toEpochMilli() / 1000.0
3839
val timeReceivedEpoch = System.currentTimeMillis() / 1000.0
3940
val id = this.get("id").textValue()
40-
val interval = this.get("heart_rate")?.get("interval")?.intValue()
41-
?: throw IOException(
42-
"Unable to get sample interval. " +
43-
this.get("heart_rate").toString(),
44-
)
45-
val items = this.get("heart_rate")?.get("items")
46-
return if (items == null) {
47-
emptySequence()
48-
} else {
49-
items.asSequence()
50-
.mapIndexedCatching { index, value ->
51-
val offset = interval * index
52-
val time = startTimeEpoch + offset
53-
TopicData(
54-
key = user.observationKey,
55-
topic = topic,
56-
offset = time.toLong(),
57-
value = toHeartRate(
58-
time,
59-
timeReceivedEpoch,
60-
id,
61-
value.intValue(),
62-
),
63-
)
64-
}
65-
}
41+
val interval = this.get(sampleKey)?.get("interval")?.intValue()
42+
?: throw IOException("Unable to get sample interval.")
43+
val items = this.get(sampleKey)?.get("items")
44+
?: throw IOException("Unable to get sample items.")
45+
return items.asSequence()
46+
.mapIndexedCatching { index, value ->
47+
val offset = interval * index
48+
val time = startTimeEpoch + offset
49+
TopicData(
50+
key = user.observationKey,
51+
topic = topic,
52+
offset = time.toLong(),
53+
value = toHeartRate(
54+
time,
55+
timeReceivedEpoch,
56+
id,
57+
value.intValue(),
58+
),
59+
)
60+
}
6661
}
6762

6863
private fun toHeartRate(

oura-library/src/main/kotlin/org/radarbase/oura/converter/OuraSessionHrvConverter.kt

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.time.OffsetDateTime
99

1010
class OuraSessionHrvConverter(
1111
private val topic: String = "connect_oura_heart_rate_variability",
12+
private val sampleKey: String = "heart_rate_variability",
1213
) : OuraDataConverter {
1314

1415
@Throws(IOException::class)
@@ -18,15 +19,15 @@ class OuraSessionHrvConverter(
1819
): Sequence<Result<TopicData>> {
1920
val array = root.get("data")
2021
?: return emptySequence()
21-
try {
22-
return array.asSequence()
23-
.flatMap {
22+
return array.asSequence()
23+
.flatMap {
24+
runCatching {
2425
it.processSamples(user)
26+
}.getOrElse {
27+
logger.error("Error processing records", it.message)
28+
emptySequence()
2529
}
26-
} catch (e: Exception) {
27-
logger.error("Error processing records", e)
28-
return emptySequence()
29-
}
30+
}
3031
}
3132

3233
private fun JsonNode.processSamples(
@@ -36,32 +37,25 @@ class OuraSessionHrvConverter(
3637
val startTimeEpoch = startTime.toInstant().toEpochMilli() / 1000.0
3738
val timeReceivedEpoch = System.currentTimeMillis() / 1000.0
3839
val id = this.get("id").textValue()
39-
val interval = this.get("heart_rate_variability")?.get("interval")?.intValue()
40-
?: throw IOException(
41-
"Unable to get sample interval. " +
42-
this.get("heart_rate_variability").toString(),
43-
)
44-
val items = this.get("heart_rate_variability")?.get("items")
45-
return if (items == null) {
46-
emptySequence()
47-
} else {
48-
items.asSequence()
49-
.mapIndexedCatching { index, value ->
50-
val offset = index * interval
51-
val time = startTimeEpoch + offset
52-
TopicData(
53-
key = user.observationKey,
54-
topic = topic,
55-
offset = time.toLong(),
56-
value = toHrv(
57-
time,
58-
timeReceivedEpoch,
59-
id,
60-
value.floatValue(),
61-
),
62-
)
63-
}
64-
}
40+
val interval = this.get(sampleKey)?.get("interval")?.intValue()
41+
?: throw IOException("Unable to get sample interval.")
42+
val items = this.get(sampleKey)?.get("items") ?: throw IOException("Unable to get items.")
43+
return items.asSequence()
44+
.mapIndexedCatching { index, value ->
45+
val offset = index * interval
46+
val time = startTimeEpoch + offset
47+
TopicData(
48+
key = user.observationKey,
49+
topic = topic,
50+
offset = time.toLong(),
51+
value = toHrv(
52+
time,
53+
timeReceivedEpoch,
54+
id,
55+
value.floatValue(),
56+
),
57+
)
58+
}
6559
}
6660

6761
private fun toHrv(

oura-library/src/main/kotlin/org/radarbase/oura/converter/OuraSessionMotionCountConverter.kt

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.time.OffsetDateTime
99

1010
class OuraSessionMotionCountConverter(
1111
private val topic: String = "connect_oura_motion_count",
12+
private val sampleKey: String = "motion_count",
1213
) : OuraDataConverter {
1314

1415
@Throws(IOException::class)
@@ -18,15 +19,15 @@ class OuraSessionMotionCountConverter(
1819
): Sequence<Result<TopicData>> {
1920
val array = root.get("data")
2021
?: return emptySequence()
21-
try {
22-
return array.asSequence()
23-
.flatMap {
22+
return array.asSequence()
23+
.flatMap {
24+
runCatching {
2425
it.processSamples(user)
26+
}.getOrElse {
27+
logger.error("Error processing records", it.message)
28+
emptySequence()
2529
}
26-
} catch (e: Exception) {
27-
logger.error("Error processing records", e)
28-
return emptySequence()
29-
}
30+
}
3031
}
3132

3233
private fun JsonNode.processSamples(
@@ -36,32 +37,25 @@ class OuraSessionMotionCountConverter(
3637
val startTimeEpoch = startTime.toInstant().toEpochMilli() / 1000.0
3738
val timeReceivedEpoch = System.currentTimeMillis() / 1000.0
3839
val id = this.get("id").textValue()
39-
val interval = this.get("motion_count")?.get("interval")?.intValue()
40-
?: throw IOException(
41-
"Unable to get sample interval. " +
42-
this.get("motion_count").toString(),
43-
)
44-
val items = this.get("motion_count")?.get("items")
45-
return if (items == null) {
46-
emptySequence()
47-
} else {
48-
items.asSequence()
49-
.mapIndexedCatching { index, value ->
50-
val offset = interval * index
51-
val time = startTimeEpoch + offset
52-
TopicData(
53-
key = user.observationKey,
54-
topic = topic,
55-
offset = time.toLong(),
56-
value = toMotionCount(
57-
startTimeEpoch,
58-
timeReceivedEpoch,
59-
id,
60-
value.intValue(),
61-
),
62-
)
63-
}
64-
}
40+
val interval = this.get(sampleKey)?.get("interval")?.intValue()
41+
?: throw IOException("Unable to get sample interval.")
42+
val items = this.get(sampleKey)?.get("items") ?: throw IOException("Unable to get items.")
43+
return items.asSequence()
44+
.mapIndexedCatching { index, value ->
45+
val offset = interval * index
46+
val time = startTimeEpoch + offset
47+
TopicData(
48+
key = user.observationKey,
49+
topic = topic,
50+
offset = time.toLong(),
51+
value = toMotionCount(
52+
startTimeEpoch,
53+
timeReceivedEpoch,
54+
id,
55+
value.intValue(),
56+
),
57+
)
58+
}
6559
}
6660

6761
private fun toMotionCount(

0 commit comments

Comments
 (0)