def group_resource_ranks(name, round_name=None): """Return the ranking of resource use for all teams.""" cache_key = "group_%s_ranks-%s" % (name, slugify(round_name)) ranks = cache_mgr.get_cache(cache_key) if ranks is None: resource_usage = _get_resource_usage(name) resource_setting = get_resource_setting(name) rate = resource_setting.conversion_rate if resource_setting.winning_order == "Ascending": ordering = "total" else: ordering = "-total" start, end = challenge_mgr.get_round_start_end(round_name) usage_ranks = resource_usage.objects.filter( date__lte=end, date__gte=start).values("team__group__name").annotate( total=Sum("usage")).order_by(ordering) ranks = [] for rank in usage_ranks: ranks.append({ "team__group__name": rank["team__group__name"], "total": utils.format_usage(rank["total"], rate) }) cache_mgr.set_cache(cache_key, ranks, 600) return ranks
def group_resource_ranks(name, round_name=None): """Return the ranking of resource use for all teams.""" cache_key = "group_%s_ranks-%s" % (name, slugify(round_name)) ranks = cache_mgr.get_cache(cache_key) if ranks is None: resource_usage = _get_resource_usage(name) resource_setting = get_resource_setting(name) rate = resource_setting.conversion_rate if resource_setting.winning_order == "Ascending": ordering = "total" else: ordering = "-total" start_end = challenge_mgr.get_round_start_end(round_name) if start_end is not None: start, end = start_end else: return None usage_ranks = resource_usage.objects.filter( date__lte=end, date__gte=start).values("team__group__name").annotate( total=Sum("usage")).order_by(ordering) ranks = [] for rank in usage_ranks: ranks.append({"team__group__name": rank["team__group__name"], "total": utils.format_usage(rank["total"], rate)}) cache_mgr.set_cache(cache_key, ranks, 600) return ranks
def resource_goal_ranks(resource, round_name=None): """Generate the scoreboard for resource goals.""" if not challenge_mgr.is_game_enabled("%s Game" % resource.capitalize()): return None cache_key = "%s_goal_ranks-%s" % (resource, slugify(round_name)) goal_ranks = cache_mgr.get_cache(cache_key) if goal_ranks is None: goal_ranks = [] goal = get_resource_goal(resource) start_end = challenge_mgr.get_round_start_end(round_name) if start_end is not None: start, end = start_end else: return None ranks = goal.objects.filter( goal_status="Below the goal", date__gte=start, date__lte=end).values("team__name").annotate( completions=Count("team"), average_reduction=Avg("percent_reduction")).order_by( "-completions", "-average_reduction") for rank in ranks: goal_ranks.append(rank) total_count = Team.objects.count() if len(goal_ranks) != total_count: for t in Team.objects.all(): # find the team in the goal_ranks count = 0 for goal_rank in goal_ranks: if t.name == goal_rank["team__name"]: break else: count += 1 if count == len(goal_ranks): # not found rank = { "team__name": t.name, "completions": 0, "average_reduction": 0 } goal_ranks.append(rank) if len(goal_ranks) == total_count: break cache_mgr.set_cache(cache_key, goal_ranks, 3600 * 24) return goal_ranks
def resource_goal_ranks(resource, round_name=None): """Generate the scoreboard for resource goals.""" if not challenge_mgr.is_game_enabled("%s Game" % resource.capitalize()): return None cache_key = "%s_goal_ranks-%s" % (resource, slugify(round_name)) goal_ranks = cache_mgr.get_cache(cache_key) if goal_ranks is None: goal_ranks = [] goal = get_resource_goal(resource) start_end = challenge_mgr.get_round_start_end(round_name) if start_end is not None: start, end = start_end else: return None ranks = goal.objects.filter( goal_status="Below the goal", date__gte=start, date__lte=end).values("team__name").annotate( completions=Count("team"), average_reduction=Avg("percent_reduction")).order_by( "-completions", "-average_reduction") for rank in ranks: goal_ranks.append(rank) total_count = Team.objects.count() if len(goal_ranks) != total_count: for t in Team.objects.all(): # find the team in the goal_ranks count = 0 for goal_rank in goal_ranks: if t.name == goal_rank["team__name"]: break else: count += 1 if count == len(goal_ranks): # not found rank = {"team__name": t.name, "completions": 0, "average_reduction": 0} goal_ranks.append(rank) if len(goal_ranks) == total_count: break cache_mgr.set_cache(cache_key, goal_ranks, 3600 * 24) return goal_ranks