"""Lambda video_dashboard_item_finder function module.""" from owsrequest import request import requests import sentry import config from config import logger from constants import errors from constants import service import s3 from warmer_util import catch_warmer_event def check_dashboard_item_id_file_exists_on_s3(): """Check if the max dashboard item id file exists on S3. Returns: bool: True if file exists, else false. """ bucket = config.VIDEO_DASHBOARD_ITEM_ID_BUCKET key = config.VIDEO_DASHBOARD_ITEM_ID_KEY return s3.object_exists(bucket, key) def read_max_video_dashboard_item_id_from_s3(): """Read the max video dashboard item id from S3. Returns: int: The max video dashboard item id from S3. """ try: bucket = config.VIDEO_DASHBOARD_ITEM_ID_BUCKET key = config.VIDEO_DASHBOARD_ITEM_ID_KEY # read id from file in s3 s3_obj = s3.get_object(bucket, key) response_status_code = s3_obj.get( 'ResponseMetadata').get('HTTPStatusCode') if response_status_code != 200: response_error_code = s3_obj.get('Error').get('Code') response_error_message = s3_obj.get('Error').get('Message') error_text = errors.S3_READ_ERROR_MESSAGE.format( status=response_error_code, text=response_error_message ) raise Exception(error_text) file_data = s3_obj.get('Body').read().decode('utf-8') # check if file data can be converted to int if str.isdigit(file_data) is False: error_text = errors.INVALID_VIDEO_DASHBOARD_ITEM_ID_TYPE raise Exception(error_text) return int(file_data) except Exception as e: logger.exception(str(e)) raise def write_max_video_dashboard_item_id_to_s3(max_video_dashboard_item_id): """Write the max video dashboard item id to a file on S3. Args: max_video_dashboard_item_id (int): Max ID in video dashboard item table. Returns: bool: True if success else raise Exception. """ try: bucket = config.VIDEO_DASHBOARD_ITEM_ID_BUCKET key = config.VIDEO_DASHBOARD_ITEM_ID_KEY # check if id is an int if isinstance(max_video_dashboard_item_id, int) is False: error_text = errors.INVALID_VIDEO_DASHBOARD_ITEM_ID_TYPE raise Exception(error_text) file_data = str(max_video_dashboard_item_id) # write id in a file to s3 s3_obj = s3.put_object(bucket, key, file_data) response_status_code = s3_obj.get( 'ResponseMetadata').get('HTTPStatusCode') if response_status_code != 200: response_error_code = s3_obj.get('Error').get('Code') response_error_message = s3_obj.get('Error').get('Message') error_text = errors.S3_WRITE_ERROR_MESSAGE.format( status=response_error_code, text=response_error_message ) raise Exception(error_text) except Exception as e: logger.exception(str(e)) raise return True def get_new_assets(video_dashboard_item_id): """Call ows-video to get assets for recently added videos. Args: video_dashboard_item_id (int): Max id in video dashboard item table. Returns: list: List of assets of videos with ids greater than the argument id. """ try: response = request.process( application=config.APPLICATION_NAME, environment=config.ENVIRONMENT, method='GET', service_name=service.OWS_VIDEO['name'], path=service.OWS_VIDEO['paths']['get_new_assets'].format( video_dashboard_item_id=video_dashboard_item_id ) ) response.raise_for_status() return response.json() except requests.RequestException as e: logger.exception(str(e)) raise def get_max_dashboard_item_id(): """Call ows-video to get the max dashboard item id. Returns: dict: The max id from the video dashboard item table. """ try: response = request.process( application=config.APPLICATION_NAME, environment=config.ENVIRONMENT, method='GET', service_name=service.OWS_VIDEO['name'], path=service.OWS_VIDEO['paths']['get_max_dashboard_item_id'] ) response.raise_for_status() return response.json() except requests.RequestException as e: logger.exception(str(e)) raise def post_setup_workflow(product_id): """Call ows-video to post the setup workflow. Args: product_id (int): Product id of input video. Returns: bool: True if success else raise Exception. """ try: data = { 'inputs': {}, 'context': { 'product_id': int(product_id) } } response = request.process( application=config.APPLICATION_NAME, environment=config.ENVIRONMENT, method='POST', service_name=service.OWS_VIDEO['name'], path=service.OWS_VIDEO['paths']['post_setup_workflow'], json=data ) response.raise_for_status() return response.json() except requests.RequestException as e: logger.exception(str(e)) raise @catch_warmer_event() def handler(event, context): """Lambda entry point.""" try: # read max dashboard item id from service if previous one is not on S3 if not check_dashboard_item_id_file_exists_on_s3(): max_dashboard_item_id = int(get_max_dashboard_item_id().get( 'max_dashboard_item_id')) write_max_video_dashboard_item_id_to_s3(max_dashboard_item_id) return prev_max_dashboard_item_id = \ read_max_video_dashboard_item_id_from_s3() new_assets = get_new_assets(prev_max_dashboard_item_id) new_assets_list = new_assets.get('items') if not new_assets_list: return for asset in new_assets_list: product_id = asset.get('product_id') # post setup workflow post_setup_workflow(product_id) # write new max id to S3 newest_asset = new_assets.get('items')[-1] new_max_video_dashboard_item_id = newest_asset.get( 'dashboard_item_id') write_max_video_dashboard_item_id_to_s3( new_max_video_dashboard_item_id) except Exception as e: if sentry.sentry_client: sentry.sentry_client.captureException() logger.exception(str(e)) raise e