Fixes #882 by aggregating DAO query for temporary basals and moving it outside loop
This commit is contained in:
parent
aa58b37643
commit
c2c6bb810a
|
@ -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<Long, TemporaryBasal?> {
|
||||
val tempBasals = HashMap<Long, TemporaryBasal?>();
|
||||
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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<Long, TemporaryBasal?>
|
||||
|
||||
/**
|
||||
* Get running extended bolus at time
|
||||
*
|
||||
|
|
|
@ -685,6 +685,10 @@ import kotlin.math.roundToInt
|
|||
.subscribeOn(Schedulers.io())
|
||||
.toWrappedSingle()
|
||||
|
||||
fun getTemporaryBasalsDataActiveBetweenTimeAndTime(from: Long, to: Long): Single<List<TemporaryBasal>> =
|
||||
database.temporaryBasalDao.getTemporaryBasalActiveBetweenTimeAndTime(from, to)
|
||||
.subscribeOn(Schedulers.io())
|
||||
|
||||
fun getTemporaryBasalsDataFromTime(timestamp: Long, ascending: Boolean): Single<List<TemporaryBasal>> =
|
||||
database.temporaryBasalDao.getTemporaryBasalDataFromTime(timestamp)
|
||||
.map { if (!ascending) it.reversed() else it }
|
||||
|
|
|
@ -47,6 +47,9 @@ internal interface TemporaryBasalDao : TraceableDao<TemporaryBasal> {
|
|||
@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<TemporaryBasal>
|
||||
|
||||
@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<List<TemporaryBasal>>
|
||||
|
||||
@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<List<TemporaryBasal>>
|
||||
|
||||
|
|
Loading…
Reference in a new issue