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) {
calculationWorkflow.runCalculation(
CalculationWorkflow.HISTORY_CALCULATION,
historyBrowserData.iobCobCalculator,
historyBrowserData.overviewData,
from,
historyBrowserData.overviewData.toTime,
job = CalculationWorkflow.HISTORY_CALCULATION,
iobCobCalculator = historyBrowserData.iobCobCalculator,
overviewData = historyBrowserData.overviewData,
reason = from,
end = historyBrowserData.overviewData.toTime,
bgDataReload = true,
limitDataToOldestAvailable = false,
cause = EventCustomCalculationFinished(),
runLoop = false
cause = EventCustomCalculationFinished()
)
}

View file

@ -110,9 +110,7 @@ class CalculationWorkflowImpl @Inject constructor(
"onEventAppInitialized",
System.currentTimeMillis(),
bgDataReload = true,
limitDataToOldestAvailable = true,
cause = it,
runLoop = true
cause = it
)
},
fabricPrivacy::logException
@ -133,14 +131,12 @@ class CalculationWorkflowImpl @Inject constructor(
job: String,
iobCobCalculator: IobCobCalculator,
overviewData: OverviewData,
from: String,
reason: String,
end: Long,
bgDataReload: Boolean,
limitDataToOldestAvailable: Boolean,
cause: Event?,
runLoop: Boolean
cause: Event?
) {
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)
.beginUniqueWork(
@ -186,11 +182,11 @@ class CalculationWorkflowImpl @Inject constructor(
.then(
if (activePlugin.activeSensitivity.isOref1)
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()
else
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()
)
.then(OneTimeWorkRequest.Builder(UpdateIobCobSensWorker::class.java).build())
@ -205,19 +201,20 @@ class CalculationWorkflowImpl @Inject constructor(
.build()
)
.then(
runLoop,
job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(InvokeLoopWorker::class.java)
.setInputData(dataWorkerStorage.storeInputData(InvokeLoopWorker.InvokeLoopData(cause)))
.build()
)
.then(
runLoop,
job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(PreparePredictionsWorker::class.java)
.setInputData(dataWorkerStorage.storeInputData(PreparePredictionsWorker.PreparePredictionsData(overviewData)))
.build()
)
.then(
runLoop, OneTimeWorkRequest.Builder(UpdateGraphWorker::class.java)
job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(UpdateGraphWorker::class.java)
.setInputData(Data.Builder().putString(JOB, job).build())
.build()
)

View file

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

View file

@ -150,15 +150,13 @@ class IobCobCalculatorPlugin @Inject constructor(
clearCache()
ads.reset()
calculationWorkflow.runCalculation(
CalculationWorkflow.MAIN_CALCULATION,
this,
overviewData,
reason,
System.currentTimeMillis(),
job = CalculationWorkflow.MAIN_CALCULATION,
iobCobCalculator = this,
overviewData = overviewData,
reason = reason,
end = System.currentTimeMillis(),
bgDataReload = false,
limitDataToOldestAvailable = true,
cause = event,
runLoop = true
cause = event
)
}
@ -447,7 +445,15 @@ class IobCobCalculatorPlugin @Inject constructor(
}
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");
}

View file

@ -67,7 +67,7 @@ class IobCobOref1Worker(
class IobCobOref1WorkerData(
val injector: HasAndroidInjector,
val iobCobCalculator: IobCobCalculator, // cannot be injected : HistoryBrowser uses different instance
val from: String,
val reason: String,
val end: Long,
val limitDataToOldestAvailable: Boolean,
val cause: Event?
@ -80,9 +80,9 @@ class IobCobOref1Worker(
val start = dateUtil.now()
try {
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.from}")
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.reason}")
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"))
}
//log.debug("Locking calculateSensitivityData");
@ -92,8 +92,8 @@ class IobCobOref1Worker(
val bucketedData = ads.bucketedData
val autosensDataTable = ads.autosensDataTable
if (bucketedData == null || bucketedData.size < 3) {
aapsLogger.debug(LTag.AUTOSENS, {"Aborting calculation thread (No bucketed data available): ${data.from}"})
return Result.success(workDataOf("Error" to "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.reason}"))
}
val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp)
aapsLogger.debug(LTag.AUTOSENS, {"Prev data time: " + dateUtil.dateAndTimeString(prevDataTime)})
@ -102,8 +102,8 @@ class IobCobOref1Worker(
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))
if (isStopped) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (trigger): ${data.from}")
return Result.failure(workDataOf("Error" to "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.reason}"))
}
// check if data already exists
var bgTime = bucketedData[i].timestamp
@ -116,10 +116,10 @@ class IobCobOref1Worker(
}
val profile = profileFunction.getProfile(bgTime)
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
}
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 autosensData = AutosensDataObject(data.injector)
autosensData.time = bgTime
@ -330,7 +330,7 @@ class IobCobOref1Worker(
}.start()
} finally {
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)
}
return Result.success()

View file

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