diff --git a/core/graph/src/main/java/info/nightscout/core/graph/OverviewData.kt b/core/graph/src/main/java/info/nightscout/core/graph/OverviewData.kt index 0a9269e559..77b3709317 100644 --- a/core/graph/src/main/java/info/nightscout/core/graph/OverviewData.kt +++ b/core/graph/src/main/java/info/nightscout/core/graph/OverviewData.kt @@ -47,6 +47,15 @@ interface OverviewData { * BG */ + /** + * Get newest glucose value from bucketed data. + * If there are less than 3 glucose values, bucketed data is not created. + * In this case take newest [GlucoseValue] from db and convert it to [InMemoryGlucoseValue] + * + * Intended for display on screen only + * + * @return newest glucose value + */ fun lastBg(autosensDataStore: AutosensDataStore): InMemoryGlucoseValue? fun isLow(autosensDataStore: AutosensDataStore): Boolean fun isHigh(autosensDataStore: AutosensDataStore): Boolean diff --git a/implementation/src/main/java/info/nightscout/implementation/overview/OverviewDataImpl.kt b/implementation/src/main/java/info/nightscout/implementation/overview/OverviewDataImpl.kt index a1fd439f2f..db143c1a1b 100644 --- a/implementation/src/main/java/info/nightscout/implementation/overview/OverviewDataImpl.kt +++ b/implementation/src/main/java/info/nightscout/implementation/overview/OverviewDataImpl.kt @@ -138,11 +138,11 @@ class OverviewDataImpl @Inject constructor( */ override fun lastBg(autosensDataStore: AutosensDataStore): InMemoryGlucoseValue? = - autosensDataStore.bucketedData?.let { if (it.size > 0) it[0] else null } - // repository.getLastGlucoseValueWrapped().blockingGet().let { gvWrapped -> - // if (gvWrapped is ValueWrapper.Existing) gvWrapped.value - // else null - // } + autosensDataStore.bucketedData?.firstOrNull() + ?: repository.getLastGlucoseValueWrapped().blockingGet().let { gvWrapped -> + if (gvWrapped is ValueWrapper.Existing) InMemoryGlucoseValue(gvWrapped.value) + else null + } override fun isLow(autosensDataStore: AutosensDataStore): Boolean = lastBg(autosensDataStore)?.let { lastBg -> diff --git a/implementation/src/test/java/info/nightscout/implementation/overview/OverviewDataImplTest.kt b/implementation/src/test/java/info/nightscout/implementation/overview/OverviewDataImplTest.kt new file mode 100644 index 0000000000..73267a0397 --- /dev/null +++ b/implementation/src/test/java/info/nightscout/implementation/overview/OverviewDataImplTest.kt @@ -0,0 +1,90 @@ +package info.nightscout.implementation.overview + +import info.nightscout.androidaps.TestBase +import info.nightscout.database.ValueWrapper +import info.nightscout.database.entities.GlucoseValue +import info.nightscout.database.impl.AppRepository +import info.nightscout.interfaces.GlucoseUnit +import info.nightscout.interfaces.aps.AutosensDataStore +import info.nightscout.interfaces.iob.InMemoryGlucoseValue +import info.nightscout.interfaces.plugin.ActivePlugin +import info.nightscout.interfaces.profile.DefaultValueHelper +import info.nightscout.interfaces.profile.ProfileFunction +import info.nightscout.shared.interfaces.ResourceHelper +import info.nightscout.shared.sharedPreferences.SP +import info.nightscout.shared.utils.DateUtil +import info.nightscout.shared.utils.T +import io.reactivex.rxjava3.core.Single +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.Mock +import org.mockito.Mockito + +class OverviewDataImplTest : TestBase() { + + @Mock lateinit var rh: ResourceHelper + @Mock lateinit var dateUtil: DateUtil + @Mock lateinit var sp: SP + @Mock lateinit var activePlugin: ActivePlugin + @Mock lateinit var defaultValueHelper: DefaultValueHelper + @Mock lateinit var profileFunction: ProfileFunction + @Mock lateinit var repository: AppRepository + @Mock lateinit var autosensDataStore: AutosensDataStore + + private lateinit var sut: OverviewDataImpl + private val time = 1000000L + + private val glucoseValue = + GlucoseValue(raw = 200.0, noise = 0.0, value = 200.0, timestamp = time, sourceSensor = GlucoseValue.SourceSensor.UNKNOWN, trendArrow = GlucoseValue.TrendArrow.FLAT) + + @BeforeEach + fun setup() { + sut = OverviewDataImpl(aapsLogger, rh, dateUtil, sp, activePlugin, defaultValueHelper, profileFunction, repository) + Mockito.`when`(defaultValueHelper.determineLowLine()).thenReturn(80.0) + Mockito.`when`(defaultValueHelper.determineHighLine()).thenReturn(180.0) + Mockito.`when`(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL) + } + + @Test + fun lastBg() { + val bucketedData: MutableList = mutableListOf() + bucketedData.add(InMemoryGlucoseValue(time, 70.0, sourceSensor = GlucoseValue.SourceSensor.UNKNOWN)) + // no data + Mockito.`when`(autosensDataStore.bucketedData).thenReturn(null) + Mockito.`when`(repository.getLastGlucoseValueWrapped()).thenReturn(Single.just(ValueWrapper.Absent())) + Assertions.assertNull(sut.lastBg(autosensDataStore)) + Assertions.assertFalse(sut.isLow(autosensDataStore)) + Assertions.assertFalse(sut.isHigh(autosensDataStore)) + + // no bucketed but in db + Mockito.`when`(autosensDataStore.bucketedData).thenReturn(null) + Mockito.`when`(repository.getLastGlucoseValueWrapped()).thenReturn(Single.just(ValueWrapper.Existing(glucoseValue))) + Assertions.assertEquals(200.0, sut.lastBg(autosensDataStore)?.value) + Assertions.assertFalse(sut.isLow(autosensDataStore)) + Assertions.assertTrue(sut.isHigh(autosensDataStore)) + + // in bucketed + Mockito.`when`(autosensDataStore.bucketedData).thenReturn(bucketedData) + Mockito.`when`(repository.getLastGlucoseValueWrapped()).thenReturn(Single.just(ValueWrapper.Existing(glucoseValue))) + Assertions.assertEquals(70.0, sut.lastBg(autosensDataStore)?.value) + Assertions.assertTrue(sut.isLow(autosensDataStore)) + Assertions.assertFalse(sut.isHigh(autosensDataStore)) + } + + @Test + fun isActualBg() { + // no bucketed but in db + Mockito.`when`(autosensDataStore.bucketedData).thenReturn(null) + Mockito.`when`(repository.getLastGlucoseValueWrapped()).thenReturn(Single.just(ValueWrapper.Existing(glucoseValue))) + Mockito.`when`(dateUtil.now()).thenReturn(time + T.mins(1).msecs()) + Assertions.assertTrue(sut.isActualBg(autosensDataStore)) + Mockito.`when`(dateUtil.now()).thenReturn(time + T.mins(9).msecs() + 1) + Assertions.assertFalse(sut.isActualBg(autosensDataStore)) + + // no data + Mockito.`when`(autosensDataStore.bucketedData).thenReturn(null) + Mockito.`when`(repository.getLastGlucoseValueWrapped()).thenReturn(Single.just(ValueWrapper.Absent())) + Assertions.assertFalse(sut.isActualBg(autosensDataStore)) + } +} \ No newline at end of file diff --git a/workflow/src/main/java/info/nightscout/workflow/LoadBgDataWorker.kt b/workflow/src/main/java/info/nightscout/workflow/LoadBgDataWorker.kt index 46caec932e..fdf7edc263 100644 --- a/workflow/src/main/java/info/nightscout/workflow/LoadBgDataWorker.kt +++ b/workflow/src/main/java/info/nightscout/workflow/LoadBgDataWorker.kt @@ -43,7 +43,6 @@ class LoadBgDataWorker( bgReadings = repository .compatGetBgReadingsDataFromTime(start, to + T.mins(2).msecs(), false) .blockingGet() - .filter { it.value >= 39 } aapsLogger.debug(LTag.AUTOSENS) { "BG data loaded. Size: ${bgReadings.size} Start date: ${dateUtil.dateAndTimeString(start)} End date: ${dateUtil.dateAndTimeString(to)}" } createBucketedData(aapsLogger, dateUtil) }