|
|
@@ -736,8 +736,8 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
return Double(sorted[length / 2])
|
|
|
}
|
|
|
|
|
|
+ // Add to dailyStats.JSON
|
|
|
private func dailyStats() {
|
|
|
- // Add to dailyStats.JSON
|
|
|
let preferences = settingsManager.preferences
|
|
|
let carbs = storage.retrieve(OpenAPS.Monitor.carbHistory, as: [CarbsEntry].self)
|
|
|
let tdds = storage.retrieve(OpenAPS.Monitor.tdd, as: [TDD].self)
|
|
|
@@ -853,7 +853,7 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
|
|
|
successIs = false
|
|
|
}
|
|
|
- } else if each.loopStatus.contains("Error") {
|
|
|
+ } else if each.loopStatus.contains("Error") || each.loopStatus.contains("APS error") {
|
|
|
errorNR += 1
|
|
|
i += 1
|
|
|
} else { i += 1 }
|
|
|
@@ -881,6 +881,177 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
maximumInt = roundDouble(maximumInt, 1)
|
|
|
}
|
|
|
|
|
|
+ // Time In Range (%) and Average Glucose (24 hours). This looks dumb and I will refactor it later.
|
|
|
+ let glucose = storage.retrieve(OpenAPS.Monitor.glucose, as: [BloodGlucose].self)
|
|
|
+
|
|
|
+ let length_ = glucose?.count ?? 0
|
|
|
+ let endIndex = length_ - 1
|
|
|
+ var oneDayGlucoseIndex = endIndex
|
|
|
+
|
|
|
+ guard length_ != 0 else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var bg: Decimal = 0
|
|
|
+ var bgArray: [Double] = []
|
|
|
+ var medianBG = 0.0
|
|
|
+ var nr_bgs: Decimal = 0
|
|
|
+ let startDate = glucose![0].date
|
|
|
+ var end1 = false
|
|
|
+ var end7 = false
|
|
|
+ var end10 = false
|
|
|
+ var bg_1: Decimal = 0
|
|
|
+ var bg_7: Decimal = 0
|
|
|
+ var bg_10: Decimal = 0
|
|
|
+ var bg_total: Decimal = 0
|
|
|
+ var j = -1
|
|
|
+
|
|
|
+ for entry in glucose! {
|
|
|
+ j += 1
|
|
|
+ if entry.glucose! > 0 {
|
|
|
+ bg += Decimal(entry.glucose!)
|
|
|
+ bgArray.append(Double(entry.glucose!))
|
|
|
+ nr_bgs += 1
|
|
|
+
|
|
|
+ if startDate - entry.date >= 8.64E7, !end1 {
|
|
|
+ end1 = true
|
|
|
+ oneDayGlucoseIndex = j
|
|
|
+ bg_1 = bg / nr_bgs
|
|
|
+ }
|
|
|
+
|
|
|
+ if startDate - entry.date >= 6.045E8, !end7 {
|
|
|
+ end7 = true
|
|
|
+ bg_7 = bg / nr_bgs
|
|
|
+ }
|
|
|
+
|
|
|
+ if startDate - entry.date >= 8.64E8, !end10 {
|
|
|
+ end10 = true
|
|
|
+ bg_10 = bg / nr_bgs
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bg_total = bg / nr_bgs
|
|
|
+
|
|
|
+ medianBG = medianCalculation(array: bgArray)
|
|
|
+
|
|
|
+ let fullTime = glucose![0].date - glucose![endIndex].date
|
|
|
+ let fullTime_1 = glucose![0].date - glucose![oneDayGlucoseIndex].date
|
|
|
+
|
|
|
+ var daysBG = fullTime / 8.64E7
|
|
|
+
|
|
|
+ var timeInHypo: Decimal = 0
|
|
|
+ var timeInHyper: Decimal = 0
|
|
|
+ var hypos: Decimal = 0
|
|
|
+ var hypers: Decimal = 0
|
|
|
+ var i = -1
|
|
|
+ var lastIndex = false
|
|
|
+
|
|
|
+ while i < endIndex {
|
|
|
+ i += 1
|
|
|
+
|
|
|
+ let currentTime = glucose![i].date
|
|
|
+ var previousTime = currentTime
|
|
|
+
|
|
|
+ if i + 1 <= endIndex {
|
|
|
+ previousTime = glucose![i + 1].date
|
|
|
+ } else {
|
|
|
+ lastIndex = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if glucose![i].glucose! < 72, !lastIndex {
|
|
|
+ timeInHypo += currentTime - previousTime
|
|
|
+ } else if glucose![i].glucose! > 180, !lastIndex {
|
|
|
+ timeInHyper += currentTime - previousTime
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if timeInHypo == 0 {
|
|
|
+ hypos = 0
|
|
|
+ } else { hypos = (timeInHypo / fullTime) * 100
|
|
|
+ }
|
|
|
+
|
|
|
+ if timeInHyper == 0 {
|
|
|
+ hypers = 0
|
|
|
+ } else { hypers = (timeInHyper / fullTime) * 100
|
|
|
+ }
|
|
|
+
|
|
|
+ let TIR = 100 - (hypos + hypers)
|
|
|
+
|
|
|
+ // Do the loop again but with for 1 day. I will change this later, because this looks really dumb:
|
|
|
+ var timeInHypo_1: Decimal = 0
|
|
|
+ var timeInHyper_1: Decimal = 0
|
|
|
+ var hypos_1: Decimal = 0
|
|
|
+ var hypers_1: Decimal = 0
|
|
|
+ i = -1
|
|
|
+ lastIndex = false
|
|
|
+
|
|
|
+ while i < oneDayGlucoseIndex {
|
|
|
+ i += 1
|
|
|
+
|
|
|
+ let currentTime = glucose![i].date
|
|
|
+ var previousTime = currentTime
|
|
|
+
|
|
|
+ if i + 1 <= oneDayGlucoseIndex {
|
|
|
+ previousTime = glucose![i + 1].date
|
|
|
+ } else {
|
|
|
+ lastIndex = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if glucose![i].glucose! < 72, !lastIndex {
|
|
|
+ timeInHypo_1 += currentTime - previousTime
|
|
|
+ } else if glucose![i].glucose! > 180, !lastIndex {
|
|
|
+ timeInHyper_1 += currentTime - previousTime
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if timeInHypo_1 == 0 {
|
|
|
+ hypos_1 = 0
|
|
|
+ } else { hypos_1 = (timeInHypo_1 / fullTime_1) * 100
|
|
|
+ }
|
|
|
+
|
|
|
+ if timeInHyper_1 == 0 {
|
|
|
+ hypers_1 = 0
|
|
|
+ } else { hypers_1 = (timeInHyper_1 / fullTime_1) * 100
|
|
|
+ }
|
|
|
+
|
|
|
+ let TIR_1 = 100 - (hypos_1 + hypers_1)
|
|
|
+
|
|
|
+ // Add 10 day average to tenDaysStats.json
|
|
|
+ let file_10 = OpenAPS.Monitor.tenDaysStats
|
|
|
+ // let tensDaysStats = storage.retrieve(file_10, as: [TenDaysStats].self)
|
|
|
+
|
|
|
+ let tenStats = TenDaysStats(
|
|
|
+ createdAt: Date(), past10daysAverage: roundDecimal(bg_10, 1)
|
|
|
+ )
|
|
|
+ var uniqEvents: [TenDaysStats] = []
|
|
|
+ var test1 = uniqEvents
|
|
|
+ let test2: [TenDaysStats] = [tenStats]
|
|
|
+ var countIndeces = 0
|
|
|
+
|
|
|
+ storage.transaction { storage in
|
|
|
+ test1 = storage.retrieve(file_10, as: [TenDaysStats].self) ?? []
|
|
|
+ countIndeces = test1.count
|
|
|
+ }
|
|
|
+
|
|
|
+ if daysBG >= 10 {
|
|
|
+ if countIndeces == 0 {
|
|
|
+ storage.transaction { storage in
|
|
|
+ storage.save(test2, as: file_10)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Keep 10 days apart from each array
|
|
|
+ } else if test1[0].createdAt.addingTimeInterval(10.days.timeInterval) < Date() {
|
|
|
+ storage.transaction { storage in
|
|
|
+ storage.append(tenStats, to: file_10, uniqBy: \.createdAt)
|
|
|
+ uniqEvents = storage.retrieve(file_10, as: [TenDaysStats].self)?
|
|
|
+ .filter { $0.createdAt.addingTimeInterval(365.days.timeInterval) > Date() }
|
|
|
+ .sorted { $0.createdAt > $1.createdAt } ?? []
|
|
|
+ storage.save(Array(uniqEvents), as: file_10)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// Retrieve the 10 days data array
|
|
|
let uniqEvents_1 = storage.retrieve(OpenAPS.Monitor.tenDaysStats, as: [TenDaysStats].self)?
|
|
|
.filter { $0.createdAt.addingTimeInterval(365.days.timeInterval) > Date() }
|
|
|
@@ -905,17 +1076,17 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
}
|
|
|
|
|
|
// HbA1c estimation (%, mmol/mol)
|
|
|
- let NGSPa1CStatisticValue = (46.7 + tir().averageGlucose_1) / 28.7 // NGSP (%)
|
|
|
+ let NGSPa1CStatisticValue = (46.7 + bg_1) / 28.7 // NGSP (%)
|
|
|
let IFCCa1CStatisticValue = 10.929 *
|
|
|
(NGSPa1CStatisticValue - 2.152) // IFCC (mmol/mol) A1C(mmol/mol) = 10.929 * (A1C(%) - 2.15)
|
|
|
// 7 days
|
|
|
- let NGSPa1CStatisticValue_7 = (46.7 + tir().averageGlucose_7) / 28.7
|
|
|
+ let NGSPa1CStatisticValue_7 = (46.7 + bg_7) / 28.7
|
|
|
let IFCCa1CStatisticValue_7 = 10.929 * (NGSPa1CStatisticValue_7 - 2.152)
|
|
|
// 30 days
|
|
|
- let NGSPa1CStatisticValue_30 = (46.7 + thirtyDays) / 28.7
|
|
|
+ let NGSPa1CStatisticValue_30 = (46.7 + bg) / 28.7
|
|
|
let IFCCa1CStatisticValue_30 = 10.929 * (NGSPa1CStatisticValue_30 - 2.152)
|
|
|
// Total days (up t0 10 days)
|
|
|
- let NGSPa1CStatisticValue_total = (46.7 + tir().averageGlucose) / 28.7
|
|
|
+ let NGSPa1CStatisticValue_total = (46.7 + bg_total) / 28.7
|
|
|
let IFCCa1CStatisticValue_total = 10.929 * (NGSPa1CStatisticValue_total - 2.152)
|
|
|
// 90 Days
|
|
|
let NGSPa1CStatisticValue_90 = (46.7 + ninetyDays) / 28.7
|
|
|
@@ -934,22 +1105,20 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
var bgAverageTotalString = ""
|
|
|
var loopString = ""
|
|
|
|
|
|
- let daysBG = tir().daysWithBG
|
|
|
- let avg1 = tir().averageGlucose_1
|
|
|
- let avg7 = tir().averageGlucose_7
|
|
|
- let avgTot = tir().averageGlucose
|
|
|
- let medianTot = tir().medianTotalGlucose
|
|
|
+ // round output values
|
|
|
+ daysBG = roundDecimal(daysBG, 1)
|
|
|
|
|
|
- if avg1 != 0 {
|
|
|
+ if bg_7 != 0 {
|
|
|
bgString1day =
|
|
|
- " Average BG (mmol/l) 24 hours): \(roundDecimal(avg1 * 0.0555, 1)). Average BG (mmg/dl) 24 hours: \(avg1)."
|
|
|
+ " Average BG (mmol/l) 24 hours): \(roundDecimal(bg_1 * 0.0555, 1)). Average BG (mmg/dl) 24 hours: \(roundDecimal(bg_1, 0))."
|
|
|
HbA1c_string_1 =
|
|
|
- "Estimated HbA1c (mmol/mol, 1 day): \(roundDecimal(IFCCa1CStatisticValue, 1)). Estimated HbA1c (%, 1 day): \(roundDecimal(NGSPa1CStatisticValue, 1)). "
|
|
|
+ "Estimated HbA1c (mmol/mol, 1 day): \(roundDecimal(IFCCa1CStatisticValue, 1)). Estimated HbA1c (%, 1 day): \(roundDecimal(NGSPa1CStatisticValue, 0)). "
|
|
|
}
|
|
|
- if avg7 != 0 {
|
|
|
+ if bg_7 != 0 {
|
|
|
string7Days =
|
|
|
" HbA1c 7 days (mmol/mol): \(roundDecimal(IFCCa1CStatisticValue_7, 1)). HbA1c 7 days (%): \(roundDecimal(NGSPa1CStatisticValue_7, 1))."
|
|
|
- bgString7Days = " Average BG (mmol/l) 7 days: \(roundDecimal(avg7 * 0.0555, 1)). Average BG (mg/dl) 7 days: \(avg7)."
|
|
|
+ bgString7Days =
|
|
|
+ " Average BG (mmol/l) 7 days: \(roundDecimal(bg_7 * 0.0555, 1)). Average BG (mg/dl) 7 days: \(roundDecimal(bg_7, 0))."
|
|
|
}
|
|
|
if thirtyDays != 0 {
|
|
|
string30Days =
|
|
|
@@ -964,21 +1133,21 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
" Average BG 90 days (mmol/l): \(roundDecimal(ninetyDays * 0.0555, 1)). Average BG 90 days (mg/dl): \(roundDecimal(ninetyDays, 0)). "
|
|
|
}
|
|
|
|
|
|
- if avgTot != 0, daysBG >= 2 {
|
|
|
+ if bg_total != 0, daysBG >= 2 {
|
|
|
stringTotal =
|
|
|
" HbA1c Total (\(daysBG)) Days (mmol/mol): \(roundDecimal(IFCCa1CStatisticValue_total, 1)). HbA1c Total (\(daysBG)) Days (mg/dl): \(roundDecimal(NGSPa1CStatisticValue_total, 1)) %."
|
|
|
bgAverageTotalString =
|
|
|
- " BG Median Total (\(daysBG)) Days (mmol/l): \(roundDouble(medianTot * 0.0555, 1)). BG Median Total (\(daysBG)) Days (mg/dl): \(medianTot). BG Average Total (\(daysBG)) Days (mmg/dl): \(avgTot)."
|
|
|
+ " BG Median Total (\(daysBG)) Days (mmol/l): \(roundDouble(medianBG * 0.0555, 1)). BG Median Total (\(daysBG)) Days (mg/dl): \(roundDouble(medianBG, 0)). BG Average Total (\(daysBG)) Days (mmg/dl): \(roundDecimal(bg_total, 0))."
|
|
|
}
|
|
|
|
|
|
let HbA1c_string = HbA1c_string_1 + string7Days + string30Days + string90Days + stringTotal
|
|
|
|
|
|
var tirString =
|
|
|
- "TIR (24 hours): \(tir().TIR_1) %. Time with Hypoglucemia: \(tir().hypos_1) % (< 4 / 72). Time with Hyperglucemia: \(tir().hypers_1) % (> 10 / 180)."
|
|
|
+ "TIR (24 hours): \(roundDecimal(TIR_1, 0)) %. Time with Hypoglucemia: \(roundDecimal(hypos_1, 0)) % (< 4 / 72). Time with Hyperglucemia: \(roundDecimal(hypers_1, 0)) % (> 10 / 180)."
|
|
|
|
|
|
if daysBG >= 2 {
|
|
|
tirString +=
|
|
|
- " Total days (\(daysBG)) TIR: \(tir().TIR) %. Time with Hypoglucemia: \(tir().hypos) % (< 4 / 72). Time with Hyperglucemia: \(tir().hypers) % (> 10 / 180)."
|
|
|
+ " Total days (\(daysBG) TIR: \(roundDecimal(TIR, 1)) %. Time with Hypoglucemia: \(roundDecimal(hypos, 1)) % (< 4 / 72). Time with Hyperglucemia: \(roundDecimal(hypers, 1)) % (> 10 / 180)."
|
|
|
}
|
|
|
|
|
|
let bgAverageString = bgString1day + bgString7Days + bgString30Days + bgString90Days + bgAverageTotalString
|
|
|
@@ -1061,196 +1230,6 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Time In Range (%) and Average Glucose (24 hours). This function looks dumb. I will refactor it later.
|
|
|
- private func tir()
|
|
|
- -> (
|
|
|
- averageGlucose: Decimal,
|
|
|
- medianTotalGlucose: Double,
|
|
|
- averageGlucose_1: Decimal,
|
|
|
- averageGlucose_7: Decimal,
|
|
|
- averageGlucose_10: Decimal,
|
|
|
- hypos: Decimal,
|
|
|
- hypers: Decimal,
|
|
|
- TIR: Decimal,
|
|
|
- hypos_1: Decimal,
|
|
|
- hypers_1: Decimal,
|
|
|
- TIR_1: Decimal,
|
|
|
- daysWithBG: Decimal
|
|
|
- )
|
|
|
- {
|
|
|
- let glucose = storage.retrieve(OpenAPS.Monitor.glucose, as: [BloodGlucose].self)
|
|
|
-
|
|
|
- let length_ = glucose?.count ?? 0
|
|
|
- let endIndex = length_ - 1
|
|
|
- var oneDayGlucoseIndex = endIndex
|
|
|
-
|
|
|
- guard length_ != 0 else {
|
|
|
- return (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
|
- }
|
|
|
-
|
|
|
- var bg: Decimal = 0
|
|
|
- var bgArray: [Double] = []
|
|
|
- var medianBG = 0.0
|
|
|
- var nr_bgs: Decimal = 0
|
|
|
- let startDate = glucose![0].date
|
|
|
- var end1 = false
|
|
|
- var end7 = false
|
|
|
- var end10 = false
|
|
|
- var bg_1: Decimal = 0
|
|
|
- var bg_7: Decimal = 0
|
|
|
- var bg_10: Decimal = 0
|
|
|
- var bg_total: Decimal = 0
|
|
|
- var j = -1
|
|
|
-
|
|
|
- for entry in glucose! {
|
|
|
- j += 1
|
|
|
- if entry.glucose! > 0 {
|
|
|
- bg += Decimal(entry.glucose!)
|
|
|
- bgArray.append(Double(entry.glucose!))
|
|
|
- nr_bgs += 1
|
|
|
-
|
|
|
- if startDate - entry.date >= 8.64E7, !end1 {
|
|
|
- end1 = true
|
|
|
- oneDayGlucoseIndex = j
|
|
|
- bg_1 = bg / nr_bgs
|
|
|
- }
|
|
|
-
|
|
|
- if startDate - entry.date >= 6.045E8, !end7 {
|
|
|
- end7 = true
|
|
|
- bg_7 = bg / nr_bgs
|
|
|
- }
|
|
|
-
|
|
|
- if startDate - entry.date >= 8.64E8, !end10 {
|
|
|
- end10 = true
|
|
|
- bg_10 = bg / nr_bgs
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- bg_total = bg / nr_bgs
|
|
|
-
|
|
|
- medianBG = medianCalculation(array: bgArray)
|
|
|
-
|
|
|
- let fullTime = glucose![0].date - glucose![endIndex].date
|
|
|
- let fullTime_1 = glucose![0].date - glucose![oneDayGlucoseIndex].date
|
|
|
-
|
|
|
- let daysBG = fullTime / 8.64E7
|
|
|
-
|
|
|
- var timeInHypo: Decimal = 0
|
|
|
- var timeInHyper: Decimal = 0
|
|
|
- var hypos: Decimal = 0
|
|
|
- var hypers: Decimal = 0
|
|
|
- var i = -1
|
|
|
- var lastIndex = false
|
|
|
-
|
|
|
- while i < endIndex {
|
|
|
- i += 1
|
|
|
-
|
|
|
- let currentTime = glucose![i].date
|
|
|
- var previousTime = currentTime
|
|
|
-
|
|
|
- if i + 1 <= endIndex {
|
|
|
- previousTime = glucose![i + 1].date
|
|
|
- } else {
|
|
|
- lastIndex = true
|
|
|
- }
|
|
|
-
|
|
|
- if glucose![i].glucose! < 72, !lastIndex {
|
|
|
- timeInHypo += currentTime - previousTime
|
|
|
- } else if glucose![i].glucose! > 180, !lastIndex {
|
|
|
- timeInHyper += currentTime - previousTime
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if timeInHypo == 0 {
|
|
|
- hypos = 0
|
|
|
- } else { hypos = (timeInHypo / fullTime) * 100
|
|
|
- }
|
|
|
-
|
|
|
- if timeInHyper == 0 {
|
|
|
- hypers = 0
|
|
|
- } else { hypers = (timeInHyper / fullTime) * 100
|
|
|
- }
|
|
|
-
|
|
|
- let TIR = 100 - (hypos + hypers)
|
|
|
-
|
|
|
- // Do the loop again but with for 1 day. I will change this later, because this looks really dumb:
|
|
|
- var timeInHypo_1: Decimal = 0
|
|
|
- var timeInHyper_1: Decimal = 0
|
|
|
- var hypos_1: Decimal = 0
|
|
|
- var hypers_1: Decimal = 0
|
|
|
- i = -1
|
|
|
- lastIndex = false
|
|
|
-
|
|
|
- while i < oneDayGlucoseIndex {
|
|
|
- i += 1
|
|
|
-
|
|
|
- let currentTime = glucose![i].date
|
|
|
- var previousTime = currentTime
|
|
|
-
|
|
|
- if i + 1 <= oneDayGlucoseIndex {
|
|
|
- previousTime = glucose![i + 1].date
|
|
|
- } else {
|
|
|
- lastIndex = true
|
|
|
- }
|
|
|
-
|
|
|
- if glucose![i].glucose! < 72, !lastIndex {
|
|
|
- timeInHypo_1 += currentTime - previousTime
|
|
|
- } else if glucose![i].glucose! > 180, !lastIndex {
|
|
|
- timeInHyper_1 += currentTime - previousTime
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if timeInHypo_1 == 0 {
|
|
|
- hypos_1 = 0
|
|
|
- } else { hypos_1 = (timeInHypo_1 / fullTime_1) * 100
|
|
|
- }
|
|
|
-
|
|
|
- if timeInHyper_1 == 0 {
|
|
|
- hypers_1 = 0
|
|
|
- } else { hypers_1 = (timeInHyper_1 / fullTime_1) * 100
|
|
|
- }
|
|
|
-
|
|
|
- let TIR_1 = 100 - (hypos_1 + hypers_1)
|
|
|
-
|
|
|
- // Add 10 day average to tenDaysStats.json
|
|
|
- let file_10 = OpenAPS.Monitor.tenDaysStats
|
|
|
- let tensDaysStats = storage.retrieve(file_10, as: [TenDaysStats].self)
|
|
|
- let lastTenDaysStatEntry = tensDaysStats?[0].past10daysAverage ?? 0
|
|
|
-
|
|
|
- let tenStats = TenDaysStats(
|
|
|
- createdAt: Date(), past10daysAverage: roundDecimal(bg_10, 1)
|
|
|
- )
|
|
|
- var uniqEvents: [TenDaysStats] = []
|
|
|
-
|
|
|
- if lastTenDaysStatEntry == 0, daysBG >= 10 {
|
|
|
- storage.save(tenStats, as: file_10)
|
|
|
- } else if daysBG >= 10, Date() > tensDaysStats?[0].createdAt.addingTimeInterval(10.days.timeInterval) ?? Date() {
|
|
|
- storage.transaction { storage in
|
|
|
- storage.append(tenStats, to: file_10, uniqBy: \.createdAt)
|
|
|
- uniqEvents = storage.retrieve(file_10, as: [TenDaysStats].self)?
|
|
|
- .filter { $0.createdAt.addingTimeInterval(365.days.timeInterval) > Date() }
|
|
|
- .sorted { $0.createdAt > $1.createdAt } ?? []
|
|
|
- storage.save(Array(uniqEvents), as: file_10)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return (
|
|
|
- roundDecimal(bg_total, 0),
|
|
|
- medianBG,
|
|
|
- roundDecimal(bg_1, 0),
|
|
|
- roundDecimal(bg_7, 0),
|
|
|
- roundDecimal(bg_10, 0),
|
|
|
- roundDecimal(hypos, 1),
|
|
|
- roundDecimal(hypers, 1),
|
|
|
- roundDecimal(TIR, 1),
|
|
|
- roundDecimal(hypos_1, 1),
|
|
|
- roundDecimal(hypers_1, 1),
|
|
|
- roundDecimal(TIR_1, 1),
|
|
|
- roundDecimal(daysBG, 1)
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
private func processError(_ error: Error) {
|
|
|
warning(.apsManager, "\(error.localizedDescription)")
|
|
|
lastError.send(error)
|