"""Snowflake connector class for the LINE 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.line import config from feed_ingestion.util.snowflake.errors import JSONParserLoading # Load SQL templates sql_loader = SQLLoader(__file__) class LINE(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 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 staging_raw_table(self): """Name of staging raw table. Returns: str: staging_raw table name """ return config.fa_staging_raw_table @property def feedid(self): """ID of the feed. Returns: int: Feed id """ return config.feedid @property def storeid(self): """ID of the store. Returns: int: Store id """ return config.storeid def create_temp_staging_raw_table(self, temp_staging_raw_table, **kwargs): """Create a temporary staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. kwargs (dict): Custom arguments with report type. """ report_type = kwargs['report_type'] query_name = 'create_temp_staging_raw_{}_table'.format(report_type) params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table, **kwargs ) self.execute_query(sql_loader, query_name, 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. key_dir (str): Custom arguments. """ aws_params = self.get_aws_params() params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table, s3_path=key_dir, on_error_action='SKIP_FILE_{}'.format(kwargs['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, report, date, licensor): """Load staging raw table with activity, user, and playlist files. Args: temp_staging_raw_table (str): A table name in Snowflake. staging_raw_table (str): Name of staging raw table. report (str): Name of report. date (str): Date in YYYY-MM-DD format. kwargs (dict): Custom arguments with temporary table names and filenames for user, playlist and activity. """ params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=staging_raw_table, temp_staging_raw_table=temp_staging_raw_table, download_date=date, licensor=licensor, ) sql = 'load_staging_raw_{}'.format(report) self.execute_query(sql_loader, sql, params) def load_staging_fact_table(self, date, licensor): """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, '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, feedid=self.feedid, storeid=self.storeid, download_date=date, licensor=licensor)) def clean_staging_raw_table(self, date, table_name, licensor): """Delete data from staging raw table for specific date. Args: date (str): Date of the data being process (YYYY-MM-DD). table_name (str): Table name to be cleaned licensor (str): Licensor name """ self.execute_query( sql_loader, 'clean_staging_raw_table', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=table_name, download_date=date, licensor=licensor)) def drop_temp_staging_raw_table(self, table_name): """Delete temp staging raw table. Args: table_name (str): Name of table to be deleted. """ self.execute_query( sql_loader, 'delete_tmp_table', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=table_name)) def delete_from_fact_table(self, date, **kwargs): """Delete rows from fact_analytics table for the given date. Args: date (str): Date of the data being process (YYYY-MM-DD). """ query_name = 'delete_from_fact_table' self.execute_query( sql_loader, query_name, params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=self.fact_table, date=date, staging_raw_table=self.staging_raw_table, storeid=self.storeid, reportdate=date, feedid=self.feedid, licensor=self.licensor)) def delete_from_fact_error_table(self, date, **kwargs): """Delete rows from fact_analytics_error table for the given date. Args: date (str): Date of the data being process (YYYY-MM-DD). """ query_name = 'delete_from_fact_table' self.execute_query( sql_loader, query_name, params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], fact_table=self.fact_error_table, date=date, staging_raw_table=self.staging_raw_table, storeid=self.storeid, reportdate=date, feedid=self.feedid, licensor=self.licensor)) def load_fact_error_data(self, date, licensor): """Load unmatched data into fact_analytics_error. Args: date (str): Date of the data being process (YYYY-MM-DD). """ 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, feedid=self.feedid, storeid=self.storeid, licensor=licensor)) class LineTheOrchard(LINE): """Helper class to abstract Snowflake operations.""" @property def licensor(self): """Licensor.""" return 'theorchard' class LineSMEJ(LINE): """Helper class to abstract Snowflake operations.""" @property def licensor(self): """Licensor.""" return 'smej'