"""Methods for retrieving Ingestion Feed status from DynamoDB.""" import boto3 from boto3.dynamodb.conditions import Key from feed_status import config dynamodb = boto3.resource('dynamodb', region_name=config.AWS_REGION) FEED_INGESTION_TABLE = dynamodb.Table(config.FEED_INGESTION_TABLE) def get_ingestion_history(feed_name, from_date, to_date): """Look up feed status in DynamoDB. Args: feed_name (str): feed name of interest. from_date (date): start date of interest. to_date (date): end date of interest. Returns: list: list of statuses. """ response = FEED_INGESTION_TABLE.query( KeyConditionExpression=Key('feed_name').eq(feed_name) & Key('date').between(from_date, to_date) ) return response.get('Items') def get_ingestion_comment(feed_id, date): """Look up feed status comment in DynamoDB. Args: date (str): date of interest. feed_id (str): feed of interest Returns: dict: comment item. """ response = FEED_INGESTION_TABLE.get_item( Key={ 'feed_name': config.COMMENTS_FEED, 'date': '{}.{}'.format(date, feed_id) }, AttributesToGet=['date', 'comment']) item = response.get('Item') if not item: return False try: comment_date, feed_name = item['date'].split('.') except ValueError: return False comment = { 'feed_name': feed_name, 'date': comment_date, 'comment': item['comment'] } return comment def save_ingestion_comment(feed_id, date, comment): """Save feed status comment in DynamoDB. Args: date (str): date of interest. feed_id (str): feed of interest Returns: dict: comment item. """ response = FEED_INGESTION_TABLE.update_item( Key={ 'feed_name': config.COMMENTS_FEED, 'date': '{}.{}'.format(date, feed_id), }, AttributeUpdates={'comment': { 'Value': comment }}, ReturnValues='ALL_NEW') item = response.get('Attributes') if not item: return False try: comment_date, feed_name = item['date'].split('.') except ValueError: return False data = { 'feed_name': feed_name, 'date': comment_date, 'comment': item['comment'] } return data def delete_ingestion_comment(feed_id, date): """Save feed status comment in DynamoDB. Args: date (str): date of interest. feed_id (str): feed of interest Returns: dict: item delete confirmation. """ response = FEED_INGESTION_TABLE.delete_item(Key={ 'feed_name': config.COMMENTS_FEED, 'date': '{}.{}'.format(date, feed_id), }, ) if response['ResponseMetadata']['HTTPStatusCode'] != 200: return False return {'details': 'item was successfully deleted'} def get_ingestion_comment_list(date): """Get all feed status comments for the period. Args: date (str): start date of interest Returns: list: list of comments. """ response = FEED_INGESTION_TABLE.query( KeyConditionExpression=Key('feed_name').eq('__comments__') & Key('date').gt(date) ) items = response.get('Items') comments = {} for item in items: try: comment_date, feed_name = item['date'].split('.') except ValueError: continue if feed_name not in comments.keys(): comments[feed_name] = {comment_date: item['comment']} else: comments[feed_name][comment_date] = item['comment'] return comments