calculate tir

This commit is contained in:
Milos Kozak 2019-10-22 13:24:35 +02:00
parent 5d17916e73
commit e566e9debc
2 changed files with 28 additions and 3 deletions

View file

@ -1,5 +1,14 @@
package info.nightscout.androidaps.utils
class TIR(val low: Double, val high: Double) {
class TIR(val date: Long, val lowThreshold: Double, val highThreshold: Double) {
private var below = 0;
private var inRange = 0;
private var above = 0;
private var error = 0;
private var count = 0;
fun error() = run { error++ }
fun below() = run { below++; count++ }
fun inRange() = run { inRange++; count++ }
fun above() = run { above++; count++ }
}

View file

@ -1,9 +1,25 @@
package info.nightscout.androidaps.utils
import android.util.LongSparseArray
import info.nightscout.androidaps.MainApp
object TirCalculator {
fun calculate(days: Long): LongSparseArray<TIR> {
return LongSparseArray()
fun calculate(days: Long, lowMgdl: Double, highMgdl: Double): LongSparseArray<TIR> {
if (lowMgdl < 39) throw RuntimeException("Low below 39")
if (lowMgdl > highMgdl) throw RuntimeException("Low > High")
val startTime = MidnightTime.calc(DateUtil.now()) - T.days(days).msecs()
val endTime = MidnightTime.calc(DateUtil.now())
val bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(startTime, endTime, true)
val result = LongSparseArray<TIR>()
for (bg in bgReadings) {
val midnight = MidnightTime.calc(bg.date)
val tir = result[midnight] ?: TIR(midnight, lowMgdl, highMgdl)
if (bg.value < 39) tir.error()
if (bg.value >= 39 && bg.value < lowMgdl) tir.below()
if (bg.value in lowMgdl..highMgdl) tir.inRange()
if (bg.value > highMgdl) tir.inRange()
}
return result
}
}