simplify code

This commit is contained in:
Milos Kozak 2022-12-02 23:03:30 +01:00
parent 6172ca8d63
commit 42f1940abc
6 changed files with 53 additions and 54 deletions

View file

@ -257,15 +257,13 @@ class HistoryBrowseActivity : DaggerAppCompatActivity() {
private fun runCalculation(from: String) { private fun runCalculation(from: String) {
calculationWorkflow.runCalculation( calculationWorkflow.runCalculation(
CalculationWorkflow.HISTORY_CALCULATION, job = CalculationWorkflow.HISTORY_CALCULATION,
historyBrowserData.iobCobCalculator, iobCobCalculator = historyBrowserData.iobCobCalculator,
historyBrowserData.overviewData, overviewData = historyBrowserData.overviewData,
from, reason = from,
historyBrowserData.overviewData.toTime, end = historyBrowserData.overviewData.toTime,
bgDataReload = true, bgDataReload = true,
limitDataToOldestAvailable = false, cause = EventCustomCalculationFinished()
cause = EventCustomCalculationFinished(),
runLoop = false
) )
} }

View file

@ -110,9 +110,7 @@ class CalculationWorkflowImpl @Inject constructor(
"onEventAppInitialized", "onEventAppInitialized",
System.currentTimeMillis(), System.currentTimeMillis(),
bgDataReload = true, bgDataReload = true,
limitDataToOldestAvailable = true, cause = it
cause = it,
runLoop = true
) )
}, },
fabricPrivacy::logException fabricPrivacy::logException
@ -133,14 +131,12 @@ class CalculationWorkflowImpl @Inject constructor(
job: String, job: String,
iobCobCalculator: IobCobCalculator, iobCobCalculator: IobCobCalculator,
overviewData: OverviewData, overviewData: OverviewData,
from: String, reason: String,
end: Long, end: Long,
bgDataReload: Boolean, bgDataReload: Boolean,
limitDataToOldestAvailable: Boolean, cause: Event?
cause: Event?,
runLoop: Boolean
) { ) {
aapsLogger.debug(LTag.AUTOSENS, "Starting calculation worker: $from to ${dateUtil.dateAndTimeAndSecondsString(end)}") aapsLogger.debug(LTag.AUTOSENS, "Starting calculation worker: $reason to ${dateUtil.dateAndTimeAndSecondsString(end)}")
WorkManager.getInstance(context) WorkManager.getInstance(context)
.beginUniqueWork( .beginUniqueWork(
@ -186,11 +182,11 @@ class CalculationWorkflowImpl @Inject constructor(
.then( .then(
if (activePlugin.activeSensitivity.isOref1) if (activePlugin.activeSensitivity.isOref1)
OneTimeWorkRequest.Builder(IobCobOref1Worker::class.java) OneTimeWorkRequest.Builder(IobCobOref1Worker::class.java)
.setInputData(dataWorkerStorage.storeInputData(IobCobOref1Worker.IobCobOref1WorkerData(injector, iobCobCalculator, from, end, limitDataToOldestAvailable, cause))) .setInputData(dataWorkerStorage.storeInputData(IobCobOref1Worker.IobCobOref1WorkerData(injector, iobCobCalculator, reason, end, job == MAIN_CALCULATION, cause)))
.build() .build()
else else
OneTimeWorkRequest.Builder(IobCobOrefWorker::class.java) OneTimeWorkRequest.Builder(IobCobOrefWorker::class.java)
.setInputData(dataWorkerStorage.storeInputData(IobCobOrefWorker.IobCobOrefWorkerData(injector, iobCobCalculator, from, end, limitDataToOldestAvailable, cause))) .setInputData(dataWorkerStorage.storeInputData(IobCobOrefWorker.IobCobOrefWorkerData(injector, iobCobCalculator, reason, end, job == MAIN_CALCULATION, cause)))
.build() .build()
) )
.then(OneTimeWorkRequest.Builder(UpdateIobCobSensWorker::class.java).build()) .then(OneTimeWorkRequest.Builder(UpdateIobCobSensWorker::class.java).build())
@ -205,19 +201,20 @@ class CalculationWorkflowImpl @Inject constructor(
.build() .build()
) )
.then( .then(
runLoop, job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(InvokeLoopWorker::class.java) OneTimeWorkRequest.Builder(InvokeLoopWorker::class.java)
.setInputData(dataWorkerStorage.storeInputData(InvokeLoopWorker.InvokeLoopData(cause))) .setInputData(dataWorkerStorage.storeInputData(InvokeLoopWorker.InvokeLoopData(cause)))
.build() .build()
) )
.then( .then(
runLoop, job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(PreparePredictionsWorker::class.java) OneTimeWorkRequest.Builder(PreparePredictionsWorker::class.java)
.setInputData(dataWorkerStorage.storeInputData(PreparePredictionsWorker.PreparePredictionsData(overviewData))) .setInputData(dataWorkerStorage.storeInputData(PreparePredictionsWorker.PreparePredictionsData(overviewData)))
.build() .build()
) )
.then( .then(
runLoop, OneTimeWorkRequest.Builder(UpdateGraphWorker::class.java) job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(UpdateGraphWorker::class.java)
.setInputData(Data.Builder().putString(JOB, job).build()) .setInputData(Data.Builder().putString(JOB, job).build())
.build() .build()
) )

View file

@ -33,11 +33,9 @@ interface CalculationWorkflow {
job: String, job: String,
iobCobCalculator: IobCobCalculator, iobCobCalculator: IobCobCalculator,
overviewData: OverviewData, overviewData: OverviewData,
from: String, reason: String,
end: Long, end: Long,
bgDataReload: Boolean, bgDataReload: Boolean,
limitDataToOldestAvailable: Boolean, cause: Event?
cause: Event?,
runLoop: Boolean
) )
} }

