Another update
This commit is contained in:
@@ -5,36 +5,41 @@ import subprocess
|
||||
from zipfile import ZipFile
|
||||
import json
|
||||
|
||||
def available_states():
|
||||
#TODO: Make a setting for this path
|
||||
location = Path('/opt/development/synthea_webservice/synthea/src/main/resources/geography/')
|
||||
from django.conf import settings
|
||||
from uuid import uuid4
|
||||
|
||||
df = pd.read_csv(location / 'timezones.csv', index_col=False)
|
||||
# The state information is expected in the first column
|
||||
states = df[df.columns[0]].to_list()
|
||||
states.sort()
|
||||
|
||||
def available_states():
|
||||
states = []
|
||||
# Read the timezones.csv file from the Synthea resources. This should give us all the 'state' on the first column
|
||||
df = pd.read_csv(settings.SYNTHEA_STATES_DIR / 'timezones.csv', index_col=False)
|
||||
for state in df[df.columns[0]].to_list():
|
||||
states.append({'id' : state , 'name' : state})
|
||||
|
||||
#states = df[df.columns[0]].to_list()
|
||||
# Sort on name
|
||||
states = sorted(states, key=lambda k: k['name'].lower())
|
||||
#states.sort()
|
||||
return states
|
||||
|
||||
def available_modules():
|
||||
#TODO: Make a setting for this path
|
||||
location = Path('/opt/development/synthea_webservice/synthea/src/main/resources/modules/')
|
||||
|
||||
# Assumption here: A folder is a single module. And all .json in the main modules folder is a module.
|
||||
# Assumption here: Only .json files in the main folder are modules. The rest are submodules...
|
||||
modules = []
|
||||
for module in location.iterdir():
|
||||
for module in settings.SYNTHEA_MODULE_DIR.iterdir():
|
||||
if module.is_file() and module.suffix == '.json':
|
||||
data = json.loads(module.read_text())
|
||||
modules.append({'module' : module.name.replace('.json',''), 'name' : data['name']})
|
||||
modules.append({'id' : module.name.replace('.json',''), 'name' : data['name']})
|
||||
|
||||
modules = sorted(modules, key=lambda k: k['name'].lower())
|
||||
return modules
|
||||
|
||||
def run_synthea(state = None, population = None, gender = None, age = None, module = None):
|
||||
# TODO: Make synthea setting(s)
|
||||
location = '/opt/development/synthea_webservice/synthea/'
|
||||
synthea_cmd = ['/opt/development/synthea_webservice/synthea/run_synthea']
|
||||
# Add a unique dir to the output, so multiple Synthea processes can run parallel
|
||||
temp_id = uuid4().hex
|
||||
output_folder = settings.SYNTHEA_OUTPUT_DIR / temp_id
|
||||
|
||||
synthea_cmd = [settings.SYNTHEA_BASE_DIR / 'run_synthea','--exporter.baseDirectory',output_folder]
|
||||
zip_file = 'Synthea_'
|
||||
zip_export = location
|
||||
|
||||
if population:
|
||||
synthea_cmd.append('-p')
|
||||
@@ -62,7 +67,7 @@ def run_synthea(state = None, population = None, gender = None, age = None, modu
|
||||
|
||||
process_ok = False
|
||||
log = ''
|
||||
with subprocess.Popen(synthea_cmd,cwd=location, stdout=subprocess.PIPE,stderr=subprocess.PIPE) as process:
|
||||
with subprocess.Popen(synthea_cmd,cwd=settings.SYNTHEA_BASE_DIR, stdout=subprocess.PIPE,stderr=subprocess.PIPE) as process:
|
||||
for line in process.stdout:
|
||||
line = line.decode('utf8')
|
||||
log += line
|
||||
@@ -70,14 +75,10 @@ def run_synthea(state = None, population = None, gender = None, age = None, modu
|
||||
process_ok = line.find('BUILD SUCCESSFUL') >= 0
|
||||
|
||||
if process_ok:
|
||||
with ZipFile(f'{zip_export}/{zip_file}.zip', 'w') as export:
|
||||
for file in Path(location + 'output/fhir_stu3').iterdir():
|
||||
with ZipFile(f'{output_folder}/{zip_file}_{temp_id}.zip', 'w') as export:
|
||||
for file in (output_folder / settings.SYNTHEA_EXPORT_TYPE).iterdir():
|
||||
export.write(file,file.name)
|
||||
|
||||
return Path(f'{zip_export}/{zip_file}.zip')
|
||||
return (log,Path(f'{output_folder}/{zip_file}_{temp_id}.zip'))
|
||||
else:
|
||||
raise Exception(log)
|
||||
|
||||
|
||||
|
||||
|
||||
raise Exception(log)
|
Reference in New Issue
Block a user