Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Average ISF/CR/CSF #903

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions FreeAPS/Sources/Localizations/Main/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,21 @@ Enact a temp Basal or a temp target */
/* Threshold Table Columns Title */
"Threshold" = "Threshold";

/* Dynamic settings View */
"Averages" = "Averages";

/* Dynamic settings View */
"Average ISF" = "Average ISF";

/* Dynamic settings View */
"Average CR" = "Average CR";

/* Dynamic settings View */
"Average CSF" = "Average CSF";

/* Dynamic settings View */
"ISF: Insulin Sensitivity, CR: Carb Ratio,\nCSF: Carb Sensitivity = ISF/CR" = "ISF: Insulin Sensitivity, CR: Carb Ratio,\nCSF: Carb Sensitivity = ISF/CR";

/* Header */
"Calculator settings" = "Calculator settings";

Expand Down
30 changes: 30 additions & 0 deletions FreeAPS/Sources/Modules/Dynamic/DynamicStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension Dynamic {
@Published var tddAdjBasal: Bool = false
@Published var threshold_setting: Decimal = 65
@Published var unit: GlucoseUnits = .mmolL
@Published var averages: (isf: Double, cr: Double, days: Double)?

var preferences: Preferences {
settingsManager.preferences
Expand All @@ -26,6 +27,7 @@ extension Dynamic {
adjustmentFactor = settings.preferences.adjustmentFactor
weightPercentage = settings.preferences.weightPercentage
tddAdjBasal = settings.preferences.tddAdjBasal
averages = thirtyDaysAverages()

if unit == .mmolL {
threshold_setting = settings.preferences.threshold_setting.asMmolL
Expand Down Expand Up @@ -65,5 +67,33 @@ extension Dynamic {
storage.save(newSettings, as: OpenAPS.Settings.preferences)
}
}

var reasons: [Reasons] {
CoreDataStorage().fetchReasons(interval: DateFilter().month)
}

private var sameUnit: Bool {
unit == .mmolL
}

private func thirtyDaysAverages() -> (isf: Double, cr: Double, days: Double)? {
let history = reasons.filter({ $0.mmol == sameUnit }).sorted(by: { $0.date ?? Date() > $1.date ?? Date() })
let days = -1 * (history.last?.date ?? .now).timeIntervalSince(history.first?.date ?? .now) / 8.64E4
// Avoid displaying "0 days"
guard !history.isEmpty, days >= 0.06 else { return nil }

let isf = history.compactMap(\.isf)
let cr = history.compactMap(\.cr)
let totalISF = isf.reduce(0, { x, y in
x + (y as Decimal)
})
let totalCR = cr.reduce(0, { x, y in
x + (y as Decimal)
})
let averageCR = Double(totalCR) / Double(cr.count)
let averageISF = Double(totalISF) / Double(isf.count)

return (averageISF, averageCR, days)
}
}
}
50 changes: 50 additions & 0 deletions FreeAPS/Sources/Modules/Dynamic/View/DynamicRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ extension Dynamic {
return formatter
}

private var daysFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 1
return formatter
}

var body: some View {
Form {
Section {
Expand Down Expand Up @@ -161,6 +168,49 @@ extension Dynamic {
Text(state.unit.rawValue)
}
} header: { Text("Safety") }

if let averages = state.averages {
Section {
HStack {
Text("Average ISF")
Spacer()
Text(
glucoseFormatter
.string(from: averages.isf as NSNumber) ?? ""
)
Text(state.unit.rawValue + NSLocalizedString("/U", comment: "")).foregroundColor(.secondary)
}

HStack {
Text("Average CR")
Spacer()
Text(
glucoseFormatter
.string(from: averages.cr as NSNumber) ?? ""
)
Text("g/U").foregroundColor(.secondary)
}

HStack {
Text("Average CSF")
Spacer()
Text(
glucoseFormatter
.string(from: (Double(averages.isf) / Double(averages.cr)) as NSNumber) ?? ""
)
Text(state.unit.rawValue + "/g").foregroundColor(.secondary)
}
} header: {
HStack(spacing: 0) {
Text("Averages")
Text(
" (" + (daysFormatter.string(from: averages.days as NSNumber) ?? "") + " " +
NSLocalizedString("days", comment: " days of data") + ")"
)
}
}
footer: { Text("ISF: Insulin Sensitivity, CR: Carb Ratio,\nCSF: Carb Sensitivity = ISF/CR") }
}
}
.blur(radius: isPresented ? 5 : 0)
.description(isPresented: isPresented, alignment: .center) {
Expand Down