def get_hourly_goal_data(team, resource): """:return: the energy goal data for the user's team.""" hourly_goal = cache_mgr.get_cache("hgoal-%s-%d" % (resource, team.id)) if hourly_goal is None: date = datetime.datetime.today() hourly_goal = {"resource": resource} if resource_mgr.is_blackout(date): hourly_goal.update({"is_blackout": True}) else: resource_setting = resource_mgr.get_resource_setting(resource) unit = resource_setting.unit rate = resource_setting.conversion_rate usage_data = resource_mgr.team_resource_data(date=date.date(), team=team, resource=resource) if usage_data: actual_usage = utils.format_usage(usage_data.usage, rate) goal_settings = resource_goal.team_goal_settings( team, resource) goal_percent = resource_goal.get_goal_percent( date, team, resource, goal_settings) baseline = resource_goal.team_hourly_resource_baseline( resource, team, usage_data.date, usage_data.time) goal_usage = utils.format_usage( baseline * (100 - goal_percent) / 100, rate) warning_usage = utils.format_usage( baseline * (100 - goal_percent / 2) / 100, rate) actual_diff = abs(actual_usage - goal_usage) hourly_goal.update({ "goal_usage": goal_usage, "warning_usage": warning_usage, "actual_usage": actual_usage, "actual_diff": actual_diff, "updated_at": datetime.datetime.combine(date=usage_data.date, time=usage_data.time), "unit": unit, }) else: hourly_goal.update({"no_data": True}) cache_mgr.set_cache("hgoal-%s-%d" % (resource, team.id), hourly_goal, 600) return hourly_goal
def _set_goal_info(goal_info, resource, team, date): """set the goal info.""" resource_setting = resource_mgr.get_resource_setting(resource) unit = resource_setting.unit rate = resource_setting.conversion_rate goal_settings = resource_goal.team_goal_settings(team, resource) goal = resource_goal.team_goal(date, team, resource) goal_percent = resource_goal.get_goal_percent(date, team, resource, goal_settings) if goal: goal_info["goal_status"] = goal.goal_status if goal.actual_usage: goal_info["goal_info"] = "usage:<br/>%d %s" % (utils.format_usage( goal.actual_usage, rate), unit) elif goal.goal_usage: goal_info["goal_info"] = "goal:%d %s<br/>(reduce %d%%)" % ( utils.format_usage(goal.goal_usage, rate), unit, goal_percent) else: goal_info["goal_status"] = "Unknown" if goal_info["goal_status"] == "Not available": goal_info["verbose_info"] = "Game disabled for today because baseline data " \ "not available." elif goal_info["goal_status"] == "Unknown": if date == datetime.date.today(): # if there is baseline, display the expected goal, # otherwise, display disabled with no baseline, as unavailable baseline = resource_goal.team_daily_resource_baseline( date, team, resource) if baseline: goal_usage = baseline * (100 - goal_percent) / 100 goal_info["goal_status"] = None goal_info["goal_info"] = "goal:%d %s<br/>(reduce %d%%)" % ( utils.format_usage(goal_usage, rate), unit, goal_percent) else: goal_info["goal_status"] = "Not available" goal_info["verbose_info"] = "Game disabled for today because " \ "baseline data not available." else: goal_info["goal_status"] = "Not available" goal_info["verbose_info"] = "Game disabled for today because usage data " \ "not available." else: goal_info["verbose_info"] = "%d %s used within the last 24 hours (ends at %s). " \ "<br/>The goal is %d %s (reduce %d%%)." % ( utils.format_usage(goal.actual_usage, rate), unit, goal_settings.manual_entry_time if goal_settings.manual_entry else "midnight", utils.format_usage(goal.goal_usage, rate), unit, goal_percent )
def _set_goal_info(goal_info, resource, team, date): """set the goal info.""" resource_setting = resource_mgr.get_resource_setting(resource) unit = resource_setting.unit rate = resource_setting.conversion_rate goal_settings = resource_goal.team_goal_settings(team, resource) goal = resource_goal.team_goal(date, team, resource) goal_percent = resource_goal.get_goal_percent(date, team, resource, goal_settings) if goal: goal_info["goal_status"] = goal.goal_status if goal.actual_usage: goal_info["goal_info"] = "usage:<br/>%d %s" % ( utils.format_usage(goal.actual_usage, rate), unit) elif goal.goal_usage: goal_info["goal_info"] = "goal:%d %s<br/>(reduce %d%%)" % ( utils.format_usage(goal.goal_usage, rate), unit, goal_percent) else: goal_info["goal_status"] = "Unknown" if goal_info["goal_status"] == "Not available": goal_info["verbose_info"] = "Game disabled for today because baseline data " \ "not available." elif goal_info["goal_status"] == "Unknown": if date == datetime.date.today(): # if there is baseline, display the expected goal, # otherwise, display disabled with no baseline, as unavailable baseline = resource_goal.team_daily_resource_baseline( date, team, resource) if baseline: goal_usage = baseline * (100 - goal_percent) / 100 goal_info["goal_status"] = None goal_info["goal_info"] = "goal:%d %s<br/>(reduce %d%%)" % ( utils.format_usage(goal_usage, rate), unit, goal_percent) else: goal_info["goal_status"] = "Not available" goal_info["verbose_info"] = "Game disabled for today because " \ "baseline data not available." else: goal_info["goal_status"] = "Not available" goal_info["verbose_info"] = "Game disabled for today because usage data " \ "not available." else: goal_info["verbose_info"] = "%d %s used within the last 24 hours (ends at %s). " \ "<br/>The goal is %d %s (reduce %d%%)." % ( utils.format_usage(goal.actual_usage, rate), unit, goal_settings.manual_entry_time if goal_settings.manual_entry else "midnight", utils.format_usage(goal.goal_usage, rate), unit, goal_percent )
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_ranks(name, round_name=None): """Return the ranking of resource use for all teams.""" cache_key = "%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" round_info = challenge_mgr.get_round_info(round_name) if not round_info: return None all_rounds_info = challenge_mgr.get_all_round_info() usage_ranks = resource_usage.objects.filter( date__lte=round_info["end"].date, date__gte=all_rounds_info["competition_start"]).values("team__name").annotate( total=Sum("usage")).order_by(ordering) ranks = [] for rank in usage_ranks: ranks.append({"team__name": rank["team__name"], "total": utils.format_usage(rank["total"], rate)}) cache_mgr.set_cache(cache_key, ranks, 600) return ranks
def get_hourly_goal_data(team, resource): """:return: the energy goal data for the user's team.""" hourly_goal = cache_mgr.get_cache("hgoal-%s-%d" % (resource, team.id)) if hourly_goal is None: date = datetime.datetime.today() hourly_goal = {"resource": resource} if resource_mgr.is_blackout(date): hourly_goal.update({"is_blackout": True}) else: resource_setting = resource_mgr.get_resource_setting(resource) unit = resource_setting.unit rate = resource_setting.conversion_rate usage_data = resource_mgr.team_resource_data(date=date.date(), team=team, resource=resource) if usage_data: actual_usage = utils.format_usage(usage_data.usage, rate) goal_settings = resource_goal.team_goal_settings(team, resource) goal_percent = resource_goal.get_goal_percent(date, team, resource, goal_settings) baseline = resource_goal.team_hourly_resource_baseline( resource, team, usage_data.date, usage_data.time) goal_usage = utils.format_usage(baseline * (100 - goal_percent) / 100, rate) warning_usage = utils.format_usage(baseline * (100 - goal_percent / 2) / 100, rate) actual_diff = abs(actual_usage - goal_usage) hourly_goal.update({"goal_usage": goal_usage, "warning_usage": warning_usage, "actual_usage": actual_usage, "actual_diff": actual_diff, "updated_at": datetime.datetime.combine(date=usage_data.date, time=usage_data.time), "unit": unit, }) else: hourly_goal.update({"no_data": True}) cache_mgr.set_cache("hgoal-%s-%d" % (resource, team.id), hourly_goal, 600) return hourly_goal
def _createData(date_list, resource, today): """Creates the datatable to be used.""" resource_goals = [] rate = resource_mgr.get_resource_setting(resource).conversion_rate rgoal = resource_goal.get_resource_goal(resource) goals = rgoal.objects.filter( date__lte=today - datetime.timedelta(days=1), date__gte=today - datetime.timedelta(days=(len(date_list) - 1))).order_by( 'team', '-date').select_related('team') data = None for goal in goals: if data: if data["name"] != goal.team: # encounter a new team, store the old team data resource_goals.append(data) # init the new team structure data = _init_data(goal.team, resource, today) else: data = _init_data(goal.team, resource, today) goal.goal_usage = utils.format_usage(goal.goal_usage, rate) if goal.actual_usage: goal.actual_usage = utils.format_usage(goal.actual_usage, rate) goal.net_usage = goal.goal_usage - goal.actual_usage else: goal.net_usage = 'N/A' data["vals"].append((goal.date, goal)) # need to store the last team data resource_goals.append(data) return resource_goals