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()
}
fun clearCachedData(from: Long) {
fun clearCachedTddData(from: Long) {
database.totalDailyDoseDao.deleteNewerThan(from, InterfaceIDs.PumpType.CACHE)
}
@ -863,7 +863,7 @@ import kotlin.math.roundToInt
.subscribeOn(Schedulers.io())
.toWrappedSingle()
fun createTotalDailyDose(tdd: TotalDailyDose) {
fun insertTotalDailyDose(tdd: TotalDailyDose) {
database.totalDailyDoseDao.insert(tdd)
}

View file

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

View file

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

View file

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

View file

@ -20,6 +20,7 @@
<!-- Activities-->
<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_tdd_stats">Do you want to reset TDD stats?</string>
<string name="statistics">Statistics</string>
<string name="calculation_in_progress">Calculation in progress</string>
<string name="invalid_age">Invalid age entry</string>