JSONImporter.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import CoreData
  2. import Foundation
  3. /// Migration-specific errors that might happen during migration
  4. enum JSONImporterError: Error {
  5. case missingGlucoseValueInGlucoseEntry
  6. case tempBasalAndDurationMismatch
  7. }
  8. // MARK: - JSONImporter Class
  9. /// Responsible for importing JSON data into Core Data.
  10. ///
  11. /// The importer handles two important states:
  12. /// - JSON files stored in the file system that contain data to import
  13. /// - Existing entries in CoreData that should not be duplicated
  14. ///
  15. /// Imports are performed when a JSON file exists. The importer checks
  16. /// CoreData for existing entries to avoid duplicating records from partial imports.
  17. class JSONImporter {
  18. private let context: NSManagedObjectContext
  19. private let coreDataStack: CoreDataStack
  20. /// Initializes the importer with a Core Data context.
  21. init(context: NSManagedObjectContext, coreDataStack: CoreDataStack) {
  22. self.context = context
  23. self.coreDataStack = coreDataStack
  24. }
  25. /// Reads and parses a JSON file from the file system.
  26. ///
  27. /// - Parameters:
  28. /// - url: The URL of the JSON file to read.
  29. /// - Returns: A decoded object of the specified type.
  30. /// - Throws: An error if the file cannot be read or decoded.
  31. private func readJsonFile<T: Decodable>(url: URL) throws -> T {
  32. let data = try Data(contentsOf: url)
  33. let decoder = JSONCoding.decoder
  34. return try decoder.decode(T.self, from: data)
  35. }
  36. /// Retrieves the set of dates for all glucose values currently stored in CoreData.
  37. ///
  38. /// - Parameters: the start and end dates to fetch glucose values, inclusive
  39. /// - Returns: A set of dates corresponding to existing glucose readings.
  40. /// - Throws: An error if the fetch operation fails.
  41. private func fetchGlucoseDates(start: Date, end: Date) async throws -> Set<Date> {
  42. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  43. ofType: GlucoseStored.self,
  44. onContext: context,
  45. predicate: .predicateForDateBetween(start: start, end: end),
  46. key: "date",
  47. ascending: false
  48. ) as? [GlucoseStored] ?? []
  49. return Set(allReadings.compactMap(\.date))
  50. }
  51. /// Imports glucose history from a JSON file into CoreData.
  52. ///
  53. /// The function reads glucose data from the provided JSON file and stores new entries
  54. /// in CoreData, skipping entries with dates that already exist in the database.
  55. ///
  56. /// - Parameters:
  57. /// - url: The URL of the JSON file containing glucose history.
  58. /// - Throws:
  59. /// - JSONImporterError.missingGlucoseValueInGlucoseEntry if a glucose entry is missing a value.
  60. /// - An error if the file cannot be read or decoded.
  61. /// - An error if the CoreData operation fails.
  62. func importGlucoseHistory(url: URL, now: Date) async throws {
  63. let twentyFourHoursAgo = now - 24.hours.timeInterval
  64. let glucoseHistoryFull: [BloodGlucose] = try readJsonFile(url: url)
  65. let existingDates = try await fetchGlucoseDates(start: twentyFourHoursAgo, end: now)
  66. // only import glucose values from the last 24 hours that don't exist
  67. let glucoseHistory = glucoseHistoryFull
  68. .filter { $0.dateString >= twentyFourHoursAgo && $0.dateString <= now && !existingDates.contains($0.dateString) }
  69. // Create a background context for batch processing
  70. let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  71. backgroundContext.parent = context
  72. try await backgroundContext.perform {
  73. for glucoseEntry in glucoseHistory {
  74. try glucoseEntry.store(in: backgroundContext)
  75. }
  76. try backgroundContext.save()
  77. }
  78. try await context.perform {
  79. try self.context.save()
  80. }
  81. }
  82. private func combineTempBasalAndDuration(pumpHistory: [PumpHistoryEvent]) throws -> [PumpHistoryEvent] {
  83. let tempBasal = pumpHistory.filter({ $0.type == .tempBasal }).sorted { $0.timestamp < $1.timestamp }
  84. let tempBasalDuration = pumpHistory.filter({ $0.type == .tempBasalDuration }).sorted { $0.timestamp < $1.timestamp }
  85. let nonTempBasal = pumpHistory.filter { $0.type != .tempBasal && $0.type != .tempBasalDuration }
  86. guard tempBasal.count == tempBasalDuration.count else {
  87. throw JSONImporterError.tempBasalAndDurationMismatch
  88. }
  89. let combinedTempBasal = try zip(tempBasal, tempBasalDuration).map { rate, duration in
  90. guard rate.timestamp == duration.timestamp else {
  91. throw JSONImporterError.tempBasalAndDurationMismatch
  92. }
  93. return PumpHistoryEvent(
  94. id: duration.id,
  95. type: .tempBasal,
  96. timestamp: duration.timestamp,
  97. duration: duration.durationMin,
  98. rate: rate.rate,
  99. temp: rate.temp
  100. )
  101. }
  102. return (combinedTempBasal + nonTempBasal).sorted { $0.timestamp < $1.timestamp }
  103. }
  104. func importPumpHistory(url: URL, now _: Date) async throws {
  105. let pumpHistoryRaw: [PumpHistoryEvent] = try readJsonFile(url: url)
  106. let pumpHistory = try combineTempBasalAndDuration(pumpHistory: pumpHistoryRaw)
  107. for pumpEntry in pumpHistory {
  108. try pumpEntry.store(in: context)
  109. }
  110. }
  111. }
  112. // MARK: - Extension for Specific Import Functions
  113. extension BloodGlucose {
  114. /// Helper function to convert `BloodGlucose` to `GlucoseStored` while importing JSON glucose entries
  115. func store(in context: NSManagedObjectContext) throws {
  116. guard let glucoseValue = glucose ?? sgv else {
  117. throw JSONImporterError.missingGlucoseValueInGlucoseEntry
  118. }
  119. let glucoseEntry = GlucoseStored(context: context)
  120. glucoseEntry.id = _id.flatMap({ UUID(uuidString: $0) }) ?? UUID()
  121. glucoseEntry.date = dateString
  122. glucoseEntry.glucose = Int16(glucoseValue)
  123. glucoseEntry.direction = direction?.rawValue
  124. glucoseEntry.isManual = type == "Manual"
  125. glucoseEntry.isUploadedToNS = true
  126. glucoseEntry.isUploadedToHealth = true
  127. glucoseEntry.isUploadedToTidepool = true
  128. }
  129. }
  130. extension PumpHistoryEvent {
  131. func store(in context: NSManagedObjectContext) throws {
  132. let pumpEntry = PumpEventStored(context: context)
  133. pumpEntry.id = id
  134. pumpEntry.timestamp = timestamp
  135. pumpEntry.type = type.rawValue
  136. pumpEntry.isUploadedToNS = true
  137. pumpEntry.isUploadedToHealth = true
  138. pumpEntry.isUploadedToTidepool = true
  139. if type == .bolus {
  140. let bolusEntry = BolusStored(context: context)
  141. bolusEntry.amount = amount.flatMap { NSDecimalNumber(decimal: $0) }
  142. bolusEntry.isSMB = isSMB ?? false
  143. bolusEntry.isExternal = isExternal ?? false
  144. pumpEntry.bolus = bolusEntry
  145. } else if type == .tempBasal {
  146. let tempEntry = TempBasalStored(context: context)
  147. tempEntry.rate = rate.flatMap { NSDecimalNumber(decimal: $0) }
  148. tempEntry.duration = duration.flatMap({ Int16($0) }) ?? 0
  149. tempEntry.tempType = temp?.rawValue
  150. pumpEntry.tempBasal = tempEntry
  151. }
  152. }
  153. }
  154. extension JSONImporter {
  155. func importGlucoseHistoryIfNeeded() async {}
  156. }