-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.py
70 lines (53 loc) · 2.31 KB
/
runner.py
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
import time
from datetime import datetime
import jsonlines
from fhir.resources.patient import Patient
from bcda_client import BCDAClient, ResourceType
from sandboxes import EXTRA_SMALL_ACO, SMALL_ADV_ACO, LARGE_ADV_ACO
def main():
client = BCDAClient(EXTRA_SMALL_ACO)
client.print_metadata()
# types to request.
# asking for less types makes the job take less time to complete.
# resource types are
# - Patient (# as per the config selected)
# - Coverage (# x4 lines)
# - ExplanationOfBenefit (# x40 lines)
resource_types = [ResourceType.Patient]
# useful to get a delta from last check; the default time below gets us all data at the moment.
# unfortunately, the synthetic data all has the exact same lastUpdated datetime, so can't play
# around with it much. setting to now returns no records, as expected.
since = datetime.fromisoformat("2021-06-01T08:00:00.000-05:00")
print("Requesting files since " + str(since))
client.run_patient_export_job(resource_types, since)
print("Fetching job result")
job_result = None
i = 0
while i != 720 and not job_result:
job_result = client.fetch_job_result()
time.sleep(3)
i += 1
if not job_result:
client.cancel_current_job()
raise Exception("Job taking longer than expected, check later")
if job_result.is_error:
print(str(job_result.error))
raise Exception("Errors returned")
if job_result.is_empty:
print(f"Job returned no data (likely no data since {str(since)})")
return
output_files = job_result.output_map()
print(f"Received URLs for the following resource types: " + ",".join(output_files.keys()))
resource_type = ResourceType.Patient
file_url = output_files[resource_type.value]
stream = client.fetch_data_stream(resource_type, file_url)
print(f"Loading records from stream")
with jsonlines.Reader(stream) as reader:
for obj in reader:
first_patient = Patient(**obj)
first_name = first_patient.name[0].given[0]
last_name = first_patient.name[0].family
last_updated = first_patient.meta.lastUpdated
print(f"Found patient {first_name} {last_name} with id {first_patient.id}, last updated {last_updated}")
if __name__ == "__main__":
main()