"""Snowflake connector class for the Apple podcast sales summary monthly.""" 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 ApplePodcastsSalesSummaryMonthlySF(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_sales_summary_monthly_table( self, staging_raw_table, date, source, vendor_id, vendor_name): """Populate sales summary monthly table. Args: staging_raw_table_dict (dict): A table name in Snowflake. date (str): Date of the data being process (YYYY-MM-DD). source (str): source of the report like Daily, Weekly and Monthly. vendor_id (str): Vendor ID. vendor_name (str): Vendor Name (e.g SME, POD_SUB_LLC). """ processed_daytime = datetime.now().replace(microsecond=0) self.execute_query( sql_loader, 'load_sales_summary_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_sales_summary_monthly( self, staging_raw_table, date, source, vendor_id): """Delete completed entries from staging raw. Args: staging_raw_table_dict (dict): A table name in Snowflake. date (str): Date of the data being process (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 ))