"""Model Sample. This is just a model sample. It depends on which database you want to use but basically make sure this file only contains methods and classes that are related to this model. """ from collections import defaultdict from owsresponse import response from socials.connectors import snowflake SQLLoader = snowflake.SQLLoader(__file__) TIMESERIES_COLUMN_NAMES = [ 'platform', 'followers', 'popularity', 'monthlyListeners', 'views', 'timestamp' ] def get_timeseries(account_url, timestamp_gte, timestamp_lte=None): """Get the timeseries for an account url, given the lower bound timestamp. Args: model_id (int): the id of the model. Returns: response.Response: the data of the model. """ params = { 'account_url': account_url, 'timestamp_gte': timestamp_gte, 'timestamp_lte': timestamp_lte, } end_date_clause = '' if timestamp_lte not in (None, ''): end_date_clause = 'AND snapshot_date <= :timestamp_lte' sql = SQLLoader.load_query('timeseries').format(end_date_clause=end_date_clause) rows = snowflake.fetchall(sql, params) return [{c: v for c, v in zip(TIMESERIES_COLUMN_NAMES, row)} for row in rows] def get_timeseries_by_account_id(account_id, timestamp_gte, timestamp_lte=None): """Get the timeseries for an account_id, given the lower bound timestamp. Args: model_id (int): the id of the model. Returns: response.Response: the data of the model. """ params = { 'account_id': account_id, 'timestamp_gte': timestamp_gte, 'timestamp_lte': timestamp_lte, } end_date_clause = '' if timestamp_lte not in (None, ''): end_date_clause = 'AND snapshot_date <= :timestamp_lte' sql = SQLLoader.load_query('timeseries_by_account_id').format( end_date_clause=end_date_clause) rows = snowflake.fetchall(sql, params) result = defaultdict(dict) for row in rows: (account_url, platform, *data) = row result[account_url]['url'] = account_url result[account_url]['platform'] = platform if result[account_url].get('timeseriesV2') is None: result[account_url]['timeseriesV2'] = [] timeseries = {c: v for c, v in zip(TIMESERIES_COLUMN_NAMES[1:], data)} result[account_url]['timeseriesV2'].append(timeseries) return list(result.values())