""" VEVO Ingestion Workflow. Ingest data from VEVO feed. Files downloaded from theorchard s3. """ 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.vevo import config from feed_ingestion.flows.vevo import tasks from feed_ingestion.tasks import overall_status_tasks, \ validate_raw_data_tasks_sf class Flow( base.FlowLicensor, base.FlowConfigMixin, base.FlowLoadRawMixinSF, base.FlowYouTubeMixin): """Class representing the workflow.""" def __init__(self): """Initialize flow object.""" super(Flow, self).__init__(config.feed_name, version='1.0') self.GRAB_DROP_FILES = { 'sme': self.grab_drop_files_sme, 'theorchard': self.grab_drop_files } def decider(self, schedule): """Activity decider. Args: schedule (callable): The scheduler method. """ check_feed_status = schedule( 'check_feed_status', self.check_feed_status) if check_feed_status.result.get('check_feed_status.stop'): return bootstrap = schedule( 'bootstrap', self.bootstrap_activity, requires=[check_feed_status]) grab_drop_files = schedule( 'grab_drop_files', self.GRAB_DROP_FILES[bootstrap.result.get('bootstrap.licensor')], requires=[bootstrap] ) # if files unavailable, let's stop here if grab_drop_files.result.get('grab_drop_files.stop'): return set_status_to_downloaded = schedule( 'set_status_to_downloaded', self.set_status_to_downloaded, requires=[grab_drop_files]) create_temp_staging_raw_table = schedule( 'create_temp_staging_raw_table', self.create_temp_staging_raw_table, requires=[set_status_to_downloaded]) load_temp_staging_raw_table = schedule( 'load_temp_staging_raw_table', self.load_temp_staging_raw_table, requires=[create_temp_staging_raw_table]) load_staging_raw_table = schedule( 'load_staging_raw_table', self.load_staging_raw_table, requires=[load_temp_staging_raw_table]) update_channel_names_table = schedule( 'update_channel_names_table', self.update_channel_names_table, requires=[load_staging_raw_table]) schedule( 'set_status_to_ingested', self.set_status_to_ingested, requires=[update_channel_names_table]) @property def check_feed_status(self): """Check and reset feed status if it is needed.""" return self.create( name='check_feed_status', tasks=base.SyncRunner( overall_status_tasks.check_feed_status.fill( namespace='check_feed_status', date='context_date', feed_name=StaticParam(config.feed_name), licensor='licensor', reload='reload'))) @property def bootstrap_activity(self): """First activity for the workflow.""" return self.create( name='bootstrap', tasks=base.SyncRunner( tasks.bootstrap.fill( namespace='bootstrap', date='context_date', licensor='licensor', reload='reload'))) @property def grab_drop_files(self): """Grab The Orchard files and upload to archive bucket on S3.""" return self.create( name='grab_drop_files', tasks=base.SyncRunner( tasks.grab_drop_files.fill( namespace='grab_drop_files', feed_name='bootstrap.feed_name', date='bootstrap.date', drop_path='bootstrap.drop_path', filename_prefix='bootstrap.filename_theorchard_prefix', archive_path='bootstrap.archive_path'))) @property def grab_drop_files_sme(self): """Grab SME files and upload to archive bucket on S3.""" return self.create( name='grab_drop_files_sme', tasks=base.SyncRunner( tasks.grab_drop_files_sme.fill( namespace='grab_drop_files', feed_name='bootstrap.feed_name', date='bootstrap.date', drop_path='bootstrap.drop_path', filename='bootstrap.filename_sme', archive_path='bootstrap.archive_path'))) @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 load_temp_staging_raw_table(self): """Load temp staging raw table.""" return self.create( name='load_temp_staging_raw_table', tasks=base.SyncRunner( validate_raw_data_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=StaticParam(config.secrets_path), key_dir='grab_drop_files.s3_drop_path', temp_staging_raw_table='bootstrap.temp_staging_raw_table', error_limit=StaticParam(config.snowflake_error_limit)))) @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))))