""" AWA Ingestion Workflow. Ingest data from AWA 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.awa import config, generators from feed_ingestion.flows.awa import tasks from feed_ingestion.tasks import load_raw_table_tasks_sf from feed_ingestion.tasks import overall_status_tasks class Flow(base.FlowLicensor, base.FlowConfigMixin, base.FlowLoadRawMixinSF, base.FlowLoadFactMixinSF): """Class representing the workflow.""" def __init__(self): """Initialize flow object.""" super(Flow, self).__init__(config.feed_name, version='1.0') def decider(self, schedule): """Activity decider. Args: schedule (callable): The scheduler method. """ bootstrap = schedule('bootstrap', self.bootstrap_activity) if bootstrap.result.get('bootstrap.stop'): return # for smej we need to call grab_drop_files_smej licensor = bootstrap.result.get('bootstrap.licensor') if licensor == 'theorchard': grab_drop_files = schedule( 'grab_drop_files', self.grab_drop_files, requires=[bootstrap] ) elif licensor == 'smej': grab_drop_files = schedule( 'grab_drop_files', self.grab_drop_files_smej, requires=[bootstrap] ) else: raise ValueError(licensor) # 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]) mark_staging_raw_table_tasks_complete = schedule( 'mark_staging_raw_table_tasks_complete', self.mark_staging_raw_table_tasks_complete, requires=[load_staging_raw_table]) set_status_populated_raw_table = schedule( 'set_status_populated_raw_table', self.set_status_populated_raw_table, requires=[mark_staging_raw_table_tasks_complete]) if bootstrap.result.get('bootstrap.stop_after_staging_raw'): drop_temp_staging_raw_table_requires = [ set_status_populated_raw_table ] else: create_temp_staging_fact_table = schedule( 'create_temp_staging_fact_table', self.create_temp_staging_fact_table, requires=[set_status_populated_raw_table]) load_temp_staging_fact_table = schedule( 'load_temp_staging_fact_table', self.load_temp_staging_fact_table, requires=[create_temp_staging_fact_table]) load_staging_fact = schedule( 'load_staging_fact', self.load_staging_fact_table, requires=[load_temp_staging_fact_table]) load_fact_table = schedule( 'load_fact_table', self.load_fact_tables, requires=[load_staging_fact]) set_overall_status_ingested = schedule( 'set_overall_status_ingested', self.set_overall_status_ingested, requires=[load_fact_table]) drop_temp_staging_raw_table_requires = [ set_overall_status_ingested] schedule( 'drop_temp_staging_raw_table', self.drop_temp_staging_table, requires=drop_temp_staging_raw_table_requires) @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 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', archive_path='bootstrap.archive_path', source_bucket_name='bootstrap.source_bucket_name', source_file_name='bootstrap.source_file_name'))) @property def grab_drop_files_smej(self): """Grab files and upload to archive bucket on S3.""" return self.create( name='grab_drop_files_sme', tasks=base.SyncRunner( tasks.grab_drop_files_smej.fill( namespace='grab_drop_files', feed_name='bootstrap.feed_name', source_bucket_name='bootstrap.source_bucket_name', source_file_name='bootstrap.source_file_name', date='bootstrap.date', drop_path='bootstrap.drop_path', archive_path='bootstrap.archive_path', clean_path='bootstrap.clean_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 create_temp_staging_raw_table(self): """Create temp staging raw table.""" return self.create( name='create_temp_staging_raw_table', generators=[generators.reports_generator], tasks=base.SyncRunner( load_raw_table_tasks_sf.create_temp_staging_raw_table.fill( namespace='create_temp_staging_raw_table', date='{}.date'.format(self.bootstrap_namespace), sfdb_params='{}.sfdb_params'.format( self.bootstrap_namespace), feed_name='{}.feed_name'.format(self.bootstrap_namespace), secrets_path='{}.secrets_path'.format( self.bootstrap_namespace), kwargs='kwargs', temp_staging_raw_table='temp_staging_raw_table'))) @property def load_temp_staging_raw_table(self): """Load temp staging raw table.""" return self.create( name='load_temp_staging_raw_tables', generators=[generators.reports_generator], tasks=base.SyncRunner( tasks.load_temp_staging_raw_table.fill( namespace='load_temp_staging_raw_tables', feed_name='bootstrap.feed_name', date='bootstrap.date', temp_staging_raw_table='temp_staging_raw_table', s3_dir_path='s3_dir_path', source_files='source_files'))) @property def load_staging_raw_table(self): """Load permanent staging raw table.""" activity_name = 'load_staging_raw_table' return self.create( name=activity_name, generators=[generators.reports_generator], tasks=base.SyncRunner( load_raw_table_tasks_sf.load_staging_raw_table.fill( namespace=activity_name, date='{}.date'.format(self.bootstrap_namespace), feed_name='{}.feed_name'.format(self.bootstrap_namespace), secrets_path='{}.secrets_path'.format( self.bootstrap_namespace), sfdb_params='sfdb_params', temp_staging_raw_table='temp_staging_raw_table', staging_raw_table='staging_raw_table', clean='clean', kwargs='kwargs'), overall_status_tasks.set_overall_status.fill( date='{}.date'.format(self.bootstrap_namespace), feed_name='{}.feed_name'.format(self.bootstrap_namespace), set_status_once=StaticParam(True), status=StaticParam( garcon_feed_status.STATUS_POPULATED_RAW_TABLE)))) @property def mark_staging_raw_table_tasks_complete(self): """Set the complete staging_raw_table_tasks to feed.""" return self.create( name='mark_staging_raw_table_tasks_complete', tasks=base.SyncRunner( (load_raw_table_tasks_sf.mark_staging_raw_table_tasks_complete. fill(namespace='mark_staging_raw_table_tasks_complete', date='bootstrap.date', feed_name='bootstrap.feed_name')))) @property def set_status_populated_raw_table(self): """Set the overall feed status to populated_raw_table.""" return self.create( name='set_status_populated_raw_table', tasks=base.SyncRunner( overall_status_tasks.set_overall_status.fill( namespace='set_status_populated_raw_table', feed_name='bootstrap.feed_name', date='bootstrap.date', status=StaticParam( garcon_feed_status.STATUS_POPULATED_RAW_TABLE)))) @property def create_temp_staging_fact_table(self): """Create and Load staging fact unpivot table.""" return self.create( name='create_temp_staging_fact_table', tasks=base.SyncRunner( tasks.create_temp_staging_fact_table.fill( namespace='create_temp_staging_fact_table', feed_name='bootstrap.feed_name', date='bootstrap.date', report='bootstrap.fact_table_report', kwargs='kwargs'))) @property def load_temp_staging_fact_table(self): """Create and Load staging fact unpivot table.""" return self.create( name='load_temp_staging_fact_table', tasks=base.SyncRunner( tasks.load_temp_staging_fact_table.fill( namespace='load_temp_staging_fact_table', feed_name='bootstrap.feed_name', date='bootstrap.date', report='bootstrap.fact_table_report' ))) @property def drop_temp_staging_table(self): """Load staging raw table.""" return self.create( name='drop_temp_staging_raw_table', generators=[generators.reports_generator], schedule_to_start=48000, tasks=base.SyncRunner( load_raw_table_tasks_sf.drop_temp_staging_raw_table.fill( namespace='drop_temp_staging_table', temp_staging_raw_table='temp_staging_raw_table', feed_name='bootstrap.feed_name', secrets_path='bootstrap.secrets_path', sfdb_params='bootstrap.sfdb_params'))) @property def set_overall_status_ingested(self): """Set overall feed status INGESTED if all reports are completed.""" return self.create( name='set_overall_status_ingested', tasks=base.SyncRunner( overall_status_tasks.set_overall_status.fill( namespace='set_overall_status_ingested', date='bootstrap.date', feed_name='bootstrap.feed_name', status=StaticParam( garcon_feed_status.STATUS_INGESTED))))