"""Lambda function module.""" import uuid from ddex_ingester_common.constants.status import (IN_CONTENT, TRANSFER_TO_CONTENT) from ddex_ingester_common.helpers.asset import copy_asset from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.release_correction.release_correction_diffs import \ ReleaseCorrectionDiffDetail from ddex_ingester_common.release_correction.release_corrections_s3 import ( create_blank_rc_json, write_rc_json) from ddex_ingester_common.schemas.state_machine_schema import ( StateMachineSchema, VideoAssetSchema) import config from config import graphql_gateway from constants import queries from constants.handle_video_asset import RC_JSON_NAME logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Lambda entrypoint.""" state_machine_data = StateMachineSchema().load(event.get('context')) correlation_id = state_machine_data.correlation_id or str(uuid.uuid4()) state_machine_data.correlation_id = correlation_id logging_utils.update_logger_correlation_id(logger, correlation_id) logging_utils.update_logger_with_message_ids( logger, state_machine_data.message_id, state_machine_data.message_thread_id, state_machine_data.execution_name ) logger.info(f'Triggered ddex-ingester-handle-video-asset: {event}') asset = VideoAssetSchema().load(event.get('asset')) logger.info(f'Asset source bucket: {asset.bucket}') logger.info(f'Asset key: {asset.key}') # copy video asset to target bucket for ows-video logger.info(f'Target bucket: {config.VIDEO_ASSET_BUCKET}') if state_machine_data.product.status in [IN_CONTENT, TRANSFER_TO_CONTENT]: update_diff_details = [] logger.info('Found difference for Video assets.') update_diff_details.append(ReleaseCorrectionDiffDetail( field_name='videoAsset', isrc=None, old=None, new='Video Assets have been updated')) event_details = { 'bucket': state_machine_data.bucket, 'key': state_machine_data.key } s3_release_corrections = {'changes': update_diff_details} create_blank_rc_json(event_details, RC_JSON_NAME) write_rc_json(event_details, s3_release_corrections, RC_JSON_NAME) else: graphql_gateway.set_headers( { 'Orchard-User-Id': config.OA_USER, 'Correlation-Id': correlation_id, } ) target_path =\ f'{config.VIDEO_ASSET_INGEST_DIR}/incoming/{asset.filename}' logger.info(f'Target path: {target_path}') copy_asset( {}, asset, config.VIDEO_ASSET_BUCKET, target_path) # trigger video ingest workflow logger.info('Trigger ows-video ingest workflow') response = create_video_workflow( target_path, state_machine_data.product.product_id) workflow_id = response['id'] attach_workflow_to_video_product( workflow_id, state_machine_data.product.product_id) state_machine_data.video.workflow_id = workflow_id ingest_custom_thumbnail(state_machine_data.product, workflow_id) return { 'context': StateMachineSchema().dump(state_machine_data), 'asset': VideoAssetSchema().dump(asset) } def create_video_workflow(target_path, product_id): """Create video workflow.""" return graphql_gateway.execute( queries.create_video_ingest_workflow, { 'data': { 'type': 'INGEST_FROM_S3', 'path': target_path, 'context': { 'productId': product_id } } } )['data']['createVideoIngestWorkflow'] def attach_workflow_to_video_product(workflow_id, product_id): """Attach workflow ID to video product.""" graphql_gateway.execute( queries.save_video_single_product, { 'data': { 'update': { 'productId': str(product_id), 'latestPipelineRunId': workflow_id, } } } ) def ingest_custom_thumbnail(product, workflow_id): """Ingest the artwork image if there is one.""" logger.info('Ingest custom thumbnail') if not product.artwork: return artwork = product.artwork # we should have and artwork object that was deserialised from JSON like: # "artwork": { # "filename": "A10301A0003654009M_T-1000615622791_Image_001-001.jpg", # "bucket": "qa-ddex-ingester", # "key": "sme_ddex/A10301A0003/resources/A10301_Image_001-001.jpg", # "ows_assets_filename": null, # "filepath": "resources/" # } upload_token = get_thumbnail_token(artwork.filename, workflow_id) target_bucket = upload_token['bucket'] target_key = upload_token['filename'] logger.info( f'Going to copy from {artwork.bucket}, key: {artwork.filename} ' f'to target {target_bucket}, key: {target_key}' ) copy_asset( {}, artwork, target_bucket, target_key ) logger.info('Done copying custom thumbnail') update_video_with_thumbnail(product.product_id, target_key) def get_thumbnail_token(filename, workflow_id): """Get an upload token to upload a custom thumbnail.""" logger.info(f'Get token {filename} workflow id: {workflow_id}') upload_token = graphql_gateway.execute( queries.get_custom_thumbnail_token, { 'data': { 'filename': filename, 'workflowJobId': workflow_id } } )['data']['createVideoCustomThumbnailToken'] logger.info(f'Got upload token for thumbn: {upload_token}') return upload_token def update_video_with_thumbnail(product_id, target_key): """Update the video metadata with the custom thumbnail path.""" logger.info(f'Going to update_video_with_thumbnail key: {target_key}') customThumbnailPath = target_key.replace( 'thumbnails/custom/', '') logger.info(f'About to set customThumbnailPath to {customThumbnailPath}') graphql_gateway.execute( queries.save_video_single_product, { 'data': { 'update': { 'productId': str(product_id), 'customThumbnailPath': customThumbnailPath } } } ) logger.info('Done setting custom thumbnail on video')