JSONImporter.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /// Retrieves the set of timestamps for all pump evets currently stored in CoreData.
  52. ///
  53. /// - Parameters: the start and end dates to fetch pump events, inclusive
  54. /// - Returns: A set of dates corresponding to existing pump events.
  55. /// - Throws: An error if the fetch operation fails.
  56. private func fetchPumpTimestamps(start: Date, end: Date) async throws -> Set<Date> {
  57. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  58. ofType: PumpEventStored.self,
  59. onContext: context,
  60. predicate: .predicateForTimestampBetween(start: start, end: end),
  61. key: "timestamp",
  62. ascending: false
  63. ) as? [PumpEventStored] ?? []
  64. return Set(allReadings.compactMap(\.timestamp))
  65. }
  66. /// Imports glucose history from a JSON file into CoreData.
  67. ///
  68. /// The function reads glucose data from the provided JSON file and stores new entries
  69. /// in CoreData, skipping entries with dates that already exist in the database.
  70. ///
  71. /// - Parameters:
  72. /// - url: The URL of the JSON file containing glucose history.
  73. /// - Throws:
  74. /// - JSONImporterError.missingGlucoseValueInGlucoseEntry if a glucose entry is missing a value.
  75. /// - An error if the file cannot be read or decoded.
  76. /// - An error if the CoreData operation fails.
  77. func importGlucoseHistory(url: URL, now: Date) async throws {
  78. let twentyFourHoursAgo = now - 24.hours.timeInterval
  79. let glucoseHistoryFull: [BloodGlucose] = try readJsonFile(url: url)
  80. let existingDates = try await fetchGlucoseDates(start: twentyFourHoursAgo, end: now)
  81. // only import glucose values from the last 24 hours that don't exist
  82. let glucoseHistory = glucoseHistoryFull
  83. .filter { $0.dateString >= twentyFourHoursAgo && $0.dateString <= now && !existingDates.contains($0.dateString) }
  84. // Create a background context for batch processing
  85. let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  86. backgroundContext.parent = context
  87. try await backgroundContext.perform {
  88. for glucoseEntry in glucoseHistory {
  89. try glucoseEntry.store(in: backgroundContext)
  90. }
  91. try backgroundContext.save()
  92. }
  93. try await context.perform {
  94. try self.context.save()
  95. }
  96. }
  97. private func combineTempBasalAndDuration(pumpHistory: [PumpHistoryEvent]) throws -> [PumpHistoryEvent] {
  98. let tempBasal = pumpHistory.filter({ $0.type == .tempBasal }).sorted { $0.timestamp < $1.timestamp }
  99. let tempBasalDuration = pumpHistory.filter({ $0.type == .tempBasalDuration }).sorted { $0.timestamp < $1.timestamp }
  100. let nonTempBasal = pumpHistory.filter { $0.type != .tempBasal && $0.type != .tempBasalDuration }
  101. guard tempBasal.count == tempBasalDuration.count else {
  102. throw JSONImporterError.tempBasalAndDurationMismatch
  103. }
  104. let combinedTempBasal = try zip(tempBasal, tempBasalDuration).map { rate, duration in
  105. guard rate.timestamp == duration.timestamp else {
  106. throw JSONImporterError.tempBasalAndDurationMismatch
  107. }
  108. return PumpHistoryEvent(
  109. id: duration.id,
  110. type: .tempBasal,
  111. timestamp: duration.timestamp,
  112. duration: duration.durationMin,
  113. rate: rate.rate,
  114. temp: rate.temp
  115. )
  116. }
  117. return (combinedTempBasal + nonTempBasal).sorted { $0.timestamp < $1.timestamp }
  118. }
  119. func importPumpHistory(url: URL, now: Date) async throws {
  120. let twentyFourHoursAgo = now - 24.hours.timeInterval
  121. let pumpHistoryRaw: [PumpHistoryEvent] = try readJsonFile(url: url)
  122. let existingTimestamps = try await fetchPumpTimestamps(start: twentyFourHoursAgo, end: now)
  123. let pumpHistoryFiltered = pumpHistoryRaw
  124. .filter { $0.timestamp >= twentyFourHoursAgo && $0.timestamp <= now && !existingTimestamps.contains($0.timestamp) }
  125. let pumpHistory = try combineTempBasalAndDuration(pumpHistory: pumpHistoryFiltered)
  126. for pumpEntry in pumpHistory {
  127. try pumpEntry.store(in: context)
  128. }
  129. }
  130. }
  131. // MARK: - Extension for Specific Import Functions
  132. extension BloodGlucose {
  133. /// Helper function to convert `BloodGlucose` to `GlucoseStored` while importing JSON glucose entries
  134. func store(in context: NSManagedObjectContext) throws {
  135. guard let glucoseValue = glucose ?? sgv else {
  136. throw JSONImporterError.missingGlucoseValueInGlucoseEntry
  137. }
  138. let glucoseEntry = GlucoseStored(context: context)
  139. glucoseEntry.id = _id.flatMap({ UUID(uuidString: $0) }) ?? UUID()
  140. glucoseEntry.date = dateString
  141. glucoseEntry.glucose = Int16(glucoseValue)
  142. glucoseEntry.direction = direction?.rawValue
  143. glucoseEntry.isManual = type == "Manual"
  144. glucoseEntry.isUploadedToNS = true
  145. glucoseEntry.isUploadedToHealth = true
  146. glucoseEntry.isUploadedToTidepool = true
  147. }
  148. }
  149. extension PumpHistoryEvent {
  150. func store(in context: NSManagedObjectContext) throws {
  151. let pumpEntry = PumpEventStored(context: context)
  152. pumpEntry.id = id
  153. pumpEntry.timestamp = timestamp
  154. pumpEntry.type = type.rawValue
  155. pumpEntry.isUploadedToNS = true
  156. pumpEntry.isUploadedToHealth = true
  157. pumpEntry.isUploadedToTidepool = true
  158. if type == .bolus {
  159. let bolusEntry = BolusStored(context: context)
  160. bolusEntry.amount = amount.flatMap { NSDecimalNumber(decimal: $0) }
  161. bolusEntry.isSMB = isSMB ?? false
  162. bolusEntry.isExternal = isExternal ?? false
  163. pumpEntry.bolus = bolusEntry
  164. } else if type == .tempBasal {
  165. let tempEntry = TempBasalStored(context: context)
  166. tempEntry.rate = rate.flatMap { NSDecimalNumber(decimal: $0) }
  167. tempEntry.duration = duration.flatMap({ Int16($0) }) ?? 0
  168. tempEntry.tempType = temp?.rawValue
  169. pumpEntry.tempBasal = tempEntry
  170. }
  171. }
  172. }
  173. extension JSONImporter {
  174. func importGlucoseHistoryIfNeeded() async {}
  175. }