"""Generic helpers for feed_sender's workflows.""" import boto3 from feed_sender.conf import config DYNAMODB_TABLE = config.FEED_SENDER_TABLE STATUS_NOT_AVAILABLE = 'NOT_AVAILABLE' STATUS_PROCESSING = 'PROCESSING' STATUS_UPLOADED_TO_FTP = 'UPLOADED_TO_FTP' STATUS_UPLOADED_TO_S3 = 'UPLOADED_TO_S3' STATUS_SENT = 'SENT' def set_status(feed_name, date, status=STATUS_NOT_AVAILABLE): """Set status of the ingestion job. Args: feed_name (str): Name of the feed. date (str): Reporting date of the ingestion job. status (str): Status code constant. """ client = boto3.client('dynamodb') client.update_item( TableName=DYNAMODB_TABLE, Key={'feed_name': {'S': feed_name}, 'date': {'S': date}}, ExpressionAttributeNames={'#status': 'status'}, ExpressionAttributeValues={':status': {'S': status}}, UpdateExpression='SET #status = :status' ) def get_status(feed_name, date): """Get status of the ingestion job. Args: feed_name (str): Name of the feed. date (str): Reporting date of the ingestion job. Returns: mixed (False | status code): False if item not found. Status code if item is found. """ client = boto3.client('dynamodb') response = client.get_item( Key={'feed_name': {'S': feed_name}, 'date': {'S': date}}, TableName=DYNAMODB_TABLE, ) if not response or 'Item' not in response: return False if not response['Item'] or 'status' not in response['Item']: return False return response['Item']['status']['S'] def delete_status(feed_name, date): """Delete item from DynamoDB. Args: feed_name (str): Name fo the feed. date (str): Ingestion date. """ client = boto3.client('dynamodb') client.delete_item( Key={'feed_name': {'S': feed_name}, 'date': {'S': date}}, TableName=DYNAMODB_TABLE )