BolusStatsSetup.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import CoreData
  2. import Foundation
  3. /// Represents statistical data about bolus insulin delivery for a specific day
  4. struct BolusStats: Identifiable {
  5. let id = UUID()
  6. /// The date representing this time period
  7. let date: Date
  8. /// Total amount of manual boluses (excluding SMB and external)
  9. let manualBolus: Double
  10. /// Total amount of Super Micro Boluses (SMB)
  11. let smb: Double
  12. /// Total amount of external boluses (e.g., from pump directly)
  13. let external: Double
  14. }
  15. extension Stat.StateModel {
  16. func setupBolusStats() {
  17. Task {
  18. let stats = await fetchBolusStats()
  19. await MainActor.run {
  20. self.bolusStats = stats
  21. }
  22. }
  23. }
  24. /// Fetches and processes bolus statistics for a specific date range
  25. /// - Returns: Array of BolusStats containing daily bolus statistics
  26. func fetchBolusStats() async -> [BolusStats] {
  27. let calendar = Calendar.current
  28. // Fetch bolus records from Core Data
  29. let results = await CoreDataStack.shared.fetchEntitiesAsync(
  30. ofType: BolusStored.self,
  31. onContext: bolusTaskContext,
  32. predicate: NSPredicate.pumpHistoryForStats,
  33. key: "pumpEvent.timestamp",
  34. ascending: true,
  35. batchSize: 100
  36. )
  37. return await bolusTaskContext.perform {
  38. guard let fetchedResults = results as? [BolusStored] else { return [] }
  39. // Group boluses by day
  40. let groupedByDay = Dictionary(grouping: fetchedResults) { bolus -> Date in
  41. guard let timestamp = bolus.pumpEvent?.timestamp else { return Date() }
  42. return calendar.startOfDay(for: timestamp)
  43. }
  44. // Calculate daily totals
  45. return groupedByDay.map { date, boluses -> BolusStats in
  46. // Calculate total manual boluses (excluding SMB and external)
  47. let manualBolus = boluses
  48. .filter { !($0.isExternal || $0.isSMB) }
  49. .reduce(0.0) { $0 + (($1.amount as? Decimal) ?? 0).doubleValue }
  50. // Calculate total SMB
  51. let smb = boluses
  52. .filter { $0.isSMB }
  53. .reduce(0.0) { $0 + (($1.amount as? Decimal) ?? 0).doubleValue }
  54. // Calculate total external boluses
  55. let external = boluses
  56. .filter { $0.isExternal }
  57. .reduce(0.0) { $0 + (($1.amount as? Decimal) ?? 0).doubleValue }
  58. return BolusStats(
  59. date: date,
  60. manualBolus: manualBolus,
  61. smb: smb,
  62. external: external
  63. )
  64. }
  65. }
  66. }
  67. func calculateAverageBolus(from startDate: Date, to endDate: Date) -> (manual: Double, smb: Double, external: Double) {
  68. let visibleStats = bolusStats.filter { stat in
  69. stat.date >= startDate && stat.date <= endDate
  70. }
  71. guard !visibleStats.isEmpty else { return (0, 0, 0) }
  72. let count = Double(visibleStats.count)
  73. let manualSum = visibleStats.reduce(0.0) { $0 + $1.manualBolus }
  74. let smbSum = visibleStats.reduce(0.0) { $0 + $1.smb }
  75. let externalSum = visibleStats.reduce(0.0) { $0 + $1.external }
  76. return (
  77. manualSum / count,
  78. smbSum / count,
  79. externalSum / count
  80. )
  81. }
  82. }
  83. /// Extension to convert Decimal to Double
  84. private extension Decimal {
  85. var doubleValue: Double {
  86. NSDecimalNumber(decimal: self).doubleValue
  87. }
  88. }