"""NetEase Data Ingestion Workflow.""" from datetime import datetime import os from garcon import task from garcon_contrib.dynamo_feed_status import garcon_feed_status from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.flows.netease import config from feed_ingestion.tasks import bootstrap as reload, check_status, s3_tasks # Load SQL templates sql_loader = SQLLoader(__file__) @task.decorate(timeout=1000) @reload.reset_dynamodb_status_on_reload(config.feed_name) def bootstrap(activity, date, dw_config=None): """Bootstrap workflow by getting the correct configurations. Args: activity (ActivityWorker): The activity worker. date (str): Reporting date (YYYY-MM-DD). Returns: dict: Context. """ date_obj = (datetime.strptime( date, '%Y-%m-%d') if date else datetime.today()) activity.logger.info('Bootstrap flow: {}'.format(date_obj)) archive_path = config.archive_path.format(date=date_obj) source_file_name = config.drop_filename.format(date=date_obj) source_key_name = '{}{}'.format(config.drop_path, source_file_name) destination_key_name = '{}{}'.format(archive_path, source_file_name) temp_staging_raw_table = config.temp_staging_raw_table.format( date.replace('-', '_')) s3_archive_uri = 's3://{}/{}{}'.format( config.data_bucket, archive_path, source_file_name) return dict( feed_name=config.feed_name, date=date_obj.strftime('%Y-%m-%d'), source_key_name=source_key_name, destination_key_name=destination_key_name, key_dir=s3_archive_uri, secrets_path=config.secrets_path, staging_raw_table=config.staging_raw_table, temp_staging_raw_table=temp_staging_raw_table ) @task.decorate(timeout=3600) @check_status() def grab_drop_files( activity, feed_name, date, source_key_name, destination_key_name): """Copy a feed files from the drop location into the archive folder. Args: activity (ActivityWorker): The activity worker. feed_name (str): The name of the feed. date (str): Reporting date (YYYY-MM-DD). source_key_name (str): The s3 key of the raw file. destination_key_name (str): s3 path to drop raw file. """ result = s3_tasks.copy_file_from_sme_s3_to_theocrhard( activity, secrets_path=None, source_bucket_name=config.drop_bucket, source_key_name=source_key_name, destination_bucket_name=config.data_bucket, destination_key_name=destination_key_name, replace=True) # file is not downloaded if not result.get(os.path.basename(source_key_name)): garcon_feed_status.set_overall_status( feed_name, date, garcon_feed_status.STATUS_NOT_AVAILABLE) return {'stop': True}