HeartRate: fix db ui access

This commit is contained in:
Milos Kozak 2023-09-01 11:41:31 +02:00
parent f2f205cc65
commit 0a34c77ce8
4 changed files with 25 additions and 26 deletions

View file

@ -10,6 +10,7 @@ import info.nightscout.database.entities.EffectiveProfileSwitch
import info.nightscout.database.entities.ExtendedBolus
import info.nightscout.database.entities.Food
import info.nightscout.database.entities.GlucoseValue
import info.nightscout.database.entities.HeartRate
import info.nightscout.database.entities.OfflineEvent
import info.nightscout.database.entities.ProfileSwitch
import info.nightscout.database.entities.TemporaryBasal
@ -859,7 +860,9 @@ import kotlin.math.roundToInt
fun getLastOfflineEventId(): Long? =
database.offlineEventDao.getLastId()
fun getHeartRatesFromTime(timeMillis: Long) = database.heartRateDao.getFromTime(timeMillis)
fun getHeartRatesFromTime(timeMillis: Long): Single<List<HeartRate>> =
database.heartRateDao.getFromTime(timeMillis)
.subscribeOn(Schedulers.io())
fun getHeartRatesFromTimeToTime(startMillis: Long, endMillis: Long) =
database.heartRateDao.getFromTimeToTime(startMillis, endMillis)

View file

@ -4,6 +4,7 @@ import androidx.room.Dao
import androidx.room.Query
import info.nightscout.database.entities.HeartRate
import info.nightscout.database.entities.TABLE_HEART_RATE
import io.reactivex.rxjava3.core.Single
@Dao
internal interface HeartRateDao : TraceableDao<HeartRate> {
@ -21,7 +22,7 @@ internal interface HeartRateDao : TraceableDao<HeartRate> {
override fun deleteTrackedChanges(): Int
@Query("SELECT * FROM $TABLE_HEART_RATE WHERE timestamp >= :timestamp ORDER BY timestamp")
fun getFromTime(timestamp: Long): List<HeartRate>
fun getFromTime(timestamp: Long): Single<List<HeartRate>>
@Query("SELECT * FROM $TABLE_HEART_RATE WHERE timestamp BETWEEN :startMillis AND :endMillis ORDER BY timestamp")
fun getFromTimeToTime(startMillis: Long, endMillis: Long): List<HeartRate>

View file

@ -31,7 +31,7 @@ class TriggerHeartRate(injector: HasAndroidInjector) : Trigger(injector) {
return true
}
val start = dateUtil.now() - averageHeartRateDurationMillis
val hrs = repository.getHeartRatesFromTime(start)
val hrs = repository.getHeartRatesFromTime(start).blockingGet()
val duration = hrs.takeUnless { it.isEmpty() }?.sumOf { hr -> hr.duration } ?: 0L
if (duration == 0L) {
aapsLogger.info(LTag.AUTOMATION, "HR not ready, no heart rate measured for ${friendlyDescription()}")

View file

@ -3,11 +3,9 @@ package info.nightscout.automation.triggers
import info.nightscout.automation.R
import info.nightscout.automation.elements.Comparator
import info.nightscout.database.entities.HeartRate
import io.reactivex.rxjava3.core.Single
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertTrue
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.verify
@ -25,7 +23,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
@Test
fun friendlyName() {
assertEquals(R.string.triggerHeartRate, TriggerHeartRate(injector).friendlyName())
Assertions.assertEquals(R.string.triggerHeartRate, TriggerHeartRate(injector).friendlyName())
}
@Test
@ -33,7 +31,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
val t = TriggerHeartRate(injector)
`when`(rh.gs(Comparator.Compare.IS_EQUAL_OR_GREATER.stringRes)).thenReturn(">")
`when`(rh.gs(R.string.triggerHeartRateDesc, ">", 80.0)).thenReturn("test")
assertEquals("test", t.friendlyDescription())
Assertions.assertEquals("test", t.friendlyDescription())
}
@Test
@ -43,16 +41,16 @@ class TriggerHeartRateTest : TriggerTestBase() {
comparator.value = Comparator.Compare.IS_GREATER
}
val dup = t.duplicate() as TriggerHeartRate
assertNotSame(t, dup)
assertEquals(100.0, dup.heartRate.value, 0.01)
assertEquals(Comparator.Compare.IS_GREATER, dup.comparator.value)
Assertions.assertNotSame(t, dup)
Assertions.assertEquals(100.0, dup.heartRate.value, 0.01)
Assertions.assertEquals(Comparator.Compare.IS_GREATER, dup.comparator.value)
}
@Test
fun shouldRunNotAvailable() {
val t = TriggerHeartRate(injector).apply { comparator.value = Comparator.Compare.IS_NOT_AVAILABLE }
assertTrue(t.shouldRun())
Assertions.assertTrue(t.shouldRun())
verifyNoMoreInteractions(repository)
}
@ -62,8 +60,8 @@ class TriggerHeartRateTest : TriggerTestBase() {
heartRate.value = 100.0
comparator.value = Comparator.Compare.IS_GREATER
}
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(emptyList())
assertFalse(t.shouldRun())
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(emptyList()))
Assertions.assertFalse(t.shouldRun())
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository)
}
@ -78,8 +76,8 @@ class TriggerHeartRateTest : TriggerTestBase() {
HeartRate(duration = 300_000, timestamp = now - 300_000, beatsPerMinute = 80.0, device = "test"),
HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 60.0, device = "test"),
)
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(hrs)
assertFalse(t.shouldRun())
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs))
Assertions.assertFalse(t.shouldRun())
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository)
}
@ -93,8 +91,8 @@ class TriggerHeartRateTest : TriggerTestBase() {
val hrs = listOf(
HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 120.0, device = "test"),
)
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(hrs)
assertTrue(t.shouldRun())
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs))
Assertions.assertTrue(t.shouldRun())
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository)
}
@ -105,12 +103,9 @@ class TriggerHeartRateTest : TriggerTestBase() {
heartRate.value = 100.0
comparator.value = Comparator.Compare.IS_GREATER
}
assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value)
Assertions.assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value)
assertEquals(
"""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""".trimMargin(),
t.toJSON()
)
Assertions.assertEquals("""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""".trimMargin(), t.toJSON())
}
@Test
@ -120,7 +115,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
"""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}"""
)
) as TriggerHeartRate
assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value)
assertEquals(100.0, t.heartRate.value, 0.01)
Assertions.assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value)
Assertions.assertEquals(100.0, t.heartRate.value, 0.01)
}
}