"""Snowflake connector class for the iTunes Marketshare tasks.""" from datetime import datetime from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.marketshare_sf.base_executor import \ SnowflakeSQLExecutorMS from feed_ingestion.flows.itunes_marketshare import config sql_loader = SQLLoader(__file__) class ITunesMarketshareSF(SnowflakeSQLExecutorMS): """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.""" return config.feed_name @property def storeid(self): """Storeid of feed data.""" return config.storeid @property def staging_raw_table(self): """Name of staging_raw table of the feed.""" return config.snowflake_table_names['staging_raw'] def load_marketshare_data(self, date): """Load data into main_market_share. Args: date (str): Date of the data being process (YYYY-MM-DD). """ processeddaytime = datetime.now().replace(microsecond=0) sql_template = 'load_main_market_share' if date >= '2023-08-01': sql_template = f'2023-08-01/{sql_template}' date = datetime.strptime(date, '%Y-%m-%d').strftime('%m%y') params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], main_market_share_table=self.main_marketshare_table, staging_raw_table=self.staging_raw_table, processeddaytime=processeddaytime, storeid=self.storeid, currency_db=config.snowflake_table_names['currency_db'], currency_schema=config.snowflake_table_names['currency_schema'], date=date) self.execute_query(sql_loader, sql_template, params=params)