"""Snowflake connector class for the Amazon Digital Services workflow.""" from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.fact_analytics_sf.base_executor \ import SnowflakeSQLExecutorFA from feed_ingestion.common.staging_raw_sf.base_executor \ import SnowflakeSQLExecutorSR from feed_ingestion.flows.amazon_digital_services import config # Load SQL templates sql_loader = SQLLoader(__file__) class AmazonDSBase(SnowflakeSQLExecutorFA, SnowflakeSQLExecutorSR): """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 licensor(self): """Licensor.""" raise NotImplementedError() @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 '_'.join([config.feed_name, self.licensor]) @property def feedid(self): """Id of the feed. Returns: str: Feed id """ return config.feedid @property def storeid(self): """Storeid of feed data. Should match dim_store and feed config value. Returns: integer: Feed's storeid. """ return 187 @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'][self.licensor] def load_staging_fact_table(self, date): """Load staging fact_analytics table from staging_raw table. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, f'load_staging_fact_{self.licensor}', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_fact_table=self.staging_fact_table(date), staging_raw_table=self.staging_raw_table, reportdate=date, licensor=self.licensor, storeid=self.storeid, feedid=self.feedid)) def load_fact_error_data(self, date): """Load unmatched data into fact_analytics_error. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, f'load_fact_analytics_error_{self.licensor}', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_error_table=self.fact_error_table, staging_fact_table=self.staging_fact_table(date), staging_raw_table=self.staging_raw_table, reportdate=date, storeid=self.storeid, licensor=self.licensor, feedid=self.feedid)) def _delete_from_fact_table(self, table, date, **kwargs): """Delete rows in fact table with the current run date. This is required for the workflow to be idempotent, and to avoid row duplication. Before we'll load rows for a specific day to the fact tables, we have to delete rows which were added by previous (allegedly unsuccessful workflow run). Args: table (str): Either fact_analytics, either fact_analytics_error. date (str): Date of the data being process (YYYY-MM-DD). kwargs (dict): Custom arguments. """ sql_template = sql_loader.load_query('delete_from_fact_table') params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=table, reportdate=date, feedid=self.feedid, licensor=self.licensor, storeid=self.storeid) params.update(kwargs) sql_template, non_identifier_params = ( self.validator.format_identifiers(sql_template, params)) return self.fetchone(sql_template, params=non_identifier_params)[0] class AmazonDSTheOrchard(AmazonDSBase): """Helper class to abstract Snowflake operations.""" @property def licensor(self): """Licensor.""" return 'theorchard' class AmazonDSSme(AmazonDSBase): """Helper class to abstract Snowflake operations.""" @property def licensor(self): """Licensor.""" return 'sme'