def test_post_resources(): try: resp1 = requests.post(f"{php}/Patient", json=patient_resc) resp1 = requests.post(f"{php}/Patient", json=patient_resc2) resp1 = requests.post(f"{php}/Observation", json=observation_resc) resp1 = requests.post(f"{php}/Observation", json=observation_resc2) resp1 = requests.post(f"{php}/Condition", json=condition_resc) resp1 = requests.post(f"{php}/Condition", json=condition_resc2) resp1 = requests.post(f"{php}/MedicationRequest", json=medication_request_resc) resp1 = requests.post(f"{php}/MedicationRequest", json=medication_request_resc2) resp1 = requests.post( f"{php}/resource", json={ "resourceTypes": ["Patient", "Observation", "Condition", "MedicationRequest"], "patientIds": [patient_id, patient_id2] }) assert resp1.status_code == 200 patients = resp1.json() assert len(patients) == 2 for patient in patients: assert patient["resourceType"] == "Bundle" assert patient["type"] == "batch-response" assert set( map(lambda x: x["resourceType"], unbundle(patient).value)) == {"Patient", "Bundle"} finally: requests.delete(f"{php}/resource")
def _get_records(ptids, fhir_plugin_id, timestamp): pt_records = [] for ptid in ptids: url_patient = f"{pds_url_base}/{fhir_plugin_id}/Patient/{ptid}" url_condition = f"{pds_url_base}/{fhir_plugin_id}/Condition?patient={ptid}" url_observation = f"{pds_url_base}/{fhir_plugin_id}/Observation?patient={ptid}" val = get(url_patient).bind(lambda patient: get(url_condition).bind( lambda condition: get(url_observation). bind(lambda observation: unbundle(condition).bind( lambda condition_unbundled: unbundle(observation).map( lambda observation_unbundled: bundle([ patient, *condition_unbundled, *observation_unbundled ])))))) pt_records.append(val) return list_traversable_either_applicative.sequence(pt_records)
def update_resource(resc_type, patient_id, bundle): coll = mongo_client[mongo_database][resc_type] res = coll.delete_many({"subject.reference": f"Patient/{patient_id}"}) logger.debug( f"bundle={bundle}, copy.deepcopy(bundle)={copy.deepcopy(bundle)}") records = unbundle(copy.deepcopy(bundle)).value if len(records) > 0: coll.insert_many(records)
def post_bundle(bundle): rescs = unbundle(bundle).value x = {} for resc in rescs: resc_type = resc["resourceType"] if resc_type in x: x[resc_type].append(resc) else: x[resc_type] = [resc] for coll, rescs in x.items(): post_resources(coll, rescs)
def get_condition_icd_code(patient_id, fhir): records = unbundle(fhir[patient_id]['Condition']).value icd_codes = [] icd_10_url = 'http://hl7.org/fhir/sid/icd-10-cm' icd_9_url = 'http://hl7.org/fhir/sid/icd-9-cm' for rec in records: codes = rec.get('code', {}).get('coding', []) for code in codes: icd_system = code.get('system', '') if icd_system == icd_10_url or icd_system == icd_9_url: icd_codes.append({ 'system': icd_system, 'code': code.get('code', '') }) return icd_codes
def test_post_resources_output_to_file(): try: resp1 = requests.post(f"{php}/Patient", json=patient_resc) resp1 = requests.post(f"{php}/Patient", json=patient_resc2) resp1 = requests.post(f"{php}/Observation", json=observation_resc) resp1 = requests.post(f"{php}/Observation", json=observation_resc2) resp1 = requests.post(f"{php}/Condition", json=condition_resc) resp1 = requests.post(f"{php}/Condition", json=condition_resc2) resp1 = requests.post(f"{php}/MedicationRequest", json=medication_request_resc) resp1 = requests.post(f"{php}/MedicationRequest", json=medication_request_resc2) files = [patient_id, patient_id2] resp = requests.post( f"{php}/resource", json={ "resourceTypes": ["Patient", "Observation", "Condition", "MedicationRequest"], "patientIds": files, "outputFile": "outputname" }) assert resp.status_code == 200 assert "$ref" in resp.json() name = resp.json()["$ref"] patients = [] for f in files: with open( os.path.join(os.environ.get("OUTPUT_DIR"), name, f + ".json")) as out: patients.append(json.load(out)) assert len(patients) == 2 for patient in patients: assert patient["resourceType"] == "Bundle" assert patient["type"] == "batch-response" assert set( map(lambda x: x["resourceType"], unbundle(patient).value)) == {"Patient", "Bundle"} finally: requests.delete(f"{php}/resource")
def _post_batch(batch): def handle_requests(requests): rescs = [] for request in requests: method = request["method"] url = request["url"] result = urlsplit(url) pcs = result.path.split("/") qcs = map(lambda x: x.split("="), result.query.split("&")) if pcs[1] == "Patient": rescs.append(_get_patient(pcs[2])) else: patient_id = None for qc in qcs: if qc[0] == "patient": patient_id = qc[1] rescs.append(_get_resource(pcs[1], patient_id)) return Right(bundle(rescs, "batch-response")) return unbundle(batch).bind(handle_requests)
def get_patient_resource_entry_array(json_in_dir, pid: str, resource_name): resource_path = os.path.join(os.path.join(json_in_dir, resource_name)) rescs_filtered = [] if os.path.isdir(resource_path): for root, _, files in os.walk(resource_path): for f in files: pid_fn = os.path.join(root, f) logger.info(f"looking into {pid_fn}") with open(pid_fn, encoding='latin-1') as pid_fp: rescs = unbundle(json.load(pid_fp)).value logger.info(f"rescs = {rescs}") if resource_name == "Patient": rescs_filtered.extend( filter(lambda x: x["id"] == pid, rescs)) else: patient_reference = f"Patient/{pid}" rescs_filtered.extend( filter( lambda x: x["subject"]["reference"] == patient_reference, rescs)) return bundle(rescs_filtered)
def handle_path(path): logger = getLogger(f"{__name__}{os.getpid()}", logging.INFO) logger.info(f"loading {path}") if not dry_run: try: with open(path) as input_stream: obj = json.load(input_stream) except: with open(path, encoding="latin-1") as input_stream: obj = json.load(input_stream) rescs = unbundle(obj).value nrescs = len(rescs) logger.info(f"{nrescs} resources loaded") maxlen = 1024 for i in range(0, nrescs, maxlen): subrescs = rescs[i:min(i + maxlen, nrescs)] subobj = bundle(subrescs) logger.info(f"ingesting {path} {i}") requests.post(f"{base_url}/Bundle", json=subobj) else: logger.info(f"post {base_url}/Bundle")
def get_patient(patient_id, fhir): return unbundle(fhir[patient_id]["Patient"]).bind(one)
def get_medication_request(patient_id, fhir): return unbundle(fhir[patient_id]["MedicationRequest"])
def get_condition(patient_id, fhir): return unbundle(fhir[patient_id]["Condition"])
def get_observation(patient_id, fhir): return unbundle(fhir[patient_id]["Observation"])
def _get_records(ptid, fhir_plugin_id, timestamp): url_patient = f"{pds_url_base}/{fhir_plugin_id}/Patient/{ptid}" url_condition = f"{pds_url_base}/{fhir_plugin_id}/Condition?patient={ptid}" url_observation = f"{pds_url_base}/{fhir_plugin_id}/Observation?patient={ptid}" return get(url_patient).bind(lambda patient: get(url_condition).bind(lambda condition: get(url_observation).bind(lambda observation: unbundle(condition).bind(lambda condition_unbundled: unbundle(observation).map(lambda observation_unbundled: bundle([ patient, *condition_unbundled, *observation_unbundled ]))))))
def get_patient(patient_id, fhir): records = unbundle(fhir) return records.bind(lambda xs: one( list(filter(lambda x: x["resourceType"] == "Patient", xs))))
def get_condition(patient_id, fhir): records = unbundle(fhir) return records.map(lambda xs: list( filter(lambda x: x["resourceType"] == "Condition", xs)))