From c2c6bb810a4720adc95993f8bd698742d2e6c167 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Tue, 9 Nov 2021 19:21:11 +0100 Subject: [PATCH] Fixes #882 by aggregating DAO query for temporary basals and moving it outside loop --- .../IobCobCalculatorPlugin.kt | 21 +++++++++++++++---- .../androidaps/utils/stats/TddCalculator.kt | 6 ++++-- .../androidaps/interfaces/IobCobCalculator.kt | 11 ++++++++++ .../androidaps/database/AppRepository.kt | 4 ++++ .../database/daos/TemporaryBasalDao.kt | 3 +++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt index 56788c5028..d20c1dde67 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt @@ -522,10 +522,7 @@ class IobCobCalculatorPlugin @Inject constructor( return null } - override fun getTempBasalIncludingConvertedExtended(timestamp: Long): TemporaryBasal? { - - val tb = repository.getTemporaryBasalActiveAt(timestamp).blockingGet() - if (tb is ValueWrapper.Existing) return tb.value + private fun getConvertedExtended(timestamp: Long): TemporaryBasal? { if (activePlugin.activePump.isFakingTempsByExtendedBoluses) { val eb = repository.getExtendedBolusActiveAt(timestamp).blockingGet() val profile = profileFunction.getProfile(timestamp) ?: return null @@ -534,6 +531,22 @@ class IobCobCalculatorPlugin @Inject constructor( return null } + override fun getTempBasalIncludingConvertedExtended(timestamp: Long): TemporaryBasal? { + val tb = repository.getTemporaryBasalActiveAt(timestamp).blockingGet() + if (tb is ValueWrapper.Existing) return tb.value + return getConvertedExtended(timestamp); + } + + override fun getTempBasalIncludingConvertedExtendedForRange(startTime: Long, endTime: Long, calculationStep: Long): Map { + val tempBasals = HashMap(); + val tbs = repository.getTemporaryBasalsDataActiveBetweenTimeAndTime(startTime, endTime).blockingGet() + for (t in startTime until endTime step calculationStep) { + val tb = tbs.firstOrNull { basal -> basal.timestamp <= t && (basal.timestamp + basal.duration) > t } + tempBasals[t] = tb ?: getConvertedExtended(t) + } + return tempBasals; + } + override fun calculateAbsoluteIobFromBaseBasals(toTime: Long): IobTotal { val total = IobTotal(toTime) var i = toTime - range() diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt index 285d3a419a..cfe0eeeaec 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt @@ -50,10 +50,12 @@ class TddCalculator @Inject constructor( result.put(midnight, tdd) } - for (t in startTime until endTime step T.mins(5).msecs()) { + val calculationStep = T.mins(5).msecs() + val tempBasals = iobCobCalculator.getTempBasalIncludingConvertedExtendedForRange(startTime, endTime, calculationStep) + for (t in startTime until endTime step calculationStep) { val midnight = MidnightTime.calc(t) val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight) - val tbr = iobCobCalculator.getTempBasalIncludingConvertedExtended(t) + val tbr = tempBasals[t] val profile = profileFunction.getProfile(t) ?: continue val absoluteRate = tbr?.convertedToAbsolute(t, profile) ?: profile.getBasal(t) tdd.basalAmount += absoluteRate / 60.0 * 5.0 diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/IobCobCalculator.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/IobCobCalculator.kt index 9e3a9d428a..7c75e0ebc8 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/IobCobCalculator.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/IobCobCalculator.kt @@ -64,6 +64,17 @@ interface IobCobCalculator { */ fun getTempBasalIncludingConvertedExtended(timestamp: Long): TemporaryBasal? + /** + * Get running temporary basals for given time range, sliced by calculationStep. + * For each step between given range it calculates equivalent of getTempBasalIncludingConvertedExtended + * + * @param startTime start of calculated period, timestamp + * @param endTime end of calculated period, timestamp + * @param calculationStep calculation step, in millisecond + * @return map where for each step, its timestamp is a key and calculated optional temporary basal is a value + */ + fun getTempBasalIncludingConvertedExtendedForRange(startTime: Long, endTime: Long, calculationStep: Long): Map + /** * Get running extended bolus at time * diff --git a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt index f32c0aac0f..caff9784bb 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -685,6 +685,10 @@ import kotlin.math.roundToInt .subscribeOn(Schedulers.io()) .toWrappedSingle() + fun getTemporaryBasalsDataActiveBetweenTimeAndTime(from: Long, to: Long): Single> = + database.temporaryBasalDao.getTemporaryBasalActiveBetweenTimeAndTime(from, to) + .subscribeOn(Schedulers.io()) + fun getTemporaryBasalsDataFromTime(timestamp: Long, ascending: Boolean): Single> = database.temporaryBasalDao.getTemporaryBasalDataFromTime(timestamp) .map { if (!ascending) it.reversed() else it } diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt index 94f717a420..b4aac3ba54 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt @@ -47,6 +47,9 @@ internal interface TemporaryBasalDao : TraceableDao { @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp <= :timestamp AND (timestamp + duration) > :timestamp AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC LIMIT 1") fun getTemporaryBasalActiveAt(timestamp: Long): Maybe + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp <= :to AND (timestamp + duration) > :from AND referenceId IS NULL AND isValid = 1 ORDER BY timestamp DESC") + fun getTemporaryBasalActiveBetweenTimeAndTime(from: Long, to: Long): Single> + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") fun getTemporaryBasalDataFromTime(timestamp: Long): Single>