""" Spotify Marketshare Workflow. Ingest Spotify marketshare data into marketshare table in Snowflake. """ from garcon.param import StaticParam from garcon_contrib.dynamo_feed_status import garcon_feed_status from feed_ingestion.flows import base from feed_ingestion.flows.base import FlowBase from feed_ingestion.flows.base import FlowConfigMixin from feed_ingestion.flows.base import FlowLoadMarketshareMixinSF from feed_ingestion.flows.base import FlowLoadRawMixinSF from feed_ingestion.flows.spotify_marketshare import config from feed_ingestion.flows.spotify_marketshare import tasks from feed_ingestion.tasks import date_tasks from feed_ingestion.tasks import feed_status_tasks from feed_ingestion.tasks import load_raw_table_tasks_sf from feed_ingestion.tasks import overall_status_tasks from feed_ingestion.tasks import s3_tasks class Flow( FlowBase, FlowConfigMixin, FlowLoadRawMixinSF, FlowLoadMarketshareMixinSF): """Spotify Marketshare Flow class. This flow downloads Spotify marketshare data and loads to a main_market_share. """ def __init__(self): """Initialize an Spotify Marketshare workflow.""" super(Flow, self).__init__(config.feed_name, config.feed_version) def decider(self, schedule): """Orchestrate a Spotify Marketshare workflow. Args: schedule (callable): The scheduler method. """ get_first_day_of_month = schedule( 'get_first_day_of_month', self.get_first_day_of_month) bootstrap = schedule( 'bootstrap', self.bootstrap, requires=[get_first_day_of_month]) check_files_on_s3 = schedule( 'check_files_on_s3', self.check_files_on_s3, requires=[bootstrap]) if check_files_on_s3.result.get('check_files_on_s3.stop'): return fetch_from_drop_location = schedule( 'fetch_from_drop_location', self.fetch_from_drop_location, requires=[check_files_on_s3]) # Stop flow if files aren't available if fetch_from_drop_location.result.get( 'fetch_from_drop_location.stop'): return set_status_to_downloaded = schedule( 'set_status_to_downloaded', self.set_status_to_downloaded, requires=[fetch_from_drop_location]) reset_dynamo_db_status = schedule( 'reset_dynamo_db_status', self.reset_dynamo_db_status, requires=[set_status_to_downloaded]) create_temp_staging_raw_table = schedule( 'create_temp_staging_raw_table', self.create_temp_staging_raw_table, requires=[reset_dynamo_db_status]) load_temp_staging_raw_table = schedule( 'load_temp_staging_raw_table', self.load_temp_staging_raw_table, requires=[create_temp_staging_raw_table]) collect_kwargs_for_load_staging_raw_table = schedule( 'collect_kwargs_for_load_staging_raw_table', self.collect_kwargs_for_load_staging_raw_table, requires=[load_temp_staging_raw_table]) load_staging_raw_table = schedule( 'load_staging_raw_table', self.load_staging_raw_table, requires=[collect_kwargs_for_load_staging_raw_table]) set_status_to_populated_raw_table = schedule( 'set_status_to_populated_raw_table', self.set_status_to_populated_raw_table, requires=[load_staging_raw_table]) drop_temp_staging_table = schedule( 'drop_temp_staging_table', self.drop_temp_staging_table, requires=[set_status_to_populated_raw_table]) load_marketshare_table = schedule( 'load_market_share_table', self.load_marketshare_table, requires=[drop_temp_staging_table]) mark_ingested_files = schedule( 'mark_ingested_files', self.mark_ingested_files, requires=[load_marketshare_table], ) schedule( 'set_status_to_ingested', self.set_status_to_ingested, requires=[mark_ingested_files], ) @property def get_first_day_of_month(self): """Get the first day of the week of date.""" return self.create( name='get_first_day_of_month', tasks=base.SyncRunner( date_tasks.get_first_day_of_month.fill( namespace='get_first_day_of_month', date='context_date'))) @property def bootstrap(self): """Bootstrap initial configuration.""" return self.create( name='bootstrap', tasks=base.SyncRunner( tasks.bootstrap.fill( namespace='bootstrap', date='get_first_day_of_month.date', reload='reload'))) @property def check_files_on_s3(self): """Check if there are some new files in s3_download_path.""" return self.create( name='check_files_on_s3', tasks=base.SyncRunner( tasks.check_files_on_s3.fill( namespace='check_files_on_s3', feed_name='bootstrap.feed_name', date='bootstrap.date', s3_download_path='bootstrap.s3_download_path', file_patterns='bootstrap.file_patterns'))) @property def fetch_from_drop_location(self): """Move file from drop (downloads) folder to archive folder on S3.""" return self.create( name='fetch_from_drop_location', tasks=base.SyncRunner( s3_tasks.copy_files.fill( namespace='fetch_from_drop_location', s3_archive_path='bootstrap.s3_archive_path', s3_download_path='bootstrap.s3_download_path', source_files_dict='check_files_on_s3.source_files_dict', need_all_files=StaticParam(True)))) @property def set_status_to_downloaded(self): """Set overall feed status to DOWNLOADED.""" return self.create( name='set_status_to_downloaded', tasks=base.SyncRunner( overall_status_tasks.set_overall_status.fill( namespace='set_status_to_downloaded', feed_name='bootstrap.feed_name', date='bootstrap.date', status=StaticParam( garcon_feed_status.STATUS_DOWNLOADED)))) @property def create_temp_staging_raw_table(self): """Create temp staging raw table.""" return self.create( name='create_temp_staging_raw_table', generators=[self.temp_staging_tables_generator], tasks=base.SyncRunner( load_raw_table_tasks_sf.create_temp_staging_raw_table.fill( namespace='create_temp_staging_raw_table', date='bootstrap.date', sfdb_params='sfdb_params', feed_name='bootstrap.feed_name', secrets_path='bootstrap.secrets_path', temp_staging_raw_table='temp_staging_raw_table', kwargs='kwargs'))) @property def load_temp_staging_raw_table(self): """Load temp staging raw table.""" return self.create( name='load_temp_staging_raw_table', generators=[self.temp_staging_tables_generator], tasks=base.SyncRunner( load_raw_table_tasks_sf.load_temp_staging_raw_table.fill( namespace='load_temp_staging_raw_table', date='bootstrap.date', sfdb_params='sfdb_params', feed_name='bootstrap.feed_name', secrets_path='bootstrap.secrets_path', kwargs='kwargs', key_dir='key_dir', temp_staging_raw_table='temp_staging_raw_table'))) @property def collect_kwargs_for_load_staging_raw_table(self): """Collect and pass kwargs to load_staging_raw_table activity.""" return self.create_kwargs_collect_activity_for( activity_name='load_staging_raw_table', mapping=dict( reports='bootstrap.reports', download_date='bootstrap.date')) @property def set_status_to_populated_raw_table(self): """Set overall feed status to POPULATED_RAW_TABLE.""" return self.create( name='set_status_to_populated_raw_table', tasks=base.SyncRunner( overall_status_tasks.set_overall_status.fill( namespace='set_status_to_populated_raw_table', feed_name='bootstrap.feed_name', date='bootstrap.date', status=StaticParam( garcon_feed_status.STATUS_POPULATED_RAW_TABLE)))) @property def drop_temp_staging_table(self): """Drop the temporary staging tables.""" return self.create( name='drop_temp_staging_table', generators=[self.temp_staging_tables_generator], tasks=base.SyncRunner( tasks.drop_temp_table.fill( namespace='drop_temp_staging_table', temp_table_name='temp_staging_raw_table'))) @property def mark_ingested_files(self): """Put list of injected files in DynamoDB.""" return self.create( name='mark_ingested_files', tasks=base.SyncRunner( feed_status_tasks.mark_ingested_files.fill( namespace='mark_ingested_files', feed_name='bootstrap.feed_name', date='bootstrap.date', source_files_dict=( 'fetch_from_drop_location.source_files_dict') ))) @property def set_status_to_ingested(self): """Set overall feed status to INGESTED.""" return self.create( name='set_status_to_ingested', tasks=base.SyncRunner( overall_status_tasks.set_overall_status.fill( namespace='set_status_to_ingested', feed_name='bootstrap.feed_name', date='bootstrap.date', status=StaticParam( garcon_feed_status.STATUS_INGESTED, ), ), ), ) def temp_staging_tables_generator(self, context): """Generate parameters for temporary staging tables. Used by the create_temp_staging_raw_table, load_temp_staging_raw_table activity and drop_temp_staging_table. Args: context (dict): The current context. Yields: dict: Dictionary of temporary staging table names. """ reports = context['bootstrap.reports'] s3_archive_path = context['bootstrap.s3_archive_path'] for report, report_data in reports.items(): key_dir = '{s3_archive_path}{file_name}'.format( s3_archive_path=s3_archive_path, file_name=report_data['file_pattern']) yield dict( temp_staging_raw_table=report_data['temp_table_name'], key_dir=key_dir, kwargs=dict( download_date=context['bootstrap.date'] ) )