Skip to content

Commit b9d6cb4

Browse files
lina-rothpre-commit-ci[bot]BobanLaustin-hall-skylight
authored
chore: update seed-script to use new ecr viewer processing endpoint (#551)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Boban Ljuljdjurovic <[email protected]> Co-authored-by: Austin Hall <[email protected]>
1 parent 8527003 commit b9d6cb4

File tree

3 files changed

+57
-102
lines changed

3 files changed

+57
-102
lines changed

containers/ecr-viewer/docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: ecr-viewer
22

33
services:
44
ecr-viewer:
5+
ports:
6+
- "3000:3000"
57
build:
68
context: ./
79
dockerfile: Dockerfile
Lines changed: 53 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,102 @@
11
import argparse
2-
import json
2+
import io
33
import os
4+
import zipfile
45

56
import grequests
67

7-
URL = "http://orchestration-service:8080"
8+
UPLOAD_URL = "http://host.docker.internal:3000/ecr-viewer/api/process-zip"
89
BASEDIR = os.path.dirname(os.path.abspath(__file__))
910

1011

1112
def _get_args():
1213
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.",
2215
)
2316
return parser.parse_args()
2417

2518

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...")
2935

30-
:return: A list of fhir bundles
31-
"""
32-
print("Converting files...")
3336
subfolders_raw = os.getenv("SEED_DATA_DIRECTORIES")
37+
if not subfolders_raw:
38+
print("No subfolders found in SEED_DATA_DIRECTORIES.")
39+
return
40+
3441
subfolders = subfolders_raw.split(",")
3542

36-
# Holds all of the rquests we are going to make
3743
requests = []
3844
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
5245
for subfolder in subfolders:
5346
subfolder_path = os.path.join(BASEDIR, "baseECR", subfolder)
5447

55-
# Check if the subfolder exists and is a directory
5648
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.")
5850
continue
5951

60-
# Now iterate through the folders inside each subfolder
6152
for folder in os.listdir(subfolder_path):
6253
folder_path = os.path.join(subfolder_path, folder)
63-
64-
# Check if it's a directory
6554
if not os.path.isdir(folder_path):
6655
continue
6756

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)
11164

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
11371
n = 0
11472
failed = []
11573
num_requests = len(requests)
116-
print(f"Starting conversion and load of {num_requests} requests")
11774
for index, response in grequests.imap_enumerated(requests, size=8):
11875
n += 1
119-
print(f"Received response {n} of {num_requests}")
12076
folder_path = folder_paths[index]
121-
responses_json = response.json()
122-
if response.status_code != 200:
77+
if response is None:
12378
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+
)
12582
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+
)
14092

14193
print(
14294
f"Conversion complete: {n} records attempted and {len(failed)} failed : {failed}"
14395
)
144-
145-
if len(failed) > 0:
96+
if failed:
14697
exit(1)
14798

14899

149100
if __name__ == "__main__":
150101
args = _get_args()
151-
_process_files(args)
102+
_process_files()

containers/ecr-viewer/seed-scripts/docker-compose-seed.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@ services:
7777
environment:
7878
- CONFIG_NAME=${CONFIG_NAME}
7979
- SEED_DATA_DIRECTORIES=${SEED_DATA_DIRECTORIES:-star-wars}
80+
extra_hosts:
81+
- host.docker.internal:host-gateway

0 commit comments

Comments
 (0)