"""Snowflake connector class for the YouTube Red Marketshare tasks.""" from datetime import datetime from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.marketshare_sf.base_executor import \ SnowflakeSQLExecutorMS from feed_ingestion.flows.youtube_red_marketshare import config from feed_ingestion.flows.youtube_red_marketshare import util sql_loader = SQLLoader(__file__) class YouTubeRedMarketshareSF(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.""" return config.feed_name @property def storeid(self): """Storeid of feed data.""" return config.storeid @property def staging_raw_table(self): """Storeid of feed data.""" return config.staging_raw_table def clean_staging_raw_table(self, start_date): """Delete rows from previous unsuccessful workflow run. Args: start_date (str): Date of the data being process (YYYY-MM-DD). """ query_name = 'delete_from_staging_raw' params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=self.staging_raw_table, start_date=start_date) self.execute_query(sql_loader, query_name, params) def load_staging_raw_table(self, staging_raw_red_summary_music, staging_raw_red_label_summary_music, staging_raw_red_label_summary_subscribers, first_day, last_day): """Load the staging raw table. Args: staging_raw_red_summary_music (str): A table name in Snowflake staging_raw_red_label_summary_music (str): A table name in Snowflake staging_raw_red_label_summary_subscribers (str): A table name in Snowflake first_day: The first day of the reporting month. last_day: The last day of the reporting month. """ processeddaytime = datetime.now().replace(microsecond=0) params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_red_summary_music=staging_raw_red_summary_music, staging_raw_red_label_summary_music=staging_raw_red_label_summary_music, # noqa: E501 staging_raw_red_label_summary_subscribers=staging_raw_red_label_summary_subscribers, # noqa: E501 staging_raw_table=self.staging_raw_table, start_date=first_day, end_date=last_day, storeid=self.storeid, ingestion_time=processeddaytime) self.execute_query(sql_loader, 'load_staging_raw', params=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). """ first_day, last_day = util.get_first_last_day(datetime.strptime( date, '%Y-%m-%d')) report_start_date = first_day.strftime('%Y-%m-%d') processeddaytime = datetime.now().replace(microsecond=0) 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, processeddaytime=processeddaytime, storeid=self.storeid, date=report_start_date) self.execute_query(sql_loader, 'load_main_market_share', params=params)