-
Notifications
You must be signed in to change notification settings - Fork 3
/
Reports.swift
470 lines (365 loc) · 15.7 KB
/
Reports.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//
// Reports.swift
// SMARTMarkers
//
// Created by Raheel Sayeed on 11/20/19.
// Copyright © 2019 Boston Children's Hospital. All rights reserved.
//
import Foundation
import SMART
public struct TaskReport {
/// Weak reference to `taskController`
weak var taskController: TaskController?
/// Practitioner's `Request`
weak var request: Request?
/// Instrument for which the reports are fetched or generated
weak var instrument: Instrument?
/// Patient for which the reports are fetched or generated
weak var patient: Patient?
/// generated bundle
var bundle: SMART.Bundle?
/// attempted metric
var metric: TaskAttempt
/// Are the reports in bundle submitted to server?
var isSubmitted: Bool {
bundle?.entry?.filter({ $0.resource?.id == nil }).count == 0
}
var reports: [Report]? {
bundle?.entry?.compactMap ({ $0.resource as? Report })
}
}
/**
Reports Collection Class
Instances of this class are mainly responsible for fetching historical `Report` conformant FHIR resources and submit newly created resources.
*/
open class Reports {
static let calendar = Calendar.current
/// Weak reference to `taskController`
weak var taskController: TaskController?
/// Practitioner's `Request`
weak var request: Request?
/// Instrument for which the reports are fetched or generated
weak var instrument: Instrument?
/// Patient for which the reports are fetched or generated
weak var patient: Patient?
/// Collection of `Report`s fetched from the FHIR `Server`
private lazy var _reports: [Report] = {
[Report]()
}()
/// Public reference to _reports
open var reports: [Report] {
_reports
}
/// Collection of `InstrumentResult`; Yet to be submitted
private lazy var _submissionQueue: [InstrumentResult] = {
[InstrumentResult]()
}()
/// Public reference to queue
// open var submissionQueue: [InstrumentResult] {
// return _submissionQueue
// }
open var recent: InstrumentResult? {
_submissionQueue.last
}
public init(for task: TaskController) {
self.taskController = task
}
/// Boolean to determine if there are reports generated Today
public func hasReportsForToday() -> Bool {
for report in reports {
if let date = report.rp_date, Reports.calendar.isDateInToday(date) {
return true
}
}
return false
}
/// Boolean to determine if there are unsubmitted FHIR resources
public var hasReportsToSubmit: Bool {
!_submissionQueue.isEmpty
}
public func reportsToSubmit() -> [InstrumentResult]? {
_submissionQueue
}
/**
Designated Initializer.
- parameter instrument: The `Instrument` the receiver should fetch or submit the reports for
- parameter for: The `Patient` for which the reports are fetched or submitted
- parameter request: The `Request` for which the reports are fetched or generated
*/
public init(_ instrument: Instrument, for patient: Patient?, request: Request?) {
self.instrument = instrument
self.patient = patient
self.request = request
}
/// `Report conformant FHIR Resource to the receiver
private func add(_ resource: Report) {
_reports.append(resource)
}
/// `Report` conformant FHIR Resources added to the receiver
private func add(_ resources: [Report]) {
_reports.append(contentsOf: resources)
}
/// Returns `InstrumentResult` generated from a specific user task session (taskId) from the queue
open func instrumentResult(for taskId: String) -> InstrumentResult? {
return _submissionQueue.filter({ (instrumentResult) -> Bool in
return instrumentResult.taskId == taskId
}).first
}
/**
Fetch historical reports for a given patient and instrument
*/
open func fetch(for patient: Patient?, server: Server, searchParams: [String:String]?, callback: @escaping ((_ _results: [Report]?, _ error: Error?) -> Void)) {
guard let resultParams = (instrument ?? taskController?.instrument)?.sm_reportSearchOptions else {
callback(nil, SMError.promeasureOrderedInstrumentMissing)
return
}
let group = DispatchGroup()
var errors = [Error]()
for param in resultParams {
var searchParam = param.relation
if let pt = patient {
searchParam["subject"] = "Patient/\(pt.id!.string)"
}
let search = param.resourceType.search(searchParam as Any)
search.pageCount = 100
group.enter()
search.perform(server) { (bundle, error) in
if let bundle = bundle, let entries = bundle.entry {
if entries.count > 0 {
let results = entries.map { $0.resource as! Report }
self.add(results)
}
}
if let error = error {
errors.append(error)
}
group.leave()
}
}
group.notify(queue: .global(qos: .default)) {
callback(self.reports, (errors.count > 0) ? SMError.reportsFetchFinishedWithErrors(description: errors.description) : nil)
}
}
/// Add InstrumentResult to the results queue
open func enqueueBundle(_ instrumentResult: InstrumentResult) {
_submissionQueue.append(instrumentResult)
}
/// Enqueue newly created `SMART.Bundle` into the receiver's queue; prepared for submission to `FHIR Server`
@discardableResult
open func enqueueSubmission(_ bundle: SMART.Bundle, taskId: String, metric: TaskAttempt, requestId: String? = nil) -> InstrumentResult {
let gr = InstrumentResult(taskId: taskId, bundle: bundle, metric: metric, requestId: requestId)
_submissionQueue.append(gr)
return gr
}
/**
Attempts to submit the `reportBundles` in the receiver's queue (`submissionQueue`)
Note: `instrumentResult.canSubmit` flag must be `true` for submission; else will be discarded.
- parameter server: `FHIR Server` to submit too
- parameter patient: `SMART.Patient` resource
- parameter request: `Request` resource (optional)
- parameter callback: Callback indicating success or fail with errors when attempted submission
*/
open func submit(to server: Server, patient: Patient, request: Request?, callback: @escaping ((_ success: Bool, _ error: [Error]?) -> Void)) {
guard !_submissionQueue.isEmpty else {
callback(true, nil)
return
}
let group = DispatchGroup()
var errors = [Error]()
for submission in _submissionQueue {
// TODO: Send the taskMetric?
if submission.bundle == nil {
continue
}
try? Reports.Tag(&submission.bundle!, with: patient, request: request)
group.enter()
submit(bundle: submission.bundle!, server: server) { (success, error) in
if let error = error {
errors.append(error)
}
group.leave()
}
}
group.notify(queue: .global(qos: .default)) {
// Remove all submitted bundles from the which were submitted or discarded
self._submissionQueue.removeAll(where: { (submission) -> Bool in
(submission.status == .submitted || submission.status == .discarded)
})
callback(errors.isEmpty, errors)
}
}
open func submit(to server: Server, patient: Patient, callback: @escaping ((_ success: Bool, _ error: [Error]?) -> Void)) {
submit(to: server, patient: patient, request: taskController?.request ?? request, callback: callback)
}
/**
Static method for "tagging" the generated `Bundle` with the patient and request if available.
Resources contained in the `Bundle` are checked for known resource types and tagged with Patient and Requests's `Reference`
Supported FHIR Resources: `QuestionnaireResponse`, `Observation`, `Binary`, `Media`, `Immunization`, `AllergyIntolerance`, `Condition`, `MedicationRequest`, `Procedure`, `DocumentRefereence`
- parameter bundle: Generated output in `SMART.Bundle` to tag
- parameter with: The `Patient` to associate the bundle resources with
- parameter request: The `Request` to associate the bundle resources with
*/
public static func Tag(_ bundle: inout SMART.Bundle, with patient: Patient?, request: Request?) throws {
do {
let patientReference = try patient?.asRelativeReference()
let requestReference = try (request as? ServiceRequest)?.asRelativeReference()
for entry in bundle.entry ?? [] {
//QuestionnaireResponse
if let answers = entry.resource as? QuestionnaireResponse {
answers.subject = patientReference
if let r = requestReference {
var basedOns = answers.basedOn ?? [Reference]()
basedOns.append(r)
answers.basedOn = basedOns
}
}
//Observation
if let observation = entry.resource as? Observation {
observation.subject = patientReference
if let r = requestReference {
var basedOns = observation.basedOn ?? [Reference]()
basedOns.append(r)
observation.basedOn = basedOns
}
}
//Binary
if let binary = entry.resource as? Binary {
binary.securityContext = patientReference
}
//Media
if let media = entry.resource as? Media {
media.subject = patientReference
if let reqReference = requestReference {
media.basedOn = [reqReference]
}
}
// Immunization
if let immunization = entry.resource as? Immunization {
immunization.patient = patientReference
}
// AllergyIntolerance
if let allergyIntolerance = entry.resource as? AllergyIntolerance {
allergyIntolerance.patient = patientReference
}
// Condition
if let condition = entry.resource as? Condition {
condition.subject = patientReference
}
// MedicationRequest
if let medicationRequest = entry.resource as? MedicationRequest {
medicationRequest.subject = patientReference
}
// MedicationStatement
if let medicationStatement = entry.resource as? MedicationStatement {
medicationStatement.subject = patientReference
}
// Procedure
if let procedure = entry.resource as? Procedure {
procedure.subject = patientReference
}
// DocumentReference
if let documentReference = entry.resource as? DocumentReference {
documentReference.subject = patientReference
}
}
}
catch {
throw error
}
}
public static func Tag(_ resources: inout [Resource], with patient: Patient?, request: Request?) throws {
let patientReference = try patient?.asRelativeReference()
let requestReference = try request?.asRelativeReference()
for resource in resources {
//QuestionnaireResponse
if let answers = resource as? QuestionnaireResponse {
answers.subject = patientReference
if let r = requestReference {
var basedOns = answers.basedOn ?? [Reference]()
basedOns.append(r)
answers.basedOn = basedOns
}
}
//Observation
if let observation = resource as? Observation {
observation.subject = patientReference
if let r = requestReference {
var basedOns = observation.basedOn ?? [Reference]()
basedOns.append(r)
observation.basedOn = basedOns
}
}
//Binary
if let binary = resource as? Binary {
binary.securityContext = patientReference
}
//Media
if let media = resource as? Media {
media.subject = patientReference
if let reqReference = requestReference {
media.basedOn = [reqReference]
}
}
// Immunization
if let immunization = resource as? Immunization {
immunization.patient = patientReference
}
// AllergyIntolerance
if let allergyIntolerance = resource as? AllergyIntolerance {
allergyIntolerance.patient = patientReference
}
// Condition
if let condition = resource as? Condition {
condition.subject = patientReference
}
// MedicationRequest
if let medicationRequest = resource as? MedicationRequest {
medicationRequest.subject = patientReference
}
// MedicationStatement
if let medicationStatement = resource as? MedicationStatement {
medicationStatement.subject = patientReference
}
// Procedure
if let procedure = resource as? Procedure {
procedure.subject = patientReference
}
// DocumentReference
if let documentReference = resource as? DocumentReference {
documentReference.subject = patientReference
}
}
}
/**
Submission method
Submits FHIR resources in the receiver's InstrumentResult (`SMART.Bundle`) to the FHIR server
FHIR Bundle **Transaction** method is used.
- parameter bundle: `SMART.Bundle` containing FHIR resources
- parameter server: FHIR `Server` to write resources
- parameter callback: The callback to call when operation completed with a success (Bool)
*/
open func submit(bundle: SMART.Bundle, server: Server, callback: @escaping ((_ success: Bool, _ error: Error?) -> Void)) {
let handler = FHIRJSONRequestHandler(.POST, resource: bundle)
let headers = FHIRRequestHeaders([.prefer: "return=representation"])
handler.add(headers: headers)
let semaphore = DispatchSemaphore(value: 0)
server.performRequest(against: "//", handler: handler) { [weak self] (response) in
if let response = response as? FHIRServerJSONResponse,
let json = response.json,
let responseBundle = try? SMART.Bundle(json: json) {
if let results = responseBundle.entry?.filter({ $0.resource is Report}).map({ $0.resource as! Report }) {
self?.add(results)
callback(true, nil)
}
else {
callback(false, SMError.reportUnknownFHIRReport)
}
}
else {
callback(false, SMError.reportSubmissionToServerError(serverError: response.error ?? SMError.undefined(description: "unknown error when submitting reports")))
}
semaphore.signal()
}
semaphore.wait()
}
}