"""Snowflake connector class for the Youtube Weekly tasks.""" from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.fact_analytics_sf.base_executor \ import SnowflakeSQLExecutorFA from feed_ingestion.flows.youtube_weekly import config from feed_ingestion.util import youtube_util sql_loader = SQLLoader(__file__) class YoutubeWeekly(SnowflakeSQLExecutorFA): """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 feedid(self): """Id of the feed. Returns: int: Feed id """ return config.feedid @property def staging_raw_table(self): """Name of the staging_raw table for the feed. Returns: str: staging_raw_{feed} table name. """ return config.staging_raw_table_name @property def storeid(self): """Storeid of feed data. Should match dim_store and feed config value. Returns: integer: Feed's storeid. """ return config.storeid 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). """ _, start_date, end_date = youtube_util.get_days(date) self.execute_query( sql_loader, 'load_staging_fact', 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, from_date=start_date, to_date=end_date, 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). """ _, start_date, end_date = youtube_util.get_days(date) self.execute_query( sql_loader, 'load_fact_analytics_error', 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, from_date=start_date, to_date=end_date, storeid=self.storeid, feedid=self.feedid)) def load_fact_data(self, date, **kwargs): """Load matched data into fact_analytics. Args: date (str): Date of the data being process (YYYY-MM-DD). kwargs (dict): Custom keyword arguments. """ _, start_date, end_date = youtube_util.get_days(date) self.execute_query( sql_loader, 'load_fact_analytics', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=self.fact_table, staging_fact_analytics_table=self.staging_fact_table(date), staging_raw_table=self.staging_raw_table, from_date=start_date, to_date=end_date))