"""Snowflake connector class for the FeatureFmFacebook workflow.""" from snowflake_connector.etl_connector import SnowflakeSQLExecutor from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.flows.feature_fm_facebook import config # Load SQL templates sql_loader = SQLLoader(__file__) class FeatureFmFacebook(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 fact_table(self): """Name of the fact table for feed. Returns: str: Fact table name. """ return 'fact_ad_spend' @property def fact_error_table(self): """Name of the fact error table for feed. Returns: str: Fact error table name. """ return 'fact_ad_spend_error' def staging_fact_table(self, date): """Get name of a temp staging fact table. Args: date (str): Date of the data being process (YYYY-MM-DD). Returns: str: Name of a temp staging fact table for a feed. """ return 'staging_fact_ad_spend_{feed_name}_{date}'.format( feed_name=self.feed_name, date=date.replace('-', '')) @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. 'deezer_daily_sf'. """ 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 dim_ad_platform(self): """Name of the dim_ad_platform table for the feed. Returns: str: dim_ad_platform table name. """ return config.snowflake_table_names['dim_ad_platform'] def create_staging_fact_table(self, date): """Create temp staging fact table. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.create_table_like( self.staging_fact_table(date), source_table='FACT_AD_SPEND', source_db=self.sf_config['db'], source_schema=self.sf_config['schema'], transient=True) def load_staging_fact_table(self, date): """Load staging fact table from staging_raw table. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, 'load_staging_fact_ad_spend', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_fact_ad_spend=( self.staging_fact_table(date)), staging_raw_table=self.staging_raw_table, dim_ad_platform=self.dim_ad_platform, date=date)) def delete_from_fact_table(self, date, *args): """Delete rows from fact table for the given date. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, 'delete_from_fact_ad_spend', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=self.fact_table, date=date)) def delete_from_fact_error_table(self, date, *args): """Delete rows from fact error table for the given date. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, 'delete_from_fact_ad_spend_error', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=self.fact_error_table, date=date)) def load_fact_data(self, date): """Load matched data into the fact table. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, 'load_fact_ad_spend', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=self.fact_table, staging_fact_ad_spend=self.staging_fact_table(date), staging_raw_table=self.staging_raw_table, date=date)) def load_fact_error_data(self, date): """Load unmatched data into the fact error table. Args: date (str): Date of the data being process (YYYY-MM-DD). """ self.execute_query( sql_loader, 'load_fact_ad_spend_error', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_error_table=self.fact_error_table, staging_fact_ad_spend=self.staging_fact_table(date), staging_raw_table=self.staging_raw_table, date=date))