|
| 1 | +from urllib import response |
1 | 2 | from responses import logger |
2 | 3 | import simplejson as json |
3 | 4 | import os |
|
21 | 22 |
|
22 | 23 | from models.utils.validation_utils import get_vaccine_type, check_identifier_system_value |
23 | 24 |
|
24 | | - |
25 | 25 | def create_table(table_name=None, endpoint_url=None, region_name="eu-west-2"): |
26 | 26 | if not table_name: |
27 | 27 | table_name = os.environ["DYNAMODB_TABLE_NAME"] |
@@ -400,86 +400,66 @@ def find_immunizations(self, patient_identifier: str, vaccine_types: list): |
400 | 400 | import logging |
401 | 401 | logger = logging.getLogger(__name__) |
402 | 402 |
|
403 | | - logger.info("SAW fi...1: find_immunizations called with patient_identifier: '%s', vaccine_types: %s", |
404 | | - patient_identifier, vaccine_types) |
405 | | - |
406 | 403 | # Create the patient PK and log it |
407 | 404 | patient_pk = _make_patient_pk(patient_identifier) |
408 | | - logger.info("SAW fi...2: patient_pk created: '%s'", patient_pk) |
409 | 405 |
|
410 | 406 | condition = Key("PatientPK").eq(patient_pk) |
411 | 407 | is_not_deleted = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated") |
412 | 408 |
|
413 | | - logger.info("SAW fi...2.1>: is_not_deleted condition created: %s", is_not_deleted) |
414 | | - |
415 | | - logger.info("SAW fi...2.1: is_not_deleted condition str: %s", str(is_not_deleted)) |
416 | | - logger.info("SAW fi...2.1: is_not_deleted condition dir: %s", dir(is_not_deleted)) |
417 | | - logger.info("SAW fi...3: check all patientpk %s", patient_pk) |
418 | | - logger.info("SAW fi...3: check all condition %s", condition) |
| 409 | + # # check response without filter |
| 410 | + # response = self.table.query( |
| 411 | + # IndexName="PatientGSI", |
| 412 | + # KeyConditionExpression=condition |
| 413 | + # ) |
| 414 | + # if 'LastEvaluatedKey' in response: |
| 415 | + # print("More rows are available. Use LastEvaluatedKey to paginate.") |
| 416 | + # else: |
| 417 | + # print("No more rows.") |
419 | 418 |
|
420 | | - # check response without filter |
421 | | - response = self.table.query( |
422 | | - IndexName="PatientGSI", |
423 | | - KeyConditionExpression=condition |
424 | | - ) |
425 | | - if 'LastEvaluatedKey' in response: |
426 | | - print("More rows are available. Use LastEvaluatedKey to paginate.") |
427 | | - else: |
428 | | - print("No more rows.") |
| 419 | + raw_items = self.get_all_items(condition,is_not_deleted) |
429 | 420 |
|
430 | | - logger.info("SAW fi...3.1: Initial DynamoDB query Count: %s, ScannedCount: %s", response.get("Count", 0), response.get("ScannedCount", 0)) |
431 | | - |
432 | | - response = self.table.query( |
433 | | - IndexName="PatientGSI", |
434 | | - KeyConditionExpression=condition, |
435 | | - FilterExpression=is_not_deleted, |
436 | | - ) |
437 | | - |
438 | 421 | # ✅ Log the raw DynamoDB response |
439 | | - logger.info("SAW fi...4: DynamoDB query response - Count: %s, ScannedCount: %s", |
440 | | - response.get("Count", 0), response.get("ScannedCount", 0)) |
441 | | - |
442 | | - if "Items" in response: |
443 | | - raw_items = response["Items"] |
444 | | - |
445 | | - |
446 | | - logger.info("SAW fi...5: total items returned from DynamoDB: %d", len(raw_items)) |
447 | | - |
448 | | - # log all raw_items["PatientSK"] fields |
449 | | - logger.info("SAW fi...5.1: all PatientSK values: %s", |
450 | | - [item.get("PatientSK", "MISSING") for item in raw_items]) |
451 | 422 |
|
| 423 | + if raw_items: |
452 | 424 | # Log first few items for debugging |
453 | | - if raw_items: |
454 | | - logger.info("SAW fi...6: sample raw item keys: %s", list(raw_items[0].keys())) |
455 | | - logger.info("SAW fi...7: first few PatientSK values: %s", |
456 | | - [item.get("PatientSK", "MISSING") for item in raw_items[:3]]) |
457 | 425 |
|
458 | 426 | # Filter the response to contain only the requested vaccine types |
459 | 427 | items = [x for x in raw_items if x["PatientSK"].split("#")[0] in vaccine_types] |
460 | 428 |
|
461 | | - logger.info("SAW fi...8: after vaccine_types filtering (%s): %d items", vaccine_types, len(items)) |
462 | | - |
463 | | - if items: |
464 | | - # Log the vaccine types found |
465 | | - found_vaccine_types = [item["PatientSK"].split("#")[0] for item in items] |
466 | | - logger.info("SAW fi...9: found vaccine types: %s", found_vaccine_types) |
467 | | - else: |
468 | | - # Debug why no items matched |
469 | | - all_vaccine_types = [item["PatientSK"].split("#")[0] for item in raw_items] |
470 | | - logger.warning("SAW fi...10: no items matched vaccine_types filter!") |
471 | | - logger.warning("SAW fi...11: requested vaccine_types: %s", vaccine_types) |
472 | | - logger.warning("SAW fi...12: available vaccine_types in data: %s", list(set(all_vaccine_types))) |
473 | | - |
474 | 429 | # Return a list of the FHIR immunization resource JSON items |
475 | 430 | final_resources = [json.loads(item["Resource"]) for item in items] |
476 | | - logger.info("SAW fi...13: returning %d FHIR resources", len(final_resources)) |
477 | 431 |
|
478 | 432 | return final_resources |
479 | 433 | else: |
480 | | - logger.error("SAW fi...14: No 'Items' key in DynamoDB response!") |
481 | | - logger.error("SAW fi...15: Response keys: %s", list(response.keys())) |
482 | | - raise UnhandledResponseError(message=f"Unhandled error. Query failed", response=response) |
| 434 | + logger.warning("no items matched patient_identifier filter!") |
| 435 | + return [] |
| 436 | + |
| 437 | + def get_all_items(self, condition, is_not_deleted): |
| 438 | + """Query DynamoDB and paginate through all results.""" |
| 439 | + all_items = [] |
| 440 | + last_evaluated_key = None |
| 441 | + |
| 442 | + while True: |
| 443 | + query_args = { |
| 444 | + "IndexName": "PatientGSI", |
| 445 | + "KeyConditionExpression": condition, |
| 446 | + "FilterExpression": is_not_deleted, |
| 447 | + } |
| 448 | + if last_evaluated_key: |
| 449 | + query_args["ExclusiveStartKey"] = last_evaluated_key |
| 450 | + |
| 451 | + response = self.table.query(**query_args) |
| 452 | + if "Items" not in response: |
| 453 | + raise UnhandledResponseError(message="No Items in DynamoDB response", response=response) |
| 454 | + |
| 455 | + items = response.get("Items", []) |
| 456 | + all_items.extend(items) |
| 457 | + |
| 458 | + last_evaluated_key = response.get("LastEvaluatedKey") |
| 459 | + if not last_evaluated_key: |
| 460 | + break |
| 461 | + |
| 462 | + return all_items |
483 | 463 |
|
484 | 464 | @staticmethod |
485 | 465 | def _handle_dynamo_response(response): |
|
0 commit comments