|
|
@@ -847,56 +847,62 @@ extension Treatments.StateModel {
|
|
|
}
|
|
|
|
|
|
private func mapForecastsForChart() async -> Determination? {
|
|
|
- do {
|
|
|
- let determinationFetchContext = CoreDataStack.shared.newTaskContext()
|
|
|
- determinationFetchContext.name = "TreatmentsStateModel.mapForecastsForChart"
|
|
|
-
|
|
|
- let determinationObjects: [OrefDetermination] = try await CoreDataStack.shared
|
|
|
- .getNSManagedObject(with: determinationObjectIDs, context: determinationFetchContext)
|
|
|
+ guard let determinationID = await MainActor.run(body: { determinationObjectIDs.first }) else {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
|
|
|
- let determination = await determinationFetchContext.perform {
|
|
|
- let determinationObject = determinationObjects.first
|
|
|
+ let context = CoreDataStack.shared.newTaskContext()
|
|
|
+ context.name = "TreatmentsStateModel.mapForecastsForChart"
|
|
|
|
|
|
- let forecastsSet = determinationObject?.forecasts ?? []
|
|
|
- let predictions = Predictions(
|
|
|
- iob: forecastsSet.extractValues(for: "iob"),
|
|
|
- zt: forecastsSet.extractValues(for: "zt"),
|
|
|
- cob: forecastsSet.extractValues(for: "cob"),
|
|
|
- uam: forecastsSet.extractValues(for: "uam")
|
|
|
- )
|
|
|
+ return await context.perform {
|
|
|
+ let request = NSFetchRequest<Forecast>(entityName: "Forecast")
|
|
|
+ request.predicate = NSPredicate(format: "orefDetermination = %@", determinationID)
|
|
|
+ request.relationshipKeyPathsForPrefetching = ["forecastValues"]
|
|
|
|
|
|
- return Determination(
|
|
|
- id: UUID(),
|
|
|
- reason: "",
|
|
|
- units: 0,
|
|
|
- insulinReq: 0,
|
|
|
- sensitivityRatio: 0,
|
|
|
- rate: 0,
|
|
|
- duration: 0,
|
|
|
- iob: 0,
|
|
|
- cob: 0,
|
|
|
- predictions: predictions.isEmpty ? nil : predictions,
|
|
|
- carbsReq: 0,
|
|
|
- temp: nil,
|
|
|
- reservoir: 0,
|
|
|
- insulinForManualBolus: 0,
|
|
|
- manualBolusErrorString: 0,
|
|
|
- carbRatio: 0,
|
|
|
- received: false
|
|
|
+ let forecasts: [Forecast]
|
|
|
+ do {
|
|
|
+ forecasts = try context.fetch(request)
|
|
|
+ } catch {
|
|
|
+ debug(
|
|
|
+ .default,
|
|
|
+ "\(DebuggingIdentifiers.failed) Error mapping forecasts for chart: \(error)"
|
|
|
)
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
- guard !determinationObjects.isEmpty else {
|
|
|
- return nil
|
|
|
+ func values(for type: String) -> [Int]? {
|
|
|
+ let result = forecasts.first { $0.type == type }?
|
|
|
+ .forecastValuesArray
|
|
|
+ .map { Int($0.value) }
|
|
|
+ return (result?.isEmpty ?? true) ? nil : result
|
|
|
}
|
|
|
|
|
|
- return determination
|
|
|
- } catch {
|
|
|
- debug(
|
|
|
- .default,
|
|
|
- "\(DebuggingIdentifiers.failed) Error mapping forecasts for chart: \(error)"
|
|
|
+ let predictions = Predictions(
|
|
|
+ iob: values(for: "iob"),
|
|
|
+ zt: values(for: "zt"),
|
|
|
+ cob: values(for: "cob"),
|
|
|
+ uam: values(for: "uam")
|
|
|
+ )
|
|
|
+
|
|
|
+ return Determination(
|
|
|
+ id: UUID(),
|
|
|
+ reason: "",
|
|
|
+ units: 0,
|
|
|
+ insulinReq: 0,
|
|
|
+ sensitivityRatio: 0,
|
|
|
+ rate: 0,
|
|
|
+ duration: 0,
|
|
|
+ iob: 0,
|
|
|
+ cob: 0,
|
|
|
+ predictions: predictions.isEmpty ? nil : predictions,
|
|
|
+ carbsReq: 0,
|
|
|
+ temp: nil,
|
|
|
+ reservoir: 0,
|
|
|
+ insulinForManualBolus: 0,
|
|
|
+ manualBolusErrorString: 0,
|
|
|
+ carbRatio: 0,
|
|
|
+ received: false
|
|
|
)
|
|
|
- return nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -988,16 +994,6 @@ extension Treatments.StateModel {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-private extension Set where Element == Forecast {
|
|
|
- func extractValues(for type: String) -> [Int]? {
|
|
|
- let values = first { $0.type == type }?
|
|
|
- .forecastValues?
|
|
|
- .sorted { $0.index < $1.index }
|
|
|
- .compactMap { Int($0.value) }
|
|
|
- return values?.isEmpty ?? true ? nil : values
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
private extension Predictions {
|
|
|
var isEmpty: Bool {
|
|
|
iob == nil && zt == nil && cob == nil && uam == nil
|