def get_global_stats_by_rank(self, session, query_filter: QueryFilterBuilder, stats_query, stds_query, player_rank=None, redis=None): """ Returns the global stats based on the rank of a player. Does modify the query_filter only setting rank. :param session: Session :param query_filter: a query filter. :param stats_query: A list of global stats :param stds_query: A list of global stats for standard deviations :param player_rank: The player that stats are associated with. Uses unranked if rank is not found :param redis: The local cache :return: """ rank_index = get_rank_number(player_rank) if redis is not None: stat_string = redis.get('global_stats') if stat_string is not None: stats_dict = json.loads(stat_string) global_stats = [stats_dict[s.field_name][rank_index]['mean'] for s in self.field_names] global_stds = [stats_dict[s.field_name][rank_index]['std'] for s in self.field_names] return global_stats, global_stds if player_rank is not None and not get_local_dev(): logger.debug('Filtering by rank') query_filter.clean().with_rank(rank_index) else: query_filter.clean() return (query_filter.with_stat_query(stats_query).build_query(session).first(), query_filter.with_stat_query(stds_query).build_query(session).first())
def get_global_stats_by_rank(self, session, query_filter: QueryFilterBuilder, stats_query, stds_query, player_rank=None, redis=None, ids=None, playlist=13): """ Returns the global stats based on the rank of a player. Does modify the query_filter only setting rank. :param session: Session :param query_filter: a query filter. :param stats_query: A list of global stats :param stds_query: A list of global stats for standard deviations :param player_rank: The player that stats are associated with. Uses unranked if rank is not found :param redis: The local cache :return: """ if ids is None: # Set the correct rank index if player_rank is not None: if isinstance(player_rank, dict) or isinstance(player_rank, list): rank_index = get_rank_tier(player_rank, playlist=playlist) else: rank_index = player_rank else: rank_index = 0 stat_list = self.get_player_stat_list() # Check to see if we have redis available (it usually is) if redis is not None: stat_string = redis.get('global_stats_by_rank') # Check to see if the key exists and if so load it if stat_string is not None: stats_dict = json.loads(stat_string) if playlist is not None: playlist = str(playlist) if playlist not in stats_dict: raise UnsupportedPlaylist(404, 'Playlist does not exist in global stats') stats_dict = stats_dict[playlist] global_stats = [] global_stds = [] for stat in stat_list: if stat.get_field_name() in stats_dict: global_stats.append(stats_dict[stat.get_field_name()][rank_index]['mean']) global_stds.append(stats_dict[stat.get_field_name()][rank_index]['std']) else: global_stats.append(1) global_stds.append(1) return global_stats, global_stds if is_local_dev(): rank_index = 0 stats = self.get_global_stats(session, with_rank=False) global_stats = [stats[stat.get_field_name()][rank_index]['mean'] for stat in stat_list] global_stds = [stats[stat.get_field_name()][rank_index]['std'] for stat in stat_list] return global_stats, global_stds raise CalculatedError(500, "Global stats unavailable or have not been calculated yet.") else: query_filter.clean().with_replay_ids(ids) return (query_filter.with_stat_query(stats_query).build_query(session).first(), query_filter.with_stat_query(stds_query).build_query(session).first())