Skip to content

Commit

Permalink
[prmp-1126] changes to script
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Nov 8, 2024
1 parent 2ba0523 commit 80381b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lambdas/models/nrl_fhir_document_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FhirDocumentReference(BaseModel):
snomed_code_doc_type: str = "None"
snomed_code_category: str = "None"
snomed_code_category_display: str = "Care plan"
attachment: Optional[NrlAttachment] = {}
attachment: Optional[NrlAttachment] = NrlAttachment()

def build_fhir_dict(self):
snomed_url = "http://snomed.info/sct"
Expand Down
27 changes: 18 additions & 9 deletions lambdas/scripts/batch_nrl_create_pointer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os

from boto3.dynamodb.conditions import Attr
from botocore.exceptions import ClientError
from enums.metadata_field_names import DocumentReferenceMetadataFields
from models.nrl_fhir_document_reference import FhirDocumentReference
Expand All @@ -22,8 +23,8 @@ class NRLBatchCreatePointer:
def __init__(self):
self.nrl_service = NrlApiService(SSMService)
self.dynamo_service = DynamoDBService()
self.table_name = os.getenv("table_name")
self.progress: [ProgressForPatient] = []
self.table_name = os.getenv("table_name", "")
self.progress: list[ProgressForPatient] = []
self.progress_store = "nrl_batch_update_progress.json"

def main(self):
Expand Down Expand Up @@ -62,20 +63,23 @@ def list_all_entries(self):

table = DynamoDBService().get_table(self.table_name)
results = {}
columns_to_fetch = [DocumentReferenceMetadataFields.NHS_NUMBER.value]

response = table.scan(ProjectionExpression=columns_to_fetch)
columns_to_fetch = DocumentReferenceMetadataFields.NHS_NUMBER.value

response = table.scan(
ProjectionExpression=columns_to_fetch,
FilterExpression=Attr(columns_to_fetch).exists(),
)
# handle pagination
while "LastEvaluatedKey" in response:

results += self.create_progress_dict(response, results)
results.update(self.create_progress_dict(response, results))
response = table.scan(
ExclusiveStartKey=response["LastEvaluatedKey"],
ProjectionExpression=columns_to_fetch,
FilterExpression=Attr(columns_to_fetch).exists(),
)

results += self.create_progress_dict(response, results)
results.update(self.create_progress_dict(response, results))
self.progress = list(results.values())
logger.info(f"Totally {len(results)} patients found.")

Expand All @@ -93,7 +97,7 @@ def create_nrl_pointer(self):
# TODO update snomed and attachment, once the API is ready
document = (
FhirDocumentReference(
nhs_number=patient.nhs_number,
nhsNumber=patient.nhs_number,
custodian=self.nrl_service.end_user_ods_code,
)
.build_fhir_dict()
Expand All @@ -113,9 +117,14 @@ def create_nrl_pointer(self):
)
)
logger.error(str(e))
self.save_progress()

def save_progress(self):
with open(self.progress_store, "w") as f:
progress_list = [patient.model_dump() for patient in self.progress]
json_str = json.dumps(progress_list)
return f.write(json_str)
f.write(json_str)


if __name__ == "__main__":
NRLBatchCreatePointer().main()

0 comments on commit 80381b6

Please sign in to comment.