improve TDD calculation and StatsActivity

This commit is contained in:
Milos Kozak 2022-11-30 15:59:04 +01:00
parent 7d87e95037
commit 0ff8161cda
5 changed files with 99 additions and 40 deletions

View file

@ -127,7 +127,7 @@ import kotlin.math.roundToInt
return ret.toString() return ret.toString()
} }
fun clearCachedData(from: Long) { fun clearCachedTddData(from: Long) {
database.totalDailyDoseDao.deleteNewerThan(from, InterfaceIDs.PumpType.CACHE) database.totalDailyDoseDao.deleteNewerThan(from, InterfaceIDs.PumpType.CACHE)
} }
@ -863,7 +863,7 @@ import kotlin.math.roundToInt
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.toWrappedSingle() .toWrappedSingle()
fun createTotalDailyDose(tdd: TotalDailyDose) { fun insertTotalDailyDose(tdd: TotalDailyDose) {
database.totalDailyDoseDao.insert(tdd) database.totalDailyDoseDao.insert(tdd)
} }

View file

@ -41,7 +41,7 @@ class TddCalculatorImpl @Inject constructor(
override fun calculate(days: Long): LongSparseArray<TotalDailyDose> { override fun calculate(days: Long): LongSparseArray<TotalDailyDose> {
var startTime = MidnightTime.calc(dateUtil.now() - T.days(days).msecs()) var startTime = MidnightTime.calc(dateUtil.now() - T.days(days).msecs())
val endTime = MidnightTime.calc(dateUtil.now()) val endTime = MidnightTime.calc(dateUtil.now())
val stepSize = T.hours(24).msecs() //val stepSize = T.hours(24).msecs() // this is not true on DST change
val result = LongSparseArray<TotalDailyDose>() val result = LongSparseArray<TotalDailyDose>()
// Try to load cached values // Try to load cached values
@ -49,21 +49,24 @@ class TddCalculatorImpl @Inject constructor(
val tdd = repository.getCalculatedTotalDailyDose(startTime).blockingGet() val tdd = repository.getCalculatedTotalDailyDose(startTime).blockingGet()
if (tdd is ValueWrapper.Existing) result.put(startTime, tdd.value) if (tdd is ValueWrapper.Existing) result.put(startTime, tdd.value)
else break else break
startTime += stepSize //startTime += stepSize
startTime = MidnightTime.calc(startTime + T.hours(27).msecs()) // be sure we find correct midnight
} }
if (endTime > startTime) { if (endTime > startTime) {
for (midnight in startTime until endTime step stepSize) { var midnight = startTime
val tdd = calculate(midnight, midnight + stepSize) while (midnight < endTime) {
val tdd = calculate(midnight, midnight + T.hours(24).msecs())
result.put(midnight, tdd) result.put(midnight, tdd)
midnight = MidnightTime.calc(midnight + T.hours(27).msecs()) // be sure we find correct midnight
} }
} }
for (i in 0 until result.size()) { for (i in 0 until result.size()) {
val tdd = result.valueAt(i) val tdd = result.valueAt(i)
if (tdd.interfaceIDs.pumpType != InterfaceIDs.PumpType.CACHE) { if (tdd.interfaceIDs.pumpType != InterfaceIDs.PumpType.CACHE) {
tdd.interfaceIDs.pumpType = InterfaceIDs.PumpType.CACHE tdd.interfaceIDs.pumpType = InterfaceIDs.PumpType.CACHE
aapsLogger.debug(LTag.CORE, "Storing TDD $tdd") aapsLogger.debug(LTag.CORE, "Storing TDD ${tdd.timestamp}")
repository.createTotalDailyDose(tdd) repository.insertTotalDailyDose(tdd)
} }
} }
return result return result

View file

@ -2,12 +2,15 @@ package info.nightscout.ui.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.widget.TextView import android.widget.TextView
import dagger.android.support.DaggerAppCompatActivity import dagger.android.support.DaggerAppCompatActivity
import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.core.ui.dialogs.OKDialog
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.database.entities.UserEntry.Action import info.nightscout.database.entities.UserEntry.Action
import info.nightscout.database.entities.UserEntry.Sources import info.nightscout.database.entities.UserEntry.Sources
import info.nightscout.database.impl.AppRepository
import info.nightscout.interfaces.logging.UserEntryLogger import info.nightscout.interfaces.logging.UserEntryLogger
import info.nightscout.interfaces.stats.DexcomTirCalculator import info.nightscout.interfaces.stats.DexcomTirCalculator
import info.nightscout.interfaces.stats.TddCalculator import info.nightscout.interfaces.stats.TddCalculator
@ -32,9 +35,11 @@ class StatsActivity : DaggerAppCompatActivity() {
@Inject lateinit var aapsSchedulers: AapsSchedulers @Inject lateinit var aapsSchedulers: AapsSchedulers
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var rh: ResourceHelper @Inject lateinit var rh: ResourceHelper
@Inject lateinit var repository: AppRepository
private lateinit var binding: ActivityStatsBinding private lateinit var binding: ActivityStatsBinding
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -75,14 +80,23 @@ class StatsActivity : DaggerAppCompatActivity() {
binding.activity.addView(it) binding.activity.addView(it)
}, fabricPrivacy::logException) }, fabricPrivacy::logException)
binding.ok.setOnClickListener { finish() } binding.close.setOnClickListener { finish() }
binding.reset.setOnClickListener { binding.resetActivity.setOnClickListener {
OKDialog.showConfirmation(this, rh.gs(R.string.do_you_want_reset_stats)) { OKDialog.showConfirmation(this, rh.gs(R.string.do_you_want_reset_stats)) {
uel.log(Action.STAT_RESET, Sources.Stats) uel.log(Action.STAT_RESET, Sources.Stats)
activityMonitor.reset() activityMonitor.reset()
recreate() recreate()
} }
} }
binding.resetTdd.setOnClickListener {
OKDialog.showConfirmation(this, rh.gs(R.string.do_you_want_reset_tdd_stats)) {
handler.post {
uel.log(Action.STAT_RESET, Sources.Stats)
repository.clearCachedTddData(0)
runOnUiThread { recreate() }
}
}
}
} }
override fun onPause() { override fun onPause() {

View file

@ -60,6 +60,11 @@
app:cardUseCompatPadding="false" app:cardUseCompatPadding="false"
app:contentPadding="2dp"> app:contentPadding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout <LinearLayout
android:id="@+id/tdds" android:id="@+id/tdds"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -67,6 +72,26 @@
android:layout_marginStart="10dp" android:layout_marginStart="10dp"
android:orientation="vertical" /> android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:background="@android:color/transparent"
android:gravity="end"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/reset_tdd"
style="@style/OkCancelButton.Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/reset" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
@ -129,6 +154,11 @@
app:cardUseCompatPadding="false" app:cardUseCompatPadding="false"
app:contentPadding="2dp"> app:contentPadding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout <LinearLayout
android:id="@+id/activity" android:id="@+id/activity"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -136,10 +166,7 @@
android:layout_marginStart="10dp" android:layout_marginStart="10dp"
android:orientation="vertical" /> android:orientation="vertical" />
</com.google.android.material.card.MaterialCardView>
<LinearLayout <LinearLayout
android:id="@+id/done_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
@ -148,20 +175,34 @@
android:orientation="horizontal"> android:orientation="horizontal">
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/reset" android:id="@+id/reset_activity"
style="@style/OkCancelButton.Text" style="@style/OkCancelButton.Text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:text="@string/reset" /> android:text="@string/reset" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:background="@android:color/transparent"
android:gravity="end"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/ok" android:id="@+id/close"
style="@style/OkCancelButton.Text" style="@style/OkCancelButton.Text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:text="@string/ok" /> android:text="@string/close" />
</LinearLayout> </LinearLayout>

View file

@ -20,6 +20,7 @@
<!-- Activities--> <!-- Activities-->
<string name="activity_monitor">Activity monitor</string> <string name="activity_monitor">Activity monitor</string>
<string name="do_you_want_reset_stats">Do you want to reset activity stats?</string> <string name="do_you_want_reset_stats">Do you want to reset activity stats?</string>
<string name="do_you_want_reset_tdd_stats">Do you want to reset TDD stats?</string>
<string name="statistics">Statistics</string> <string name="statistics">Statistics</string>
<string name="calculation_in_progress">Calculation in progress</string> <string name="calculation_in_progress">Calculation in progress</string>
<string name="invalid_age">Invalid age entry</string> <string name="invalid_age">Invalid age entry</string>