View file

@ -150,15 +150,13 @@ class IobCobCalculatorPlugin @Inject constructor(
clearCache() clearCache()
ads.reset() ads.reset()
calculationWorkflow.runCalculation( calculationWorkflow.runCalculation(
CalculationWorkflow.MAIN_CALCULATION, job = CalculationWorkflow.MAIN_CALCULATION,
this, iobCobCalculator = this,
overviewData, overviewData = overviewData,
reason, reason = reason,
System.currentTimeMillis(), end = System.currentTimeMillis(),
bgDataReload = false, bgDataReload = false,
limitDataToOldestAvailable = true, cause = event
cause = event,
runLoop = true
) )
} }
@ -447,7 +445,15 @@ class IobCobCalculatorPlugin @Inject constructor(
} }
ads.newHistoryData(time, aapsLogger, dateUtil) ads.newHistoryData(time, aapsLogger, dateUtil)
} }
calculationWorkflow.runCalculation(CalculationWorkflow.MAIN_CALCULATION, this, overviewData, event.javaClass.simpleName, System.currentTimeMillis(), bgDataReload, true, event, runLoop = true) calculationWorkflow.runCalculation(
job = CalculationWorkflow.MAIN_CALCULATION,
iobCobCalculator = this,
overviewData = overviewData,
reason = event.javaClass.simpleName,
end = System.currentTimeMillis(),
bgDataReload = bgDataReload,
cause = event
)
//log.debug("Releasing onNewHistoryData"); //log.debug("Releasing onNewHistoryData");
} }

View file

