Skip to content

Commit e47a56b

Browse files
[PRMT-435] Stitching manual trigger doesn't add all records to queue (#671)
* [PRMT-435]- Add batching to manual trigger when adding entires to the queue * [PRMT-435]- added tests and better logging * [PRMT-435]- fixed small issue and added logging * [PRMT-435]- commented test for now * [PRMT-435]- commented test for now * [PRMT-435]- added more logging * [PRMT-435]- added tests, and fixed logic * [PRMT-435]- fixed comments * [PRMT-435]- relocated batch method, and made it less specific * [PRMT-435]- accepted changed * [PRMT-435]- fixed test and merged * [PRMT-435]- added extra logging
1 parent 8cbb8ee commit e47a56b

File tree

10 files changed

+496
-367
lines changed

10 files changed

+496
-367
lines changed

lambdas/handlers/pdf_stitching_handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ def lambda_handler(event, context):
4444
@validate_sqs_event
4545
def handle_sqs_request(event, pdf_stitching_service):
4646
request_context.app_interaction = LoggingAppInteraction.STITCH_RECORD.value
47-
4847
logger.info("Received PDF Stitching SQS message event")
49-
event_message_records = event.get("Records")
50-
48+
event_message_records = event.get("Records", [])
5149
for message in event_message_records:
5250
try:
5351
request_context.patient_nhs_no = ""
@@ -56,7 +54,6 @@ def handle_sqs_request(event, pdf_stitching_service):
5654
request_context.patient_nhs_no = stitching_message.nhs_number
5755
pdf_stitching_service.process_message(stitching_message=stitching_message)
5856
except (JSONDecodeError, ValidationError) as e:
59-
logger.error("Malformed PDF Stitching SQS message")
6057
logger.error(
6158
f"Failed to parse PDF stitching from SQS message: {str(e)}",
6259
{"Results": "Failed to stitch PDF"},

lambdas/services/base/sqs_service.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import uuid
2+
13
import boto3
24
from botocore.client import Config as BotoConfig
35

@@ -16,6 +18,23 @@ def send_message_fifo(self, queue_url: str, message_body: str, group_id: str):
1618
def send_message_standard(self, queue_url: str, message_body: str):
1719
self.client.send_message(QueueUrl=queue_url, MessageBody=message_body)
1820

21+
def send_message_batch_standard(self, queue_url: str, messages: list[str], delay=0):
22+
entries = []
23+
for i, body in enumerate(messages):
24+
entries.append(
25+
{
26+
"Id": str(uuid.uuid4()),
27+
"MessageBody": body,
28+
"DelaySeconds": delay,
29+
}
30+
)
31+
32+
response = self.client.send_message_batch(
33+
QueueUrl=queue_url,
34+
Entries=entries,
35+
)
36+
return response
37+
1938
def send_message_with_attr(
2039
self, queue_url: str, message_body: str, attributes: dict
2140
):

lambdas/services/document_service.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,31 @@ def fetch_documents_from_table(
5959
query_filter: Attr | ConditionBase = None,
6060
) -> list[DocumentReference]:
6161
documents = []
62+
exclusive_start_key = None
63+
64+
while True:
65+
response = self.dynamo_service.query_table_by_index(
66+
table_name=table,
67+
index_name=index_name,
68+
search_key=search_key,
69+
search_condition=search_condition,
70+
requested_fields=DocumentReferenceMetadataFields.list(),
71+
query_filter=query_filter,
72+
exclusive_start_key=exclusive_start_key,
73+
)
6274

63-
response = self.dynamo_service.query_table_by_index(
64-
table_name=table,
65-
index_name=index_name,
66-
search_key=search_key,
67-
search_condition=search_condition,
68-
query_filter=query_filter,
69-
)
70-
71-
for item in response["Items"]:
72-
try:
73-
document = DocumentReference.model_validate(item)
74-
documents.append(document)
75-
except ValidationError as e:
76-
logger.error(f"Validation error on document: {item}")
77-
logger.error(f"{e}")
78-
continue
75+
for item in response["Items"]:
76+
try:
77+
document = DocumentReference.model_validate(item)
78+
documents.append(document)
79+
except ValidationError as e:
80+
logger.error(f"Validation error on document: {item}")
81+
logger.error(f"{e}")
82+
continue
83+
if "LastEvaluatedKey" in response:
84+
exclusive_start_key = response["LastEvaluatedKey"]
85+
else:
86+
break
7987
return documents
8088

8189
def get_nhs_numbers_based_on_ods_code(self, ods_code: str) -> list[str]:

lambdas/services/mock_pds_service.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,27 @@ def pds_request(self, nhs_number: str, *args, **kwargs) -> Response:
2727

2828
try:
2929
for file in all_mock_files:
30-
with open(file) as f:
31-
mock_pds_results.append(json.load(f))
30+
with open(file) as mock_file:
31+
mock_pds_results.append(json.load(mock_file))
3232

3333
except FileNotFoundError:
3434
raise PdsErrorException("Error when requesting patient from PDS")
3535

3636
pds_patient: dict = {}
3737
response = Response()
3838
if self.always_pass_mock:
39-
pds_patient_index = 3
40-
pds_patient = mock_pds_results[pds_patient_index]
39+
mock_file_name = "pds_patient_9000000068_M85143_gp.json"
40+
file_path = os.path.join(
41+
parent_dir_of_this_file, "services", "mock_data", mock_file_name
42+
)
43+
try:
44+
with open(file_path) as open_file:
45+
pds_patient = json.load(open_file)
46+
except FileNotFoundError:
47+
raise PdsErrorException(f"Mock file '{mock_file_name}' not found")
4148
pds_patient["id"] = nhs_number
4249
pds_patient["identifier"][0]["value"] = nhs_number
50+
print(f"pds_patient={pds_patient}")
4351
else:
4452
for result in mock_pds_results:
4553
mock_patient_nhs_number = result.get("id")

0 commit comments

Comments
 (0)