"""Lambda function module.""" import base64 import json import logging from aws_kinesis_agg import deaggregator from requests import Session from models import api_exceptions from models import ows_product from models import ows_product_digital from models import ows_track import utils import sentry logger = logging.getLogger() logger.setLevel(logging.INFO) def handler(event, context): """Lambda entry point.""" logger.info(event) count = 0 session = Session() for raw_record in deaggregator.iter_deaggregate_records(event['Records']): count += 1 if count > 375: logger.info('Skipping because # of records > 375.') return record_str = base64.b64decode(raw_record['kinesis']['data']) record_json = json.loads(record_str) record_table = record_json.get('table') record_type = record_json.get('type') record_data = record_json.get('data') record_old_data = record_json.get('old', {}) logger.info(record_str) # Note: label_participant_id being used as flag to help with backfills is_name_change = record_old_data.get('name') or \ record_old_data.get('writer_name') or \ record_old_data.get('artist_name') or \ record_old_data.get('label_participant_id') if record_type != 'insert' and not is_name_change: logger.info('Skipping because record is not an insert event or an update event with a name change.') # noqa continue try: if record_table == 'artist_info': pass elif record_table == 'release_artist': # Exit step if release_artist already has an artist_info_id. if record_data.get('artist_info_id'): logger.info('Skipping because artist_info_id already exists.') # noqa continue name = record_data['artist_name'] release_artist_id = record_data['release_artist_id'] product_id = record_data['release_id'] product_info = ows_product.get_product_info( product_id, session ) vendor_id = product_info['vendor_id'] artist = utils.get_or_create_artist_info_record( name, vendor_id, session) ows_product_digital.update_digital_product_info( { 'product_id': product_id, 'product_artists': [{ 'id': release_artist_id, 'artist_info_id': artist['id'] }] }, session ) logger.info( 'Release artist {} was assigned artist_info id {}'.format( release_artist_id, artist['id'])) elif record_table == 'track_artist' or \ record_table == 'track_writer': # Exit step if track_artist or track_writer already has an artist_info_id # noqa if record_data.get('artist_info_id') and not is_name_change: logger.info('Skipping because artist_info_id already exists and the name is unchanged.') # noqa continue if not record_data.get('name') and not \ record_data.get('writer_name'): logger.info('Skipping because track contributor has blank name.') # noqa continue name = record_data.get('name') or record_data['writer_name'] track_id = record_data.get('unique_track_id') or \ record_data['track_id'] track_contributor_role = record_data.get('type', 'writer') track_contributor_id = record_data.get('id') or \ record_data['track_writer_id'] track_info = ows_track.get_track_info(track_id, session) product_id = track_info['product_id'] product_info = ows_product.get_product_info( product_id, session ) vendor_id = product_info['vendor_id'] artist = utils.get_or_create_artist_info_record( name, vendor_id, session) # Update track contributor's artist_info_id column. ows_track.update_track_contributor( track_id, track_contributor_role, track_contributor_id, { 'artist_info_id': artist['id'] }, session ) logger.info( 'Track {} {} was assigned artist_info id {}'.format( track_contributor_role, track_contributor_id, artist['id'] )) except api_exceptions.IntermittentException as e: if sentry.sentry_client: sentry.sentry_client.captureException() logging.exception('Failing and retrying entire batch because of an intermittent Api error.') # noqa raise e except api_exceptions.PersistentException: if sentry.sentry_client: sentry.sentry_client.captureException() logging.exception('Skipping because of a persistent Api error.') continue