@ -67,7 +67,7 @@ class IobCobOref1Worker(
class IobCobOref1WorkerData( class IobCobOref1WorkerData(
val injector: HasAndroidInjector, val injector: HasAndroidInjector,
val iobCobCalculator: IobCobCalculator, // cannot be injected : HistoryBrowser uses different instance val iobCobCalculator: IobCobCalculator, // cannot be injected : HistoryBrowser uses different instance
val from: String, val reason: String,
val end: Long, val end: Long,
val limitDataToOldestAvailable: Boolean, val limitDataToOldestAvailable: Boolean,
val cause: Event? val cause: Event?
@ -80,9 +80,9 @@ class IobCobOref1Worker(
val start = dateUtil.now() val start = dateUtil.now()
try { try {
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.reason}")
if (!profileFunction.isProfileValid("IobCobThread")) { if (!profileFunction.isProfileValid("IobCobThread")) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No profile): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No profile): ${data.reason}")
return Result.success(workDataOf("Error" to "app still initializing")) return Result.success(workDataOf("Error" to "app still initializing"))
} }
//log.debug("Locking calculateSensitivityData"); //log.debug("Locking calculateSensitivityData");
@ -92,8 +92,8 @@ class IobCobOref1Worker(
val bucketedData = ads.bucketedData val bucketedData = ads.bucketedData
val autosensDataTable = ads.autosensDataTable val autosensDataTable = ads.autosensDataTable
if (bucketedData == null || bucketedData.size < 3) { if (bucketedData == null || bucketedData.size < 3) {
aapsLogger.debug(LTag.AUTOSENS, {"Aborting calculation thread (No bucketed data available): ${data.from}"}) aapsLogger.debug(LTag.AUTOSENS, {"Aborting calculation thread (No bucketed data available): ${data.reason}"})
return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.from}")) return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.reason}"))
} }
val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp) val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp)
aapsLogger.debug(LTag.AUTOSENS, {"Prev data time: " + dateUtil.dateAndTimeString(prevDataTime)}) aapsLogger.debug(LTag.AUTOSENS, {"Prev data time: " + dateUtil.dateAndTimeString(prevDataTime)})
@ -102,8 +102,8 @@ class IobCobOref1Worker(
for (i in bucketedData.size - 4 downTo 0) { for (i in bucketedData.size - 4 downTo 0) {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100 - (100.0 * i / bucketedData.size).toInt(), data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100 - (100.0 * i / bucketedData.size).toInt(), data.cause))
if (isStopped) { if (isStopped) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (trigger): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (trigger): ${data.reason}")
return Result.failure(workDataOf("Error" to "Aborting calculation thread (trigger): ${data.from}")) return Result.failure(workDataOf("Error" to "Aborting calculation thread (trigger): ${data.reason}"))
} }
// check if data already exists // check if data already exists
var bgTime = bucketedData[i].timestamp var bgTime = bucketedData[i].timestamp
@ -116,10 +116,10 @@ class IobCobOref1Worker(
} }
val profile = profileFunction.getProfile(bgTime) val profile = profileFunction.getProfile(bgTime)
if (profile == null) { if (profile == null) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (no profile): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (no profile): ${data.reason}")
continue // profile not set yet continue // profile not set yet
} }
aapsLogger.debug(LTag.AUTOSENS, "Processing calculation thread: ${data.from} ($i/${bucketedData.size})") aapsLogger.debug(LTag.AUTOSENS, "Processing calculation thread: ${data.reason} ($i/${bucketedData.size})")
val sens = profile.getIsfMgdl(bgTime) val sens = profile.getIsfMgdl(bgTime)
val autosensData = AutosensDataObject(data.injector) val autosensData = AutosensDataObject(data.injector)
autosensData.time = bgTime autosensData.time = bgTime
@ -330,7 +330,7 @@ class IobCobOref1Worker(
}.start() }.start()
} finally { } finally {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause))
aapsLogger.debug(LTag.AUTOSENS, {"AUTOSENSDATA thread ended: ${data.from}"}) aapsLogger.debug(LTag.AUTOSENS, {"AUTOSENSDATA thread ended: ${data.reason}"})
profiler.log(LTag.AUTOSENS, "IobCobOref1Thread", start) profiler.log(LTag.AUTOSENS, "IobCobOref1Thread", start)
} }
return Result.success() return Result.success()

View file

@ -63,7 +63,7 @@ class IobCobOrefWorker @Inject internal constructor(
class IobCobOrefWorkerData( class IobCobOrefWorkerData(
val injector: HasAndroidInjector, val injector: HasAndroidInjector,
val iobCobCalculatorPlugin: IobCobCalculator, // cannot be injected : HistoryBrowser uses different instance val iobCobCalculatorPlugin: IobCobCalculator, // cannot be injected : HistoryBrowser uses different instance
val from: String, val reason: String,
val end: Long, val end: Long,
val limitDataToOldestAvailable: Boolean, val limitDataToOldestAvailable: Boolean,
val cause: Event? val cause: Event?
@ -75,9 +75,9 @@ class IobCobOrefWorker @Inject internal constructor(
val start = dateUtil.now() val start = dateUtil.now()
try { try {
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.reason}")
if (!profileFunction.isProfileValid("IobCobThread")) { if (!profileFunction.isProfileValid("IobCobThread")) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No profile): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No profile): ${data.reason}")
return Result.success(workDataOf("Error" to "app still initializing")) return Result.success(workDataOf("Error" to "app still initializing"))
} }
//log.debug("Locking calculateSensitivityData"); //log.debug("Locking calculateSensitivityData");
@ -87,8 +87,8 @@ class IobCobOrefWorker @Inject internal constructor(
val bucketedData = ads.bucketedData val bucketedData = ads.bucketedData
val autosensDataTable = ads.autosensDataTable val autosensDataTable = ads.autosensDataTable
if (bucketedData == null || bucketedData.size < 3) { if (bucketedData == null || bucketedData.size < 3) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No bucketed data available): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No bucketed data available): ${data.reason}")
return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.from}")) return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.reason}"))
} }
val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp) val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp)
aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime)) aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime))
@ -97,8 +97,8 @@ class IobCobOrefWorker @Inject internal constructor(
for (i in bucketedData.size - 4 downTo 0) { for (i in bucketedData.size - 4 downTo 0) {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100 - (100.0 * i / bucketedData.size).toInt(), data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100 - (100.0 * i / bucketedData.size).toInt(), data.cause))
if (isStopped) { if (isStopped) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (trigger): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (trigger): ${data.reason}")
return Result.failure(workDataOf("Error" to "Aborting calculation thread (trigger): ${data.from}")) return Result.failure(workDataOf("Error" to "Aborting calculation thread (trigger): ${data.reason}"))
} }
// check if data already exists // check if data already exists
var bgTime = bucketedData[i].timestamp var bgTime = bucketedData[i].timestamp
@ -111,10 +111,10 @@ class IobCobOrefWorker @Inject internal constructor(
} }
val profile = profileFunction.getProfile(bgTime) val profile = profileFunction.getProfile(bgTime)
if (profile == null) { if (profile == null) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (no profile): ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (no profile): ${data.reason}")
continue // profile not set yet continue // profile not set yet
} }
aapsLogger.debug(LTag.AUTOSENS, "Processing calculation thread: ${data.from} ($i/${bucketedData.size})") aapsLogger.debug(LTag.AUTOSENS, "Processing calculation thread: ${data.reason} ($i/${bucketedData.size})")
val sens = profile.getIsfMgdl(bgTime) val sens = profile.getIsfMgdl(bgTime)
val autosensData = AutosensDataObject(data.injector) val autosensData = AutosensDataObject(data.injector)
autosensData.time = bgTime autosensData.time = bgTime
@ -273,7 +273,7 @@ class IobCobOrefWorker @Inject internal constructor(
}.start() }.start()
} finally { } finally {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause))
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread ended: ${data.from}") aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread ended: ${data.reason}")
profiler.log(LTag.AUTOSENS, "IobCobThread", start) profiler.log(LTag.AUTOSENS, "IobCobThread", start)
} }
return Result.success() return Result.success()