"""Snowflake connector class for Amazon Unlimited tasks.""" from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.marketshare_sf.base_executor import \ SnowflakeSQLExecutorMS from feed_ingestion.flows.amazon_prime_marketshare import config # Load SQL templates sql_loader = SQLLoader(__file__) class AmazonPrimeMKTSFExecutor(SnowflakeSQLExecutorMS): """Helper class to abstract Snowflake 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, and a key of executors dict in feed_ingestion/common/ fact_analytics_sf/__init__.py file. Returns: str: Feed name, e.g. 'amazon_unlimited'. """ 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 187 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['date_for_sqlloader']) 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, 'create_temp_staging_raw', params) def load_temp_staging_raw_table( self, temp_staging_raw_table, aws, key_dir): """Load temp staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. aws (dict): AWS credentials to fill a template of COPY SQL statement. key_dir (str): A S3 path to load files from. """ 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, 'load_temp_staging_raw', 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). """ 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): """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=date) params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table, staging_raw_table=staging_raw_table, date=date, storeid=self.storeid, currency_db=config.snowflake_table_names['currency_db'], currency_schema=config.snowflake_table_names['currency_schema']) self.execute_query(sql_loader, 'load_staging_raw', params) def load_marketshare_data(self, date): """Load data into main_market_share. Args: date (str): Date of the data being process (YYYY-MM-DD). """ 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)