"""Snowflake connector class for the Spotify's charts tasks.""" from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.staging_raw_sf.base_executor \ import SnowflakeSQLExecutorSR from feed_ingestion.flows.spotify_charts import config from feed_ingestion.util.snowflake.errors import JSONParserLoading sql_loader = SQLLoader(__file__) class SpotifyCharts(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 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 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 def create_temp_staging_raw_table(self, temp_staging_raw_table): """Create a temporary staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. """ 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, **kwargs): """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. [DEPRECATED] 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'], table_name=temp_staging_raw_table, s3_path=key_dir, on_error_action='SKIP_FILE_{}'.format( config.snowflake_error_limit), **aws_params ) return [JSONParserLoading(*e) for e in self.fetchall_query( sql_loader, 'load_temp_staging_raw', params)] def load_staging_raw_table( self, temp_staging_raw_table, staging_raw_table, date, **kwargs): """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). """ params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], download_date=date, temp_table_name=temp_staging_raw_table, chart_type=kwargs['chart_type'], country=kwargs['country'], frequency=kwargs['frequency'], staging_raw_table=staging_raw_table) self.execute_query(sql_loader, 'load_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, chart_type=kwargs['chart_type'], frequency=kwargs['frequency'], countries=kwargs['countries']) self.execute_query(sql_loader, 'delete_from_staging_raw', params) def load_spotify_tables( self, date, chart_type, frequency, table_name, countries): """Load the temp_staging_raw data to the feed's staging_raw table. Args: date (str): Date of the data being process (YYYY-MM-DD). chart_type (str): The type of a chart (regional, viral). frequency (str): The frequency of a chart (weekly, daily). table_name (str): The name of the table to load. countries (str): List of countries to load. """ params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], date=date, chart_type=chart_type, frequency=frequency, staging_raw_table=self.staging_raw_table, countries=countries) self.execute_query( sql_loader, f'load_{table_name}', params)