Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

834 message parser meta data #853

Merged
merged 9 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion containers/message-parser/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.utils import (
load_parsing_schema,
get_parsers,
get_metadata,
convert_to_fhir,
get_credential_manager,
search_for_required_values,
Expand Down Expand Up @@ -69,6 +70,10 @@ class ParseMessageInput(BaseModel):
"FHIR converter when conversion to FHIR is required.",
default=None,
)
include_metadata: Optional[Literal["true", "false"]] = Field(
description="Boolean to include metadata in the response.",
default=None,
)
message: Union[str, dict] = Field(description="The message to be parsed.")

@root_validator
Expand Down Expand Up @@ -200,7 +205,8 @@ async def parse_message_endpoint(
)
values.append(value)
parsed_values[field] = values

if input.include_metadata == "true":
parsed_values = get_metadata(parsed_values, parsing_schema)
return {"message": "Parsing succeeded!", "parsed_values": parsed_values}


Expand Down
29 changes: 29 additions & 0 deletions containers/message-parser/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from phdi.cloud.azure import AzureCredentialManager
from phdi.cloud.core import BaseCredentialManager
from phdi.cloud.gcp import GcpCredentialManager
import re


@cache
Expand Down Expand Up @@ -99,6 +100,34 @@ def get_parsers(extraction_schema: frozendict) -> frozendict:
return frozendict(parsers)


def get_metadata(parsed_values: dict, schema):
data = {}
for key, value in parsed_values.items():
if key not in schema:
data[key] = field_metadata(value=value)
else:
fhir_path = schema[key]["fhir_path"] if "fhir_path" in schema[key] else ""
match = re.search(r"resourceType\s*=\s*'([^']+)'", fhir_path)
resource_type = match.group(1) if match and match.group(1) else ""
data_type = schema[key]["data_type"] if "data_type" in schema[key] else ""
data[key] = field_metadata(
value=value,
fhir_path=fhir_path,
data_type=data_type,
resource_type=resource_type,
)
return data


def field_metadata(value="", fhir_path="", data_type="", resource_type=""):
return {
"value": value,
"fhir_path": fhir_path,
"data_type": data_type,
"resource_type": resource_type,
}


def search_for_required_values(input: dict, required_values: list) -> str:
"""
Search for required values in the input dictionary and the environment.
Expand Down
114 changes: 114 additions & 0 deletions containers/message-parser/tests/test_parse_message_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,51 @@
},
}

expected_successful_response_with_meta_data = {
"message": "Parsing succeeded!",
"parsed_values": {
"first_name": {
"value": "John ",
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').name"
+ ".first().given.first()",
"data_type": "string",
"resource_type": "Patient",
},
"last_name": {
"value": "doe",
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').name"
+ ".first().family",
"data_type": "string",
"resource_type": "Patient",
},
"latitude": {
"value": None,
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient')"
+ ".address.extension.where"
+ "(url='http://hl7.org/fhir/StructureDefinition/geolocation').extension."
+ "where(url='latitude').valueDecimal",
"data_type": "float",
"resource_type": "Patient",
},
"longitude": {
"value": None,
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').address"
+ ".extension.where"
+ "(url='http://hl7.org/fhir/StructureDefinition/geolocation').extension"
+ ".where(url='longitude').valueDecimal",
"data_type": "float",
"resource_type": "Patient",
},
"active_problems": {
"value": [],
"fhir_path": "Bundle.entry.resource.where(resourceType='Condition')"
+ ".where(category.coding.code='problem-item-list')",
"data_type": "array",
"resource_type": "Condition",
},
},
}

expected_successful_response_floats = {
"message": "Parsing succeeded!",
"parsed_values": {
Expand All @@ -58,6 +103,51 @@
},
}

expected_successful_response_floats_with_meta_data = {
"message": "Parsing succeeded!",
"parsed_values": {
"first_name": {
"value": "John ",
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').name"
+ ".first().given.first()",
"data_type": "string",
"resource_type": "Patient",
},
"last_name": {
"value": "doe",
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').name"
+ ".first().family",
"data_type": "string",
"resource_type": "Patient",
},
"latitude": {
"value": "34.58002",
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').address"
+ ".extension"
+ ".where(url='http://hl7.org/fhir/StructureDefinition/geolocation')"
+ ".extension.where(url='latitude').valueDecimal",
"data_type": "float",
"resource_type": "Patient",
},
"longitude": {
"value": "-118.08925",
"fhir_path": "Bundle.entry.resource.where(resourceType = 'Patient').address"
+ ".extension"
+ ".where(url='http://hl7.org/fhir/StructureDefinition/geolocation')"
+ ".extension.where(url='longitude').valueDecimal",
"data_type": "float",
"resource_type": "Patient",
},
"active_problems": {
"value": [],
"fhir_path": "Bundle.entry.resource.where(resourceType='Condition')"
+ ".where(category.coding.code='problem-item-list')",
"data_type": "array",
"resource_type": "Condition",
},
},
}


def test_parse_message_success_internal_schema():
test_request = {
Expand All @@ -82,6 +172,30 @@ def test_parse_message_success_internal_schema():
assert actual_response2.json() == expected_successful_response_floats


def test_parse_message_success_internal_schema_with_metadata():
test_request = {
"message_format": "fhir",
"parsing_schema_name": "test_schema.json",
"include_metadata": "true",
"message": fhir_bundle,
}

actual_response = client.post("/parse_message", json=test_request)
assert actual_response.status_code == 200
assert actual_response.json() == expected_successful_response_with_meta_data

test_request2 = {
"message_format": "fhir",
"include_metadata": "true",
"parsing_schema_name": "test_schema.json",
"message": fhir_bundle_w_float,
}

actual_response2 = client.post("/parse_message", json=test_request2)
assert actual_response2.status_code == 200
assert actual_response2.json() == expected_successful_response_floats_with_meta_data


def test_parse_message_success_external_schema():
request = {
"message_format": "fhir",
Expand Down
48 changes: 48 additions & 0 deletions containers/message-parser/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
search_for_required_values,
convert_to_fhir,
freeze_parsing_schema,
field_metadata,
get_metadata,
)
from app.config import get_settings

Expand Down Expand Up @@ -170,3 +172,49 @@ def test_freeze_parsing_schema():
for key in test_schema:
for subkey in test_schema[key]:
assert test_schema[key][subkey] == frozen_schema[key][subkey]


def test_field_metadata():
expected_result = {
"value": "foo",
"fhir_path": "bar",
"data_type": "biz",
"resource_type": "baz",
}
assert (
field_metadata(
value="foo", fhir_path="bar", data_type="biz", resource_type="baz"
)
== expected_result
)
expected_result2 = {
"value": "",
"fhir_path": "",
"data_type": "",
"resource_type": "",
}
assert field_metadata() == expected_result2


def test_get_metadata():
example_parsed_values = {"foo": "bar", "fiz": "biz", "baz": "Null"}
example_schema = {
"foo": {
"fhir_path": "Bundle.entry.resource.where(resourceType='Foo').biz",
"data_type": "string",
"nullable": False,
},
"baz": {},
}
expected_result = {
"foo": {
"value": "bar",
"fhir_path": "Bundle.entry.resource.where(resourceType='Foo').biz",
"data_type": "string",
"resource_type": "Foo",
},
"fiz": {"value": "biz", "fhir_path": "", "data_type": "", "resource_type": ""},
"baz": {"value": "Null", "fhir_path": "", "data_type": "", "resource_type": ""},
}
result = get_metadata(parsed_values=example_parsed_values, schema=example_schema)
assert result == expected_result
Loading