PumpHistoryStorage.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import Foundation
  2. import LoopKit
  3. import SwiftDate
  4. import Swinject
  5. protocol PumpHistoryObserver {
  6. func pumpHistoryDidUpdate(_ events: [PumpHistoryEvent])
  7. }
  8. protocol PumpHistoryStorage {
  9. func storePumpEvents(_ events: [NewPumpEvent])
  10. func storeEvents(_ events: [PumpHistoryEvent])
  11. func storeJournalCarbs(_ carbs: Int)
  12. func recent() -> [PumpHistoryEvent]
  13. func nightscoutTretmentsNotUploaded() -> [NigtscoutTreatment]
  14. func saveCancelTempEvents()
  15. func deleteInsulin(at date: Date)
  16. }
  17. final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
  18. private let processQueue = DispatchQueue(label: "BasePumpHistoryStorage.processQueue")
  19. @Injected() private var storage: FileStorage!
  20. @Injected() private var broadcaster: Broadcaster!
  21. init(resolver: Resolver) {
  22. injectServices(resolver)
  23. }
  24. typealias PumpEvent = PumpEventStored.EventType
  25. typealias TempType = PumpEventStored.TempType
  26. private let context = CoreDataStack.shared.backgroundContext
  27. func storePumpEvents(_ events: [NewPumpEvent]) {
  28. processQueue.async {
  29. let eventsToStore = events.flatMap { event -> [PumpHistoryEvent] in
  30. let id = event.raw.md5String
  31. switch event.type {
  32. case .bolus:
  33. guard let dose = event.dose else { return [] }
  34. let amount = Decimal(string: dose.unitsInDeliverableIncrements.description)
  35. let minutes = Int((dose.endDate - dose.startDate).timeInterval / 60)
  36. self.context.perform {
  37. // create pump event
  38. let newPumpEvent = PumpEventStored(context: self.context)
  39. newPumpEvent.id = id
  40. newPumpEvent.timestamp = event.date
  41. newPumpEvent.type = PumpEvent.bolus.rawValue
  42. // create bolus entry and specify relationship to pump event
  43. let newBolusEntry = BolusStored(context: self.context)
  44. newBolusEntry.pumpEvent = newPumpEvent
  45. newBolusEntry.amount = amount as? NSDecimalNumber
  46. newBolusEntry.isExternal = dose.manuallyEntered
  47. newBolusEntry.isSMB = dose.automatic ?? true
  48. // TODO: - do we need duration here?
  49. if self.context.hasChanges {
  50. do {
  51. try self.context.save()
  52. debugPrint(
  53. "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved smbs to core data"
  54. )
  55. } catch {
  56. debugPrint(
  57. "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save smbs to core data"
  58. )
  59. }
  60. }
  61. }
  62. return [PumpHistoryEvent(
  63. id: id,
  64. type: .bolus,
  65. timestamp: event.date,
  66. amount: amount,
  67. duration: minutes,
  68. durationMin: nil,
  69. rate: nil,
  70. temp: nil,
  71. carbInput: nil,
  72. isSMB: dose.automatic,
  73. isExternal: dose.manuallyEntered
  74. )]
  75. case .tempBasal:
  76. guard let dose = event.dose else { return [] }
  77. let rate = Decimal(dose.unitsPerHour)
  78. let minutes = (dose.endDate - dose.startDate).timeInterval / 60
  79. let delivered = dose.deliveredUnits
  80. let date = event.date
  81. let isCancel = delivered != nil //! event.isMutable && delivered != nil
  82. guard !isCancel else { return [] }
  83. self.context.perform {
  84. // create pump event
  85. let newPumpEvent = PumpEventStored(context: self.context)
  86. newPumpEvent.id = id
  87. newPumpEvent.timestamp = date
  88. newPumpEvent.type = PumpEvent.tempBasal.rawValue
  89. // create temp basal and specify relationship
  90. let newTempBasal = TempBasalStored(context: self.context)
  91. newTempBasal.pumpEvent = newPumpEvent
  92. newTempBasal.duration = Int16(round(minutes))
  93. newTempBasal.rate = rate as NSDecimalNumber
  94. newTempBasal.tempType = TempType.absolute.rawValue
  95. if self.context.hasChanges {
  96. do {
  97. try self.context.save()
  98. debugPrint(
  99. "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved temp basal to core data"
  100. )
  101. } catch {
  102. debugPrint(
  103. "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save temp basal to core data"
  104. )
  105. }
  106. }
  107. }
  108. return [
  109. PumpHistoryEvent(
  110. id: id,
  111. type: .tempBasalDuration,
  112. timestamp: date,
  113. amount: nil,
  114. duration: nil,
  115. durationMin: Int(round(minutes)),
  116. rate: nil,
  117. temp: nil,
  118. carbInput: nil
  119. ),
  120. PumpHistoryEvent(
  121. id: "_" + id,
  122. type: .tempBasal,
  123. timestamp: date,
  124. amount: nil,
  125. duration: nil,
  126. durationMin: nil,
  127. rate: rate,
  128. temp: .absolute,
  129. carbInput: nil
  130. )
  131. ]
  132. case .suspend:
  133. self.context.perform {
  134. // create pump event
  135. let newPumpEvent = PumpEventStored(context: self.context)
  136. newPumpEvent.id = id
  137. newPumpEvent.timestamp = event.date
  138. newPumpEvent.type = PumpEvent.pumpSuspend.rawValue
  139. if self.context.hasChanges {
  140. do {
  141. try self.context.save()
  142. debugPrint(
  143. "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved suspension to core data"
  144. )
  145. } catch {
  146. debugPrint(
  147. "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save suspension to core data"
  148. )
  149. }
  150. }
  151. }
  152. return [
  153. PumpHistoryEvent(
  154. id: id,
  155. type: .pumpSuspend,
  156. timestamp: event.date,
  157. amount: nil,
  158. duration: nil,
  159. durationMin: nil,
  160. rate: nil,
  161. temp: nil,
  162. carbInput: nil
  163. )
  164. ]
  165. case .resume:
  166. return [
  167. PumpHistoryEvent(
  168. id: id,
  169. type: .pumpResume,
  170. timestamp: event.date,
  171. amount: nil,
  172. duration: nil,
  173. durationMin: nil,
  174. rate: nil,
  175. temp: nil,
  176. carbInput: nil
  177. )
  178. ]
  179. case .rewind:
  180. return [
  181. PumpHistoryEvent(
  182. id: id,
  183. type: .rewind,
  184. timestamp: event.date,
  185. amount: nil,
  186. duration: nil,
  187. durationMin: nil,
  188. rate: nil,
  189. temp: nil,
  190. carbInput: nil
  191. )
  192. ]
  193. case .prime:
  194. return [
  195. PumpHistoryEvent(
  196. id: id,
  197. type: .prime,
  198. timestamp: event.date,
  199. amount: nil,
  200. duration: nil,
  201. durationMin: nil,
  202. rate: nil,
  203. temp: nil,
  204. carbInput: nil
  205. )
  206. ]
  207. case .alarm:
  208. return [
  209. PumpHistoryEvent(
  210. id: id,
  211. type: .pumpAlarm,
  212. timestamp: event.date,
  213. note: event.title
  214. )
  215. ]
  216. default:
  217. return []
  218. }
  219. }
  220. self.storeEvents(eventsToStore)
  221. }
  222. }
  223. func storeJournalCarbs(_ carbs: Int) {
  224. processQueue.async {
  225. let eventsToStore = [
  226. PumpHistoryEvent(
  227. id: UUID().uuidString,
  228. type: .journalCarbs,
  229. timestamp: Date(),
  230. amount: nil,
  231. duration: nil,
  232. durationMin: nil,
  233. rate: nil,
  234. temp: nil,
  235. carbInput: carbs
  236. )
  237. ]
  238. self.storeEvents(eventsToStore)
  239. }
  240. }
  241. func storeEvents(_ events: [PumpHistoryEvent]) {
  242. processQueue.async {
  243. let file = OpenAPS.Monitor.pumpHistory
  244. var uniqEvents: [PumpHistoryEvent] = []
  245. self.storage.transaction { storage in
  246. storage.append(events, to: file, uniqBy: \.id)
  247. uniqEvents = storage.retrieve(file, as: [PumpHistoryEvent].self)?
  248. .filter { $0.timestamp.addingTimeInterval(1.days.timeInterval) > Date() }
  249. .sorted { $0.timestamp > $1.timestamp } ?? []
  250. storage.save(Array(uniqEvents), as: file)
  251. }
  252. self.broadcaster.notify(PumpHistoryObserver.self, on: self.processQueue) {
  253. $0.pumpHistoryDidUpdate(uniqEvents)
  254. }
  255. }
  256. }
  257. func recent() -> [PumpHistoryEvent] {
  258. storage.retrieve(OpenAPS.Monitor.pumpHistory, as: [PumpHistoryEvent].self)?.reversed() ?? []
  259. }
  260. func deleteInsulin(at date: Date) {
  261. processQueue.sync {
  262. var allValues = storage.retrieve(OpenAPS.Monitor.pumpHistory, as: [PumpHistoryEvent].self) ?? []
  263. guard let entryIndex = allValues.firstIndex(where: { $0.timestamp == date }) else {
  264. return
  265. }
  266. allValues.remove(at: entryIndex)
  267. storage.save(allValues, as: OpenAPS.Monitor.pumpHistory)
  268. broadcaster.notify(PumpHistoryObserver.self, on: processQueue) {
  269. $0.pumpHistoryDidUpdate(allValues)
  270. }
  271. }
  272. }
  273. func determineBolusEventType(for event: PumpHistoryEvent) -> EventType {
  274. if event.isSMB ?? false {
  275. return .smb
  276. }
  277. if event.isExternal ?? false {
  278. return .isExternal
  279. }
  280. return event.type
  281. }
  282. func nightscoutTretmentsNotUploaded() -> [NigtscoutTreatment] {
  283. let events = recent()
  284. guard !events.isEmpty else { return [] }
  285. let temps: [NigtscoutTreatment] = events.reduce([]) { result, event in
  286. var result = result
  287. switch event.type {
  288. case .tempBasal:
  289. result.append(NigtscoutTreatment(
  290. duration: nil,
  291. rawDuration: nil,
  292. rawRate: event,
  293. absolute: event.rate,
  294. rate: event.rate,
  295. eventType: .nsTempBasal,
  296. createdAt: event.timestamp,
  297. enteredBy: NigtscoutTreatment.local,
  298. bolus: nil,
  299. insulin: nil,
  300. notes: nil,
  301. carbs: nil,
  302. fat: nil,
  303. protein: nil,
  304. targetTop: nil,
  305. targetBottom: nil
  306. ))
  307. case .tempBasalDuration:
  308. if var last = result.popLast(), last.eventType == .nsTempBasal, last.createdAt == event.timestamp {
  309. last.duration = event.durationMin
  310. last.rawDuration = event
  311. result.append(last)
  312. }
  313. default: break
  314. }
  315. return result
  316. }
  317. let bolusesAndCarbs = events.compactMap { event -> NigtscoutTreatment? in
  318. switch event.type {
  319. case .bolus:
  320. let eventType = determineBolusEventType(for: event)
  321. return NigtscoutTreatment(
  322. duration: event.duration,
  323. rawDuration: nil,
  324. rawRate: nil,
  325. absolute: nil,
  326. rate: nil,
  327. eventType: eventType,
  328. createdAt: event.timestamp,
  329. enteredBy: NigtscoutTreatment.local,
  330. bolus: event,
  331. insulin: event.amount,
  332. notes: nil,
  333. carbs: nil,
  334. fat: nil,
  335. protein: nil,
  336. targetTop: nil,
  337. targetBottom: nil
  338. )
  339. case .journalCarbs:
  340. return NigtscoutTreatment(
  341. duration: nil,
  342. rawDuration: nil,
  343. rawRate: nil,
  344. absolute: nil,
  345. rate: nil,
  346. eventType: .nsCarbCorrection,
  347. createdAt: event.timestamp,
  348. enteredBy: NigtscoutTreatment.local,
  349. bolus: nil,
  350. insulin: nil,
  351. notes: nil,
  352. carbs: Decimal(event.carbInput ?? 0),
  353. fat: nil,
  354. protein: nil,
  355. targetTop: nil,
  356. targetBottom: nil
  357. )
  358. default: return nil
  359. }
  360. }
  361. let misc = events.compactMap { event -> NigtscoutTreatment? in
  362. switch event.type {
  363. case .prime:
  364. return NigtscoutTreatment(
  365. duration: event.duration,
  366. rawDuration: nil,
  367. rawRate: nil,
  368. absolute: nil,
  369. rate: nil,
  370. eventType: .nsSiteChange,
  371. createdAt: event.timestamp,
  372. enteredBy: NigtscoutTreatment.local,
  373. bolus: event,
  374. insulin: nil,
  375. notes: nil,
  376. carbs: nil,
  377. fat: nil,
  378. protein: nil,
  379. targetTop: nil,
  380. targetBottom: nil
  381. )
  382. case .rewind:
  383. return NigtscoutTreatment(
  384. duration: nil,
  385. rawDuration: nil,
  386. rawRate: nil,
  387. absolute: nil,
  388. rate: nil,
  389. eventType: .nsInsulinChange,
  390. createdAt: event.timestamp,
  391. enteredBy: NigtscoutTreatment.local,
  392. bolus: nil,
  393. insulin: nil,
  394. notes: nil,
  395. carbs: nil,
  396. fat: nil,
  397. protein: nil,
  398. targetTop: nil,
  399. targetBottom: nil
  400. )
  401. case .pumpAlarm:
  402. return NigtscoutTreatment(
  403. duration: 30, // minutes
  404. rawDuration: nil,
  405. rawRate: nil,
  406. absolute: nil,
  407. rate: nil,
  408. eventType: .nsAnnouncement,
  409. createdAt: event.timestamp,
  410. enteredBy: NigtscoutTreatment.local,
  411. bolus: nil,
  412. insulin: nil,
  413. notes: "Alarm \(String(describing: event.note)) \(event.type)",
  414. carbs: nil,
  415. fat: nil,
  416. protein: nil,
  417. targetTop: nil,
  418. targetBottom: nil
  419. )
  420. default: return nil
  421. }
  422. }
  423. let uploaded = storage.retrieve(OpenAPS.Nightscout.uploadedPumphistory, as: [NigtscoutTreatment].self) ?? []
  424. let treatments = Array(Set([bolusesAndCarbs, temps, misc].flatMap { $0 }).subtracting(Set(uploaded)))
  425. return treatments.sorted { $0.createdAt! > $1.createdAt! }
  426. }
  427. func saveCancelTempEvents() {
  428. let basalID = UUID().uuidString
  429. let date = Date()
  430. let events = [
  431. PumpHistoryEvent(
  432. id: basalID,
  433. type: .tempBasalDuration,
  434. timestamp: date,
  435. amount: nil,
  436. duration: nil,
  437. durationMin: 0,
  438. rate: nil,
  439. temp: nil,
  440. carbInput: nil
  441. ),
  442. PumpHistoryEvent(
  443. id: "_" + basalID,
  444. type: .tempBasal,
  445. timestamp: date,
  446. amount: nil,
  447. duration: nil,
  448. durationMin: nil,
  449. rate: 0,
  450. temp: .absolute,
  451. carbInput: nil
  452. )
  453. ]
  454. storeEvents(events)
  455. }
  456. }