"""Snowflake connector class for the Spotify Marketshare tasks.""" from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.marketshare_sf.base_executor import \ SnowflakeSQLExecutorMS from feed_ingestion.flows.spotify_marketshare import config sql_loader = SQLLoader(__file__) class SpotifyMarketshareSF(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. 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 staging_raw_table(self): """Name of the staging_raw table for the feed. Returns: str: staging_raw_{feed} table name. """ return config.snowflake_table_names['staging_raw'] @property def storeid(self): """Storeid of feed data. Should match dim_store and feed config value. Returns: integer: Feed's storeid. """ return 286 def create_temp_staging_raw_table(self, temp_staging_raw_table, **kwargs): """Create a temporary staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. """ sql_loader = SQLLoader(__file__, date=kwargs['download_date']) for report in config.reports: if report in temp_staging_raw_table: sql_template = 'create_{report}_temp_staging_raw'.format( report=report) params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table) self.execute_query(sql_loader, sql_template, params) def load_temp_staging_raw_table( self, temp_staging_raw_table, aws, key_dir, **kwargs): """Load temp staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. aws (dict): [DEPRECATED] AWS credentials to fill a template of COPY SQL statement. key_dir (str): A S3 path to load files from. """ sql_loader = SQLLoader(__file__, date=kwargs['download_date']) for report in config.reports: if report in temp_staging_raw_table: sql_template = 'load_{report}_temp_staging_raw'.format( report=report) aws_params = self.get_aws_params() params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table, s3_path=key_dir, **aws_params, ) self.execute_query(sql_loader, sql_template, params) def clean_staging_raw_table(self, staging_raw_table, date, **kwargs): """Delete rows from previous unsuccessful workflow run. Args: staging_raw_table (str): A table name in Snowflake. date (str): Date of the data being process (YYYY-MM-DD). """ sql_loader = SQLLoader(__file__, date=date) params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=staging_raw_table, date=date) self.execute_query(sql_loader, 'delete_from_staging_raw', params) def load_staging_raw_table( self, temp_staging_raw_table, staging_raw_table, date, **kwargs): """Load the temp_staging_raw data to the feed's staging_raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. staging_raw_table (str): A table name in Snowflake. date (str): Date of the data being process (YYYY-MM-DD). """ sql_loader = SQLLoader(__file__, date=kwargs['download_date']) reports = kwargs['reports'] params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], date=date, storeid=self.storeid, legend_temp_staging_raw_table=reports['legend']['temp_table_name'], revshare_temp_staging_raw_table=reports[ 'revshare']['temp_table_name'], staging_raw_table=staging_raw_table, is_trial=config.product_types['is_trial'], is_adsup=config.product_types['is_adsup'], is_multiuser=config.product_types['is_multiuser'], non_bundle=config.product_types['non_bundle']) self.execute_query(sql_loader, 'load_staging_raw', params) def load_marketshare_data(self, date): """Load data in main_marketshare_table. Args: date (str): Date of the data being process (YYYY-MM-DD). """ sql_loader = SQLLoader(__file__, date=date) 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, storeid=self.storeid, date=date) self.execute_query(sql_loader, 'load_main_market_share', params=params)