"""Utils functions related to DynamoDB.""" import datetime import boto3 from activity_detector import base_config def set_status(activity_type_name, date, status, timestamp=None): """Set status for the activity type in DynamoDB. Args: activity_type_name (str): the name of the activity type. status (str): status to set. date (str): date of the interest. timestamp (datetime): optional last procssed time, defaults to now """ if not timestamp: timestamp = datetime.datetime.utcnow().strftime( base_config.STATUS_TIMESTAMP_FORMAT) else: timestamp = timestamp.strftime(base_config.STATUS_TIMESTAMP_FORMAT) client = boto3.client('dynamodb', region_name='us-east-1') client.update_item( TableName=base_config.DYNAMODB_STATUS_TABLE, Key={ 'activity_type_name': {'S': activity_type_name}, 'date': {'S': date}}, ExpressionAttributeNames={'#status': 'status'}, UpdateExpression='SET last_processed_timestamp = :d, #status = :s', ExpressionAttributeValues={ ':d': {'S': timestamp}, ':s': {'S': status}}) def get_status(activity_type_name, date): """Get status for the activity type from DynamoDB. Args: activity_type_name (str): the name of the activity type. date (str): date of the interest. Returns: dict: dictionary of the form: { 'status': 'PROCESSED', 'last_processed_timestamp': '1970-01-01' }. """ client = boto3.client('dynamodb', region_name='us-east-1') response = client.get_item( TableName=base_config.DYNAMODB_STATUS_TABLE, Key={ 'activity_type_name': {'S': activity_type_name}, 'date': {'S': date}}, ExpressionAttributeNames={'#status': 'status'}, ProjectionExpression='#status,last_processed_timestamp') if 'Item' not in response: return None status = response['Item']['status']['S'] timestamp = response['Item']['last_processed_timestamp']['S'] return { 'status': status, 'last_processed_timestamp': datetime.datetime.strptime( timestamp, base_config.STATUS_TIMESTAMP_FORMAT)}