"""Snowflake connector class for the TIKTOK FRAUDULENT \ STREAMS REPORT 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.tiktok_fraudulent_streams_report import config from feed_ingestion.util.snowflake.errors import JSONParserLoading # Load SQL templates sql_loader = SQLLoader(__file__) class TiktokFraudulentStreamsReport(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, and a key of executors dict in feed_ingestion/common/ fact_analytics_sf/__init__.py file. Returns: str: Feed name, e.g. 'tiktok_fraudulent_streams_report'. """ return config.feed_name @property def staging_raw_table(self): """Name of staging raw table. Returns: str: staging_raw table name """ return config.staging_raw_table def create_temp_staging_raw_table(self, temp_staging_raw_table, date): """Create a temporary staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. kwargs (dict): Custom arguments. """ query_name = 'create_temp_staging_raw_table' params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=temp_staging_raw_table, date=date ) 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(1), table_name=temp_staging_raw_table, **aws_params ) return [JSONParserLoading(*e) for e in self.fetchall_query( sql_loader, 'load_temp_staging_raw_table', params)] def load_staging_raw_table( self, temp_staging_raw_table, staging_raw_table, date, **kwargs): """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. 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) self.execute_query(sql_loader, 'load_staging_raw_table', params) def clean_staging_raw_table_(self, date, staging_raw_table, **kwargs): """Delete data from staging raw table for specific date. Args: staging_raw_table (str): Table name to be cleaned date (str): Date of the data being process (YYYY-MM-DD). """ self.clean_staging_raw_table( db=self.sf_config['db'], schema=self.sf_config['schema'], date=date, staging_raw_table=staging_raw_table, ) 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.drop_table( table_name, db=self.sf_config['db'], schema=self.sf_config['schema'] )