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.ExtendedBolus
import info.nightscout.database.entities.Food import info.nightscout.database.entities.Food
import info.nightscout.database.entities.GlucoseValue import info.nightscout.database.entities.GlucoseValue
import info.nightscout.database.entities.HeartRate
import info.nightscout.database.entities.OfflineEvent import info.nightscout.database.entities.OfflineEvent
import info.nightscout.database.entities.ProfileSwitch import info.nightscout.database.entities.ProfileSwitch
import info.nightscout.database.entities.TemporaryBasal import info.nightscout.database.entities.TemporaryBasal
@ -859,7 +860,9 @@ import kotlin.math.roundToInt
fun getLastOfflineEventId(): Long? = fun getLastOfflineEventId(): Long? =
database.offlineEventDao.getLastId() 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) = fun getHeartRatesFromTimeToTime(startMillis: Long, endMillis: Long) =
database.heartRateDao.getFromTimeToTime(startMillis, endMillis) database.heartRateDao.getFromTimeToTime(startMillis, endMillis)

View file

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

View file

@ -31,7 +31,7 @@ class TriggerHeartRate(injector: HasAndroidInjector) : Trigger(injector) {
return true return true
} }
val start = dateUtil.now() - averageHeartRateDurationMillis 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 val duration = hrs.takeUnless { it.isEmpty() }?.sumOf { hr -> hr.duration } ?: 0L
if (duration == 0L) { if (duration == 0L) {
aapsLogger.info(LTag.AUTOMATION, "HR not ready, no heart rate measured for ${friendlyDescription()}") 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.R
import info.nightscout.automation.elements.Comparator import info.nightscout.automation.elements.Comparator
import info.nightscout.database.entities.HeartRate import info.nightscout.database.entities.HeartRate
import io.reactivex.rxjava3.core.Single
import org.json.JSONObject import org.json.JSONObject
import org.junit.Assert.assertEquals import org.junit.jupiter.api.Assertions
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertTrue
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mockito.verify import org.mockito.Mockito.verify
@ -25,7 +23,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
@Test @Test
fun friendlyName() { fun friendlyName() {
assertEquals(R.string.triggerHeartRate, TriggerHeartRate(injector).friendlyName()) Assertions.assertEquals(R.string.triggerHeartRate, TriggerHeartRate(injector).friendlyName())
} }
@Test @Test
@ -33,7 +31,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
val t = TriggerHeartRate(injector) val t = TriggerHeartRate(injector)
`when`(rh.gs(Comparator.Compare.IS_EQUAL_OR_GREATER.stringRes)).thenReturn(">") `when`(rh.gs(Comparator.Compare.IS_EQUAL_OR_GREATER.stringRes)).thenReturn(">")
`when`(rh.gs(R.string.triggerHeartRateDesc, ">", 80.0)).thenReturn("test") `when`(rh.gs(R.string.triggerHeartRateDesc, ">", 80.0)).thenReturn("test")
assertEquals("test", t.friendlyDescription()) Assertions.assertEquals("test", t.friendlyDescription())
} }
@Test @Test
@ -43,16 +41,16 @@ class TriggerHeartRateTest : TriggerTestBase() {
comparator.value = Comparator.Compare.IS_GREATER comparator.value = Comparator.Compare.IS_GREATER
} }
val dup = t.duplicate() as TriggerHeartRate val dup = t.duplicate() as TriggerHeartRate
assertNotSame(t, dup) Assertions.assertNotSame(t, dup)
assertEquals(100.0, dup.heartRate.value, 0.01) Assertions.assertEquals(100.0, dup.heartRate.value, 0.01)
assertEquals(Comparator.Compare.IS_GREATER, dup.comparator.value) Assertions.assertEquals(Comparator.Compare.IS_GREATER, dup.comparator.value)
} }
@Test @Test
fun shouldRunNotAvailable() { fun shouldRunNotAvailable() {
val t = TriggerHeartRate(injector).apply { comparator.value = Comparator.Compare.IS_NOT_AVAILABLE } val t = TriggerHeartRate(injector).apply { comparator.value = Comparator.Compare.IS_NOT_AVAILABLE }
assertTrue(t.shouldRun()) Assertions.assertTrue(t.shouldRun())
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -62,8 +60,8 @@ class TriggerHeartRateTest : TriggerTestBase() {
heartRate.value = 100.0 heartRate.value = 100.0
comparator.value = Comparator.Compare.IS_GREATER comparator.value = Comparator.Compare.IS_GREATER
} }
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(emptyList()) `when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(emptyList()))
assertFalse(t.shouldRun()) Assertions.assertFalse(t.shouldRun())
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis) verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository) 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 - 300_000, beatsPerMinute = 80.0, device = "test"),
HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 60.0, device = "test"), HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 60.0, device = "test"),
) )
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(hrs) `when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs))
assertFalse(t.shouldRun()) Assertions.assertFalse(t.shouldRun())
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis) verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -93,8 +91,8 @@ class TriggerHeartRateTest : TriggerTestBase() {
val hrs = listOf( val hrs = listOf(
HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 120.0, device = "test"), HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 120.0, device = "test"),
) )
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(hrs) `when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs))
assertTrue(t.shouldRun()) Assertions.assertTrue(t.shouldRun())
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis) verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -105,12 +103,9 @@ class TriggerHeartRateTest : TriggerTestBase() {
heartRate.value = 100.0 heartRate.value = 100.0
comparator.value = Comparator.Compare.IS_GREATER comparator.value = Comparator.Compare.IS_GREATER
} }
assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value) Assertions.assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value)
assertEquals( Assertions.assertEquals("""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""".trimMargin(), t.toJSON())
"""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""".trimMargin(),
t.toJSON()
)
} }
@Test @Test
@ -120,7 +115,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
"""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""" """{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}"""
) )
) as TriggerHeartRate ) as TriggerHeartRate
assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value) Assertions.assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value)
assertEquals(100.0, t.heartRate.value, 0.01) Assertions.assertEquals(100.0, t.heartRate.value, 0.01)
} }
} }