"""Snowflake connector. Class for the Apple podcast subscription snapshot monthly tasks. """ from datetime import datetime from snowflake_connector.etl_connector import SnowflakeSQLExecutor from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.flows.itunes import config sql_loader = SQLLoader(__file__) class ApplePodcastsSubsSnapshotMonthlySF(SnowflakeSQLExecutor): """Helper class to abstract Snowflake operations. This class inherits from SnowflakeSQLExecutor class, which provides basic set of methods. This class extends SnowflakeSQLExecutor with some specific methods, which are useful to encapsulate some flow specific operations. """ @property def feed_name(self): """Name of the feed. Should match dir name of this feed, feed_name in config.py of a feed. Returns: str: Feed name """ return config.feed_name @property def feedid(self): """Id of the feed. Returns: str: Feed id """ return config.feedid def load_subscription_snapshot_monthly_table( self, staging_raw_table, date, source, vendor_id, vendor_name): """Populate subscription snapshot monthly table. Args: staging_raw_table (str): A staging table name in Snowflake. date (str): Reporting date (YYYY-MM-DD). source (str): source of the report like Daily, Weekly and Monthly. vendor_id (str): vendor_id. vendor_name (str): Name of the vendor(e.g POD_SUB_LLC, SME). """ processed_daytime = datetime.now().replace(microsecond=0) self.execute_query( sql_loader, 'load_subscription_snapshot_monthly_table', dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=staging_raw_table, processed_daytime=processed_daytime, report_date=date, source=source, vendor_id=vendor_id, vendor_name=vendor_name )) def delete_from_staging_raw_subscription_snapshot_monthly( self, staging_raw_table, date, source, vendor_id): """Delete completed entries from staging raw. Args: staging_raw_table (str): A staging table name in Snowflake. date (str): Reporting date (YYYY-MM-DD). source (str): source of the report like Daily, Weekly and Monthly. vendor_id (str): vendor_id. """ self.execute_query( sql_loader, 'delete_from_staging_raw', dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=staging_raw_table, report_date=date, source=source, vendor_id=vendor_id )) def delete_data_for_old_source( self, from_date, to_date, delete_sources, vendor_id): """Delete entries which are from old source. Args: from_date (str): from_date from which records should be deleted to_date (str): to_date till which records should be deleted delete_sources (tuple): Sources for which records should be deleted vendor_id (str): vendor_id. """ self.execute_query( sql_loader, 'delete_data_for_old_source', dict( db=self.sf_config['db'], schema=self.sf_config['schema'], from_date=from_date, to_date=to_date, delete_sources=delete_sources, vendor_id=vendor_id ))