|
8 | 8 | from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table |
9 | 9 | from botocore.config import Config |
10 | 10 | from .mappings import vaccine_type_mappings, VaccineTypes |
11 | | -from functools import lru_cache |
12 | 11 | from .constants import valid_nhs_number1 |
13 | 12 |
|
14 | 13 | current_directory = os.path.dirname(os.path.realpath(__file__)) |
@@ -138,31 +137,32 @@ def get_full_row_from_identifier(identifier: str) -> dict: |
138 | 137 | return table.get_item(Key={"PK": f"Immunization#{identifier}"}).get("Item") |
139 | 138 |
|
140 | 139 |
|
141 | | -@lru_cache() |
142 | 140 | def get_dynamodb_table() -> Table: |
143 | 141 | config = Config(connect_timeout=2, read_timeout=2, retries={"max_attempts": 1}) |
144 | 142 | db: DynamoDBServiceResource = boto3.resource("dynamodb", region_name="eu-west-2", config=config) |
145 | 143 | return db.Table(os.getenv("DYNAMODB_TABLE_NAME")) |
146 | 144 |
|
147 | 145 |
|
148 | 146 | def delete_imms_records(identifiers: list[str]) -> dict: |
149 | | - """Batch delete immunization records from the DynamoDB table.""" |
| 147 | + """Batch delete immunization records from the DynamoDB table. |
| 148 | + Returns counts of successful and failed deletions. |
| 149 | + """ |
150 | 150 | table = get_dynamodb_table() |
151 | | - deleted = [] |
152 | | - errors = [] |
| 151 | + success_count = 0 |
| 152 | + failure_count = 0 |
153 | 153 |
|
154 | 154 | try: |
155 | 155 | with table.batch_writer(overwrite_by_pkeys=["PK"]) as batch: |
156 | 156 | for identifier in identifiers: |
157 | 157 | key = {"PK": f"Immunization#{identifier}"} |
158 | 158 | try: |
159 | 159 | batch.delete_item(Key=key) |
160 | | - deleted.append(identifier) |
| 160 | + success_count += 1 |
161 | 161 | except Exception as e: |
162 | 162 | print(f"Failed to delete record with key {key}: {e}") |
163 | | - errors.append({"identifier": identifier, "error": str(e)}) |
| 163 | + failure_count += 1 |
164 | 164 | except Exception as e: |
165 | 165 | print(f"Batch writer failed: {e}") |
166 | | - return {"Error": str(e), "Deleted": deleted, "Failures": errors} |
| 166 | + return {"success_count": success_count, "failure_count": len(identifiers)} |
167 | 167 |
|
168 | | - return {"Deleted": deleted, "Failures": errors} |
| 168 | + return {"success_count": success_count, "failure_count": failure_count} |
0 commit comments