Skip to content

Commit 722c7ed

Browse files
committed
Added suggested changes from code review
1 parent 0d200c5 commit 722c7ed

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

scripts/manual_uploads/manual_s3_dynamo_upload.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import boto3
2-
import hashlib
32
import json
43
import os
54
import argparse
5+
import logging
66
from pathlib import Path
77
from typing import Any, Dict, List, Optional, Union, Generator
88
from decimal import Decimal
@@ -21,11 +21,9 @@ def map_dynamo_type(value: Any) -> Dict[str, Any]:
2121
return {"L": [map_dynamo_type(item) for item in value]}
2222
elif isinstance(value, dict):
2323
return {"M": {k: map_dynamo_type(v) for k, v in value.items()}}
24-
elif isinstance(value, Row):
25-
return {"M": {k: map_dynamo_type(v) for k, v in value.asDict().items()}}
2624
else:
2725
logging.warning(f"Unsupported value type: {type(value)}", "Converting it to string")
28-
return {"S": value}
26+
return {"S": str(value)}
2927

3028

3129
def load_json_lines(filepath: Union[str, Path]) -> Generator[Dict[str, Any], None, None]:
@@ -61,16 +59,20 @@ def upload_to_dynamo(
6159
table_name: str,
6260
filepath: Union[str, Path],
6361
) -> None:
64-
62+
uploaded_items = 0
6563
for item in load_json_lines(filepath):
6664
try:
6765
dynamo_client.put_item(
6866
TableName=table_name, Item={key: map_dynamo_type(value) for key, value in item.items()}
6967
)
70-
print(f"Uploaded {filepath} to DynamoDB table {table_name}")
68+
uploaded_items += 1
7169
except Exception as e:
72-
print(f"Failed to upload {filepath}: {e}")
70+
partition_key = item.get("NHS_NUMBER", "Unknown")
71+
sort_key = item.get("ATTRIBUTE_TYPE", "Unknown")
72+
print(f"Failed to upload item (NHS_NUMBER: {partition_key}, ATTRIBUTE_TYPE: {sort_key}) from {filepath}: {e}")
7373

74+
if uploaded_items > 0:
75+
print(f"Uploaded {uploaded_items} items from {filepath} to DynamoDB table {table_name}")
7476

7577
def run_upload(args: Optional[List[str]] = None) -> None:
7678
parser = argparse.ArgumentParser()
@@ -87,6 +89,8 @@ def run_upload(args: Optional[List[str]] = None) -> None:
8789
else:
8890
parsed_args = parser.parse_args(args)
8991

92+
if not parsed_args.upload_s3 and not parsed_args.upload_dynamo:
93+
logging.warning("Neither '--upload-s3' nor '--upload-dynamo' flags specified. No upload actions will be performed.")
9094
if not parsed_args.s3_bucket:
9195
parsed_args.s3_bucket = f"eligibility-signposting-api-{parsed_args.env}-eli-rules"
9296
if not parsed_args.dynamo_table:

tests/utils/manual_s3_dynamo_upload_tests.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import json
2-
32
import boto3
43
import pytest
54
from moto import mock_aws
6-
from path import Path
5+
from pathlib import Path
76

87
from scripts.manual_uploads.manual_s3_dynamo_upload import map_dynamo_type, run_upload
98

0 commit comments

Comments
 (0)