def get_hourly_goal_data(team, resource): """:return: the energy goal data for the user's team.""" date = datetime.datetime.today() if resource_mgr.is_blackout(date): return {"is_blackout": True} data = resource_mgr.team_resource_data(date=date.date(), team=team, resource=resource) if data: goal_settings = resource_goal.team_goal_settings(team, resource) goal_percentage = goal_settings.goal_percent_reduction baseline = resource_goal.team_hourly_resource_baseline(date, team, resource) if goal_settings.baseline_method == "Dynamic": # get previous day's goal result and the current goal percent previous_goal_result = resource_goal.team_goal(date - datetime.timedelta(days=1), team, resource) if previous_goal_result and previous_goal_result.current_goal_percent_reduction: goal_percentage = previous_goal_result.current_goal_percent_reduction goal = {"goal_usage": (baseline * 100 - baseline * goal_percentage) / 100, "warning_usage": (baseline * 100 - baseline * goal_percentage / 2) / 100, "actual_usage": data.usage, "updated_at": datetime.datetime.combine(date=data.date, time=data.time) } goal["actual_diff"] = abs(goal["actual_usage"] - goal["goal_usage"]) return goal else: return {"actual_usage": None}
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 get_daily_goal_data(team, resource): """:return: the daily energy goal data.""" round_info = challenge_mgr.get_round_info() if not round_info: return None start = round_info["start"].date() end = round_info["end"].date() delta = (end - start).days + 1 data_table = [] for day in range(0, delta): date = start + datetime.timedelta(days=day) goal_info = {"date": date} if day == 0: # cal and store the filler_days in the first day goal_info goal_info["filler_days"] = range(0, date.weekday()) if day == (delta - 1): # cal and store the filler_days in the last day goal_info goal_info["filler_days"] = range(0, 6 - date.weekday()) goal = resource_goal.team_goal(date, team, resource) goal_settings = resource_goal.team_goal_settings(team, resource) unit = resource_mgr.get_resource_setting(resource).unit goal_usage = resource_goal.team_daily_goal_usage(date, team, resource, goal_settings) goal_info["goal_info"] = "%d %s" % (goal_usage, unit) if goal: goal_info["goal_status"] = goal.goal_status goal_info["verbose_info"] = "%d %s used within the last 24 hours (ends at %s). " \ "The goal is %d %s." % ( resource_mgr.team_resource_usage(date, team, resource), unit, goal_settings.manual_entry_time, goal_usage, unit ) data_table.append(goal_info) return data_table