Kaynağa Gözat

Use type Integer for rounding of TIR etc.

Jon Mårtensson 3 yıl önce
ebeveyn
işleme
90119f3a2b

+ 80 - 27
FreeAPS/Sources/APS/APSManager.swift

@@ -740,7 +740,7 @@ final class BaseAPSManager: APSManager, Injectable {
 
         var bgAvg = bg / nr_bgs
         // Round to two decimals
-        bgAvg = Decimal(round(Double(bgAvg * 100)) / 100)
+        bgAvg = Decimal(round(Double(bgAvg)))
 
         var algo_ = "oref0"
         if preferences.enableChris, preferences.useNewFormula {
@@ -768,20 +768,29 @@ final class BaseAPSManager: APSManager, Injectable {
         let cgm = settingsManager.settings.cgm
         let file = OpenAPS.Monitor.dailyStats
         let date_ = Date()
+        var iPa: Decimal = 75
+        if preferences.useCustomPeakTime {
+            iPa = preferences.insulinPeakTime
+        } else if preferences.curve.rawValue == "rapid-acting" {
+            iPa = 65
+        } else if preferences.curve.rawValue == "ultra-rapid" {
+            iPa = 50
+        }
 
         let dailystat = DailyStats(
             date: date_,
             Pump: pump_,
             CGM: cgm.rawValue,
-            TIR_Percentage: Decimal(tir().tir),
-            Hypoglucemias_Percentage: Decimal(tir().hypos),
-            Hyperglucemias_Percentage: Decimal(tir().hypers),
+            TIR_Percentage: tir().TIR,
+            Hypoglucemias_Percentage: tir().hypos,
+            Hyperglucemias_Percentage: tir().hypers,
             BG_daily_Average: bgAvg,
             TDD: currentTDD ?? 0,
             Carbs_24h: carbTotal,
             Algorithm: algo_,
             AdjustmentFactor: af,
             insulinType: insulin_type.rawValue,
+            peakActivityTime: iPa,
             FAX_Build_Version: version,
             FAX_Build_Date: buildDate,
             id: UUID().uuidString
@@ -791,48 +800,92 @@ final class BaseAPSManager: APSManager, Injectable {
         if file.rawJSON.algo == nil {
             storage.save(dailystat, as: file)
         } else {
-            var items: [DailyStats] = []
+            // var items: [DailyStats] = []
+
+            let file_2 = storage.retrieve(OpenAPS.Monitor.dailyStats, as: [DailyStats].self)
+            let isDateOldEnough = file_2?[0].date
+
+            if date_ > (isDateOldEnough?.addingTimeInterval(-1.days.timeInterval))! {
+                storage.transaction { storage in
+                    // storage.append(dailystat, to: file, uniqBy: \.id)
+                    // .filter { $0.date.addingTimeInterval(-1.days.timeInterval) < Date() }
+                    // .sorted { $0.date > $1.date } ?? []
 
-            storage.transaction { storage in
-                storage.append(dailystat, to: file, uniqBy: \.id)
-                items = storage.retrieve(file, as: [DailyStats].self)?
-                    .filter { $0.date.addingTimeInterval(-1435.minutes.timeInterval) < Date() }
-                    .sorted { $0.date > $1.date } ?? []
-                storage.save(Array(items), as: file)
+                    storage.retrieve(file, as: [DailyStats].self)?
+                        .sorted { $0.date > $1.date } ?? []
+                    storage.append(Array(arrayLiteral: dailystat), to: file, uniqBy: \.id)
+
+                    // storage.append(dailystat, to: file, uniqBy: \.id)
+                    // storage.append(Array(items), to: file, uniqBy: \.id)
+                    storage.save(Array(arrayLiteral: dailystat), as: file)
+                    // storage.save(file, as: OpenAPS.Monitor.dailyStats)
+                }
             }
         }
     }
 
-    func tir() -> (hypos: Double, hypers: Double, tir: Double) {
+    // Time In Range (%)
+    func tir() -> (hypos: Int, hypers: Int, TIR: Int) {
         let glucose = storage.retrieve(OpenAPS.Monitor.glucose, as: [BloodGlucose].self)
         let length_ = glucose!.count
         let endIndex = length_ - 1
         let fullTime = glucose![0].date - glucose![endIndex].date
-        var timeInHypo = 0.0
-        var timeInHyper = 0.0
+
+        // If empty json
+        guard fullTime != 0 else {
+            return (0, 0, 0)
+        }
+
+        var timeInHypo: Decimal = 0
+        var timeInHyper: Decimal = 0
+        var hypos: Decimal = 0
+        var hypers: Decimal = 0
         var i = 0
+        var currentTime = glucose![i].date
 
-        repeat {
-            let currentTime = glucose![i].date
+        while i < endIndex {
+            if i == 0 {
+                currentTime = glucose![0].date
+            } else {
+                currentTime = glucose![i].date
+            }
             var x = i
+
             while x < endIndex {
-                x += 1
-                if x < endIndex {
-                    if glucose![x].glucose! < 80 {
-                        timeInHypo += Double(currentTime - glucose![x].date)
-                    } else if glucose![x].glucose! > 200 {
-                        timeInHyper += Double(currentTime - glucose![x].date)
+                if x <= endIndex {
+                    if glucose![x].glucose! < 72 {
+                        timeInHypo += currentTime - glucose![x].date
+                        currentTime = glucose![x].date
+                        x += 1
+                        continue
+                    } else if glucose![x].glucose! > 180 {
+                        timeInHyper += currentTime - glucose![x].date
+                        currentTime = glucose![x].date
+                        x += 1
+                        continue
                     } else { break }
                 }
             }
             i += 1
-        } while i <= endIndex
+        }
+
+        if timeInHypo == 0 {
+            hypos = 0
+        } else { hypos = (timeInHypo / fullTime) * 100
+        }
 
-        let hypos = (timeInHypo / Double(fullTime)) * 100
-        let hypers = (timeInHyper / Double(fullTime)) * 100
-        let tir = 100 - (round(hypos) + round(hypers))
+        if timeInHyper == 0 {
+            hypers = 0
+        } else { hypers = (timeInHyper / fullTime) * 100
+        }
+
+        let TIR = 100 - (hypos + hypers)
+
+        print(
+            "Total time: \(fullTime), Hypo time: \(timeInHypo), Hyper time: \(timeInHyper), Hypos: \(hypos) %, Hypers: \(hypers) % and TIR: \(TIR) %, Current Time: \(currentTime)"
+        )
 
-        return (round(hypos), round(hypers), round(tir))
+        return (Int(hypos), Int(hypers), Int(TIR))
     }
 
     private func processError(_ error: Error) {

+ 10 - 6
FreeAPS/Sources/Models/DailyStats.swift

@@ -4,15 +4,16 @@ struct DailyStats: JSON, Equatable {
     var date: Date
     var Pump: String
     var CGM: String
-    var TIR_Percentage: Decimal
-    var Hypoglucemias_Percentage: Decimal
-    var Hyperglucemias_Percentage: Decimal
+    var TIR_Percentage: Int
+    var Hypoglucemias_Percentage: Int
+    var Hyperglucemias_Percentage: Int
     var BG_daily_Average: Decimal
     var TDD: Decimal
     var Carbs_24h: Decimal
     var Algorithm: String
     var AdjustmentFactor: Decimal
     var insulinType: String
+    var peakActivityTime: Decimal
     var FAX_Build_Version: String
     var FAX_Build_Date: Date
     var id: String
@@ -21,15 +22,16 @@ struct DailyStats: JSON, Equatable {
         date: Date,
         Pump: String,
         CGM: String,
-        TIR_Percentage: Decimal,
-        Hypoglucemias_Percentage: Decimal,
-        Hyperglucemias_Percentage: Decimal,
+        TIR_Percentage: Int,
+        Hypoglucemias_Percentage: Int,
+        Hyperglucemias_Percentage: Int,
         BG_daily_Average: Decimal,
         TDD: Decimal,
         Carbs_24h: Decimal,
         Algorithm: String,
         AdjustmentFactor: Decimal,
         insulinType: String,
+        peakActivityTime: Decimal,
         FAX_Build_Version: String,
         FAX_Build_Date: Date,
         id: String
@@ -46,6 +48,7 @@ struct DailyStats: JSON, Equatable {
         self.Algorithm = Algorithm
         self.AdjustmentFactor = AdjustmentFactor
         self.insulinType = insulinType
+        self.peakActivityTime = peakActivityTime
         self.FAX_Build_Version = FAX_Build_Version
         self.FAX_Build_Date = FAX_Build_Date
         self.id = id
@@ -66,6 +69,7 @@ extension DailyStats {
         case Algorithm
         case AdjustmentFactor
         case insulinType
+        case peakActivityTime
         case FAX_Build_Version
         case FAX_Build_Date
         case id