"""Snowflake connector class for MRC tasks.""" import datetime from snowflake_connector.etl_connector import SQLLoader from snowflake_connector.etl_connector import SnowflakeSQLExecutor import config # Load SQL templates sql_loader = SQLLoader(__file__) looker_sql_loader = SQLLoader(__file__, folder='/looker_queries') class SoundCloudStatsSFExecutor(SnowflakeSQLExecutor): """Helper class to abstract Snowflake operations.""" @property def stats_name(self): """Name of the stats. Should match dir name of this stats, stats_name in config.py of a statistics. Returns: str: Stats name, e.g. 'youtube_statistics'. """ return config.STATS_NAME @property def track_statistics_table(self): """Name of the artist_to_track table for the statistics. Returns: str: {stats}_artist_to_track table name. """ return config.snowflake_table_names['track_statistics'] def create_track_statistics_table(self): """Create artists links table.""" params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=self.track_statistics_table) return self.fetchone_query(sql_loader, 'create_track_statistics', params) def insert_video_statistics_table(self, stats: dict): """First soundcloud statistics insertion.""" params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=self.track_statistics_table, track_title=stats.get('track_title'), track_full_link=stats.get('track_link'), artist=stats.get('artist'), artist_link=stats.get('artist_link'), plays=stats.get('plays'), likes=stats.get('likes'), reposts=stats.get('reposts'), comments_count=stats.get('comments_count'), comments=stats.get('comments'), processing_date=datetime.datetime.now().strftime("%d-%m-%Y") ) return self.fetchall_query(sql_loader, 'insert_track_statistics', params)