|
1 | 1 | import argparse
|
2 |
| -import json |
| 2 | +import io |
3 | 3 | import os
|
| 4 | +import zipfile |
4 | 5 |
|
5 | 6 | import grequests
|
6 | 7 |
|
7 |
| -URL = "http://orchestration-service:8080" |
| 8 | +UPLOAD_URL = "http://host.docker.internal:3000/ecr-viewer/api/process-zip" |
8 | 9 | BASEDIR = os.path.dirname(os.path.abspath(__file__))
|
9 | 10 |
|
10 | 11 |
|
11 | 12 | def _get_args():
|
12 | 13 | parser = argparse.ArgumentParser(
|
13 |
| - prog="Create Seed Data", |
14 |
| - description="Convert eICR and RR files to FHIR bundles and insert them into the database.", |
15 |
| - epilog="For each directory in baseECR the script will look for a `CDA_eICR.xml` and `CDA_RR.xml` file. If they are found, it will convert them into a FHIR bundle (saved as `bundle.json`) and insert that into the database using the Orchestration service.", |
16 |
| - ) |
17 |
| - parser.add_argument( |
18 |
| - "-s", |
19 |
| - "--skip_convert", |
20 |
| - action="store_true", |
21 |
| - help="If this is set, if `bundle.json` already exists, the script will not look for `CDA_eICR.xml` and `CDA_RR.xml` files to convert them again, and use the existing `bundle.json`.", |
| 14 | + description="Zip subfolders and upload them to the ECR Viewer API.", |
22 | 15 | )
|
23 | 16 | return parser.parse_args()
|
24 | 17 |
|
25 | 18 |
|
26 |
| -def _process_files(args): |
27 |
| - """ |
28 |
| - Convert eICR and RR into FHIR bundles using the FHIR converter. |
| 19 | +def zip_folder(folder_path): |
| 20 | + """Zips the given folder into an in-memory ZIP file.""" |
| 21 | + zip_buffer = io.BytesIO() |
| 22 | + with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file: |
| 23 | + for root, _, files in os.walk(folder_path): |
| 24 | + for file in files: |
| 25 | + file_path = os.path.join(root, file) |
| 26 | + arcname = os.path.relpath(file_path, folder_path) |
| 27 | + zip_file.write(file_path, arcname=arcname) |
| 28 | + zip_buffer.seek(0) # Move to the beginning of the buffer |
| 29 | + return zip_buffer |
| 30 | + |
| 31 | + |
| 32 | +def _process_files(): |
| 33 | + """Zips subfolders and sends them to the API.""" |
| 34 | + print("Processing subfolders...") |
29 | 35 |
|
30 |
| - :return: A list of fhir bundles |
31 |
| - """ |
32 |
| - print("Converting files...") |
33 | 36 | subfolders_raw = os.getenv("SEED_DATA_DIRECTORIES")
|
| 37 | + if not subfolders_raw: |
| 38 | + print("No subfolders found in SEED_DATA_DIRECTORIES.") |
| 39 | + return |
| 40 | + |
34 | 41 | subfolders = subfolders_raw.split(",")
|
35 | 42 |
|
36 |
| - # Holds all of the rquests we are going to make |
37 | 43 | requests = []
|
38 | 44 | folder_paths = []
|
39 |
| - configName = "integrated.json" |
40 |
| - config = os.getenv("CONFIG_NAME") or "" |
41 |
| - if "_SQLSERVER_" in config: |
42 |
| - configName = "non-integrated-extended.json" |
43 |
| - elif "_PG_" in config: |
44 |
| - configName = "non-integrated-core.json" |
45 |
| - |
46 |
| - def _process_eicrs(subfolder, folder, folder_path, payload): |
47 |
| - r = grequests.post(f"{URL}/process-message", json=payload) |
48 |
| - requests.append(r) |
49 |
| - folder_paths.append(folder_path) |
50 |
| - |
51 |
| - # Iterate over the subfolders to collect requests |
52 | 45 | for subfolder in subfolders:
|
53 | 46 | subfolder_path = os.path.join(BASEDIR, "baseECR", subfolder)
|
54 | 47 |
|
55 |
| - # Check if the subfolder exists and is a directory |
56 | 48 | if not os.path.isdir(subfolder_path):
|
57 |
| - print(f"{subfolder_path} is not a valid directory.") |
| 49 | + print(f"Skipping: {subfolder_path} is not a valid directory.") |
58 | 50 | continue
|
59 | 51 |
|
60 |
| - # Now iterate through the folders inside each subfolder |
61 | 52 | for folder in os.listdir(subfolder_path):
|
62 | 53 | folder_path = os.path.join(subfolder_path, folder)
|
63 |
| - |
64 |
| - # Check if it's a directory |
65 | 54 | if not os.path.isdir(folder_path):
|
66 | 55 | continue
|
67 | 56 |
|
68 |
| - if os.path.exists(os.path.join(folder_path, "bundle.json")) and ( |
69 |
| - args.skip_convert |
70 |
| - or not os.path.exists(os.path.join(folder_path, "CDA_eICR.xml")) |
71 |
| - ): |
72 |
| - # Just upload the bundle |
73 |
| - with open(os.path.join(folder_path, "bundle.json")) as fhir_file: |
74 |
| - payload = { |
75 |
| - "message_type": "fhir", |
76 |
| - "data_type": "fhir", |
77 |
| - "config_file_name": "save-bundle-to-ecr-viewer.json", |
78 |
| - "message": json.load(fhir_file), |
79 |
| - } |
80 |
| - _process_eicrs(subfolder, folder, folder_path, payload) |
81 |
| - |
82 |
| - # If we are not just inserting the bundle, check for the necessary files |
83 |
| - elif os.path.exists(os.path.join(folder_path, "CDA_eICR.xml")): |
84 |
| - # Get the RR data if available |
85 |
| - rr_data = None |
86 |
| - if os.path.exists(os.path.join(folder_path, "CDA_RR.xml")): |
87 |
| - with ( |
88 |
| - open(os.path.join(folder_path, "CDA_RR.xml")) as rr_file, |
89 |
| - ): |
90 |
| - rr_data = rr_file.read() |
91 |
| - |
92 |
| - # Open the necessary files in the folder |
93 |
| - with ( |
94 |
| - open(os.path.join(folder_path, "CDA_eICR.xml")) as eicr_file, |
95 |
| - ): |
96 |
| - payload = { |
97 |
| - "message_type": "ecr", |
98 |
| - "data_type": "ecr", |
99 |
| - "config_file_name": configName, |
100 |
| - "message": eicr_file.read(), |
101 |
| - "rr_data": rr_data, |
102 |
| - } |
103 |
| - |
104 |
| - _process_eicrs(subfolder, folder, folder_path, payload) |
105 |
| - # If neither `bundle.json` nor `CDA_eICR.xml` exists, skip processing |
106 |
| - else: |
107 |
| - print( |
108 |
| - f"Neither `bundle.json` nor `CDA_eICR.xml` found in {folder_path}. Skipping." |
109 |
| - ) |
110 |
| - continue |
| 57 | + print(f"Zipping and uploading: {folder_path}") |
| 58 | + |
| 59 | + zip_buffer = zip_folder(folder_path) |
| 60 | + |
| 61 | + files = [("upload_file", (f"{folder}.zip", zip_buffer, "application/zip"))] |
| 62 | + print(files) |
| 63 | + request = grequests.post(UPLOAD_URL, files=files) |
111 | 64 |
|
112 |
| - # Asynchronously send our collected requests |
| 65 | + requests.append(request) |
| 66 | + folder_paths.append(folder_path) |
| 67 | + |
| 68 | + print(f"Sending {len(requests)} ZIP files...") |
| 69 | + |
| 70 | + # Send requests asynchronously |
113 | 71 | n = 0
|
114 | 72 | failed = []
|
115 | 73 | num_requests = len(requests)
|
116 |
| - print(f"Starting conversion and load of {num_requests} requests") |
117 | 74 | for index, response in grequests.imap_enumerated(requests, size=8):
|
118 | 75 | n += 1
|
119 |
| - print(f"Received response {n} of {num_requests}") |
120 | 76 | folder_path = folder_paths[index]
|
121 |
| - responses_json = response.json() |
122 |
| - if response.status_code != 200: |
| 77 | + if response is None: |
123 | 78 | failed.append(folder_path)
|
124 |
| - print(f"Failed to convert {folder_path}.\nResponse:\n{responses_json}") |
| 79 | + print( |
| 80 | + f"Received response {n} of {num_requests} - Failed to upload {folder_path}: No response received" |
| 81 | + ) |
125 | 82 | continue
|
126 |
| - |
127 |
| - if "responses" in responses_json.get("processed_values", {}): |
128 |
| - for response in responses_json["processed_values"]["responses"]: |
129 |
| - if "stamped_ecr" in response: |
130 |
| - with open( |
131 |
| - os.path.join(folder_path, "bundle.json"), |
132 |
| - "w", |
133 |
| - ) as fhir_file: |
134 |
| - json.dump( |
135 |
| - response["stamped_ecr"]["extended_bundle"], |
136 |
| - fhir_file, |
137 |
| - indent=4, |
138 |
| - ) |
139 |
| - print(f"Converted {folder_path} successfully.") |
| 83 | + if response.status_code != 200: |
| 84 | + failed.append(folder_path) |
| 85 | + print( |
| 86 | + f"Received response {n} of {num_requests} - Failed to upload {folder_path}. Status: {response.status_code}" |
| 87 | + ) |
| 88 | + else: |
| 89 | + print( |
| 90 | + f"Received response {n} of {num_requests} - Successfully uploaded {folder_path}" |
| 91 | + ) |
140 | 92 |
|
141 | 93 | print(
|
142 | 94 | f"Conversion complete: {n} records attempted and {len(failed)} failed : {failed}"
|
143 | 95 | )
|
144 |
| - |
145 |
| - if len(failed) > 0: |
| 96 | + if failed: |
146 | 97 | exit(1)
|
147 | 98 |
|
148 | 99 |
|
149 | 100 | if __name__ == "__main__":
|
150 | 101 | args = _get_args()
|
151 |
| - _process_files(args) |
| 102 | + _process_files() |
0 commit comments