11import boto3
2- import hashlib
32import json
43import os
54import argparse
5+ import logging
66from pathlib import Path
77from typing import Any , Dict , List , Optional , Union , Generator
88from 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
3129def 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
7577def 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 :
0 commit comments