""" Apple podcast sales summary Garcon tasks. Tasks to ingest Apple podcast sales summary data. """ from datetime import datetime from garcon import task from garcon_contrib.dynamo_feed_status import garcon_feed_status from feed_ingestion.flows.apple_podcasts_subscription_snapshot import config from feed_ingestion.flows.apple_podcasts_subscription_snapshot \ .snowflake_executor import ApplePodcastsSubscriptionSnapshotSF from feed_ingestion.flows.helpers import get_sf_config from feed_ingestion.tasks import check_status @task.decorate(timeout=1000) def bootstrap(activity, date, reload, vendor_name, **kwargs): """Bootstrap workflow by injecting initial context from config. Args: activity (ActivityWorker): The Garcon activity worker. date (str): Reporting date (YYYY-MM-DD). reload (str or None): If 'True' delete all feed statuses in DynamoDB. vendor_name (str): Name of the vendor like SME or POD_SUB_LLC. Returns: dict: Initial context for the workflow. """ date_obj = datetime.strptime(date, '%Y-%m-%d') overall_feed_name = config.overall_feed_name activity.logger.info( 'Bootstrap flow: {} for vendor_name: {}'.format(date_obj, vendor_name)) if reload == 'True': activity.logger.info( 'Delete status for feed: {} {} '.format(overall_feed_name, date)) garcon_feed_status.delete_status(overall_feed_name, date) else: overall_status = garcon_feed_status.get_overall_status( overall_feed_name, date) if overall_status == garcon_feed_status.STATUS_INGESTED: activity.logger.info('Already ingested for flow: {}'.format(date)) return {'stop': True} if vendor_name not in [config.sme_vendor, config.pod_sub_llc_vendor]: activity.logger.info( 'Invalid vendor_name: {} for flow: {}'.format(vendor_name, date)) return {'stop': True} expected_file_name = config.report_name.format( vendor_id=config.vendors[vendor_name], date=datetime.strftime(date_obj, '%Y%m%d') ) source_files_dict = {'files': [{'file_name': expected_file_name}]} return { 'date': date_obj.strftime('%Y-%m-%d'), 'feed_name': overall_feed_name, 'expected_file_name': expected_file_name, 'source_files_dict': source_files_dict, 'secrets_path': config.secrets_path, 's3_archive_path': config.s3_archive_path_template, 'staging_raw_table': config.staging_raw_table, 'vendor_id': config.vendors[vendor_name], 'vendor_name': vendor_name } @task.decorate(timeout=1000) @check_status(feed_name=config.overall_feed_name) def load_subscription_snapshot_table( activity, date, staging_raw_table, vendor_id, vendor_name): """Load subscription snapshot table. Also, delete staging raw podcast reviews table. Args: activity (ActivityWorker): The Garcon activity worker. date (str): Reporting date (YYYY-MM-DD). staging_raw_table (str): A staging table name in Snowflake. """ with ApplePodcastsSubscriptionSnapshotSF( get_sf_config(config.secrets_path)) as executor: activity.logger.info('Load subscription snapshot table') executor.load_subscription_snapshot_table( staging_raw_table, date, vendor_id, vendor_name) activity.logger.info('Clear staging raw subscription snapshot table') executor.delete_from_staging_raw_subscription_snapshot( staging_raw_table, date, vendor_id, vendor_name)