"""Metrics logic.""" from oto import response from oto import status from social_analytics.models import artist_social_profile from social_analytics.models import metric def get_latest_metrics_by_artist_id(artist_id): """Get latest metrics by artist id. Args: artist_id (int): The artist id for which to get latest metrics. Returns: response.Response: containing a list of metric dictionaries. """ profiles_response = ( artist_social_profile.get_artist_social_profiles_by_artist_id( artist_id)) if not profiles_response: return profiles_response if profiles_response.status == status.NOT_FOUND: return profiles_response profiles = profiles_response.message['items'] for profile in profiles: metric_response = ( metric.get_latest_metric_by_social_profile_id( profile['social_profile_id'])) if not metric_response: continue if metric_response.status == status.NOT_FOUND: continue metric_value = metric_response.message profile['metric'] = metric_value['metric'] profile['metric_value'] = metric_value['metric_value'] profile['platform'] = metric_value['platform'] return response.Response({'items': profiles}) def get_time_series_data_for_period(time_series_query_params): """Fetch time series data for one metric for a period. Args: time_series_query_params (dict) : a dict containing the following: { 'social_profile_id', 'platform_id', 'metric_id', 'start_date', 'end_date' } where: social_profile_id (int): The social profile id for which to fetch time series data. platform_id (int): The platform id for which to fetch time series data. metric_id (int): The metric id for which to fetch time series data. start_date (datetime): The period start for which to return data. end_date (datetime): The period end for which to return data. Returns: response.Response: containing a list of metric dicts. """ return metric.get_time_series_data_for_period(time_series_query_params)