"""Set ingestion state.""" import json from common.connectors.snowfalke_connector import execute_snowflake_query from common.constants.grps_ingestion_status import INGEST_FAILED, \ INGEST_SKIPPED_ALREADY_EXISTS, INGEST_SKIPPED_REPOWNER_DO_NOT_INGEST, \ INGEST_SUCCEEDED from common.constants.sfn_status import FAILURE, SUCCESS from common.models.state_machine.product import Product from common.schemas.state_machine_schema import StateMachineSchema import config from src.constants.queries import UPDATE_GRPS_INGESTER_EXTENDED, \ UPDATE_INGESTION_STATE logger = config.app_logger def handler(event, context): """Lambda function to set ingestion state.""" status = event.get('status', FAILURE) errors = event.get('context', {}).get('errors', {}) if 'detail' in event.get('context') and 'requestParameters' in event.get( 'context').get('detail'): upc = event['context']['detail']['requestParameters']['upc'] grps_ingestion_id = event['context']['detail']['requestParameters'][ 'grps_ingestion_id'] state_machine_data = StateMachineSchema().load({}) state_machine_data.grps_ingestion_id = grps_ingestion_id product = Product(upc=upc) state_machine_data.product = product else: state_machine_data = StateMachineSchema().load(event.get('context')) if status == FAILURE: ingestion_status = INGEST_FAILED elif status == SUCCESS and not errors: ingestion_status = INGEST_SUCCEEDED else: error = errors['Error'] if error == 'ProductAlreadyExistsException': ingestion_status = INGEST_SKIPPED_ALREADY_EXISTS elif error == 'RepOwnerDoNotIngestException': ingestion_status = INGEST_SKIPPED_REPOWNER_DO_NOT_INGEST else: raise Exception('Unexpected SUCCESS status error: ' + error) logger.info(f'Setting ingestion state to {ingestion_status}') if state_machine_data.grps_ingestion_id: grps_ingestion_id = state_machine_data.grps_ingestion_id logger.info( f'Found grps_ingestion_id in state machine data: ' f'{grps_ingestion_id}') else: grps_ingestion_id = event.get('context').get('detail').get( 'requestParameters').get('grps_ingestion_id') logger.info( f'Found grps_ingestion_id context.detail.requestParameters: ' f'{grps_ingestion_id}') execute_snowflake_query(UPDATE_INGESTION_STATE, {'status': ingestion_status, 'grps_ingestion_id': grps_ingestion_id}) isrcs = [] if state_machine_data.tracks: isrcs = [track.isrc for track in state_machine_data.tracks] deleted_tracks = [] if state_machine_data.deleted_tracks: deleted_tracks = state_machine_data.deleted_tracks error = None if errors: error = json.dumps(errors).replace("'", '') logger.info('Updating extended metadata') execute_snowflake_query(UPDATE_GRPS_INGESTER_EXTENDED, { 'grps_ingestion_id': grps_ingestion_id, 'isrcs': isrcs, 'deleted_tracks': deleted_tracks, 'error': error }) return StateMachineSchema().dump(state_machine_data)