|
| 1 | +from urllib import response |
| 2 | +from responses import logger |
1 | 3 | import simplejson as json |
2 | 4 | import os |
3 | 5 | import time |
@@ -394,70 +396,49 @@ def delete_immunization( |
394 | 396 |
|
395 | 397 | def find_immunizations(self, patient_identifier: str, vaccine_types: list): |
396 | 398 | """it should find all of the specified patient's Immunization events for all of the specified vaccine_types""" |
397 | | - |
398 | | - # ✅ Add debug logging |
399 | | - import logging |
400 | | - logger = logging.getLogger(__name__) |
401 | | - |
402 | | - logger.info("SAW fi...1: find_immunizations called with patient_identifier: '%s', vaccine_types: %s", |
403 | | - patient_identifier, vaccine_types) |
404 | | - |
405 | | - # Create the patient PK and log it |
406 | | - patient_pk = _make_patient_pk(patient_identifier) |
407 | | - logger.info("SAW fi...2: patient_pk created: '%s'", patient_pk) |
408 | | - |
409 | | - condition = Key("PatientPK").eq(patient_pk) |
| 399 | + condition = Key("PatientPK").eq(_make_patient_pk(patient_identifier)) |
410 | 400 | is_not_deleted = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated") |
411 | 401 |
|
412 | | - logger.info("SAW fi...2.1: is_not_deleted condition created: %s", is_not_deleted) |
| 402 | + raw_items = self.get_all_items(condition, is_not_deleted) |
413 | 403 |
|
414 | | - logger.info("SAW fi...3: executing DynamoDB query on PatientGSI index") |
415 | | - |
416 | | - response = self.table.query( |
417 | | - IndexName="PatientGSI", |
418 | | - KeyConditionExpression=condition, |
419 | | - FilterExpression=is_not_deleted, |
420 | | - ) |
421 | | - |
422 | | - # ✅ Log the raw DynamoDB response |
423 | | - logger.info("SAW fi...4: DynamoDB query response - Count: %s, ScannedCount: %s", |
424 | | - response.get("Count", 0), response.get("ScannedCount", 0)) |
425 | | - |
426 | | - if "Items" in response: |
427 | | - raw_items = response["Items"] |
428 | | - logger.info("SAW fi...5: total items returned from DynamoDB: %d", len(raw_items)) |
429 | | - |
430 | | - # Log first few items for debugging |
431 | | - if raw_items: |
432 | | - logger.info("SAW fi...6: sample raw item keys: %s", list(raw_items[0].keys())) |
433 | | - logger.info("SAW fi...7: first few PatientSK values: %s", |
434 | | - [item.get("PatientSK", "MISSING") for item in raw_items[:3]]) |
435 | | - |
| 404 | + if raw_items: |
436 | 405 | # Filter the response to contain only the requested vaccine types |
437 | 406 | items = [x for x in raw_items if x["PatientSK"].split("#")[0] in vaccine_types] |
438 | 407 |
|
439 | | - logger.info("SAW fi...8: after vaccine_types filtering (%s): %d items", vaccine_types, len(items)) |
440 | | - |
441 | | - if items: |
442 | | - # Log the vaccine types found |
443 | | - found_vaccine_types = [item["PatientSK"].split("#")[0] for item in items] |
444 | | - logger.info("SAW fi...9: found vaccine types: %s", found_vaccine_types) |
445 | | - else: |
446 | | - # Debug why no items matched |
447 | | - all_vaccine_types = [item["PatientSK"].split("#")[0] for item in raw_items] |
448 | | - logger.warning("SAW fi...10: no items matched vaccine_types filter!") |
449 | | - logger.warning("SAW fi...11: requested vaccine_types: %s", vaccine_types) |
450 | | - logger.warning("SAW fi...12: available vaccine_types in data: %s", list(set(all_vaccine_types))) |
451 | | - |
452 | 408 | # Return a list of the FHIR immunization resource JSON items |
453 | 409 | final_resources = [json.loads(item["Resource"]) for item in items] |
454 | | - logger.info("SAW fi...13: returning %d FHIR resources", len(final_resources)) |
455 | 410 |
|
456 | 411 | return final_resources |
457 | 412 | else: |
458 | | - logger.error("SAW fi...14: No 'Items' key in DynamoDB response!") |
459 | | - logger.error("SAW fi...15: Response keys: %s", list(response.keys())) |
460 | | - raise UnhandledResponseError(message=f"Unhandled error. Query failed", response=response) |
| 413 | + logger.warning("no items matched patient_identifier filter!") |
| 414 | + return [] |
| 415 | + |
| 416 | + def get_all_items(self, condition, is_not_deleted): |
| 417 | + """Query DynamoDB and paginate through all results.""" |
| 418 | + all_items = [] |
| 419 | + last_evaluated_key = None |
| 420 | + |
| 421 | + while True: |
| 422 | + query_args = { |
| 423 | + "IndexName": "PatientGSI", |
| 424 | + "KeyConditionExpression": condition, |
| 425 | + "FilterExpression": is_not_deleted, |
| 426 | + } |
| 427 | + if last_evaluated_key: |
| 428 | + query_args["ExclusiveStartKey"] = last_evaluated_key |
| 429 | + |
| 430 | + response = self.table.query(**query_args) |
| 431 | + if "Items" not in response: |
| 432 | + raise UnhandledResponseError(message="No Items in DynamoDB response", response=response) |
| 433 | + |
| 434 | + items = response.get("Items", []) |
| 435 | + all_items.extend(items) |
| 436 | + |
| 437 | + last_evaluated_key = response.get("LastEvaluatedKey") |
| 438 | + if not last_evaluated_key: |
| 439 | + break |
| 440 | + |
| 441 | + return all_items |
461 | 442 |
|
462 | 443 | @staticmethod |
463 | 444 | def _handle_dynamo_response(response): |
|
0 commit comments