Exemple #1
0
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}
Exemple #2
0
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
Exemple #3
0
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 check_team_resource_goal(resource, team, date):
    """Check the daily goal, award points to the team members if the goal is met.
    Returns the number of players in the team that got the award."""
    count = 0
    goal = get_resource_goal(resource)
    goal, _ = goal.objects.get_or_create(team=team, date=date)

    if goal.actual_usage:
        # if there is already actual_usage in the goal, do nothing
        print "=== %s %s goal already checked." % (date, team.name)
        return 0

    goal_settings = team_goal_settings(team, resource)
    goal.current_goal_percent_reduction = get_goal_percent(
        date, team, resource, goal_settings)

    goal.baseline_usage = team_daily_resource_baseline(date, team, resource)
    goal.goal_usage = goal.baseline_usage * (
        100 - goal.current_goal_percent_reduction) / 100

    resource_data = resource_mgr.team_resource_data(date, team, resource)
    # check if the manual entry time is within the target time,
    # otherwise can not determine the actual usage
    if resource_data and (not goal_settings.manual_entry
                          or goal_settings.manual_entry_time.hour
                          == resource_data.time.hour):
        goal.actual_usage = resource_data.usage
    else:
        goal.actual_usage = 0

    if not goal.actual_usage:
        # if can not determine the actual usage, set the status to unknown
        goal.goal_status = "Unknown"
    elif not goal.baseline_usage:
        # if no baseline, set the status to not available
        goal.goal_status = "Not available"
    else:
        # there are actual and goal usage
        if goal.actual_usage <= goal.goal_usage:
            # if already awarded, do nothing
            if goal.goal_status != "Below the goal":
                goal.goal_status = "Below the goal"

                # record the reduction percentage
                goal.percent_reduction = (goal.goal_usage - goal.actual_usage
                                          ) * 100 / goal.goal_usage

                #adjust the dynamimc goal percent.
                #the current goal percent is 1 percent less from the previous day's goal percent
                # unless the previous goal percent is already 1.
                if goal.current_goal_percent_reduction > 1:
                    goal.current_goal_percent_reduction -= 1

                count = _award_goal_points(team, resource,
                                           goal_settings.goal_points, goal,
                                           date)
        else:
            goal.goal_status = "Over the goal"

    print "=== %s %s actual: %d, goal_usage: %d, reduction: %d, goal: %d" % (
        date, team.name, goal.actual_usage, goal.goal_usage,
        goal.percent_reduction, goal.current_goal_percent_reduction)

    goal.save()

    return count
def check_daily_resource_goal(team, resource):
    """Check the daily goal, award points to the team members if the goal is met.
    Returns the number of players in the team that got the award."""

    # because the check is scheduled at midnight, we should check the previous day's data
    today = datetime.datetime.today()
    if today.hour == 0:
        today = today - datetime.timedelta(hours=1)
    # do nothing if out of round
    rounds_info = challenge_mgr.get_all_round_info()
    if not rounds_info["competition_start"] < today < rounds_info["competition_end"]:
        return 0

    date = today.date()
    actual_usage = None

    resource_data = resource_mgr.team_resource_data(date, team, resource)
    if resource_data:
        goal_settings = team_goal_settings(team, resource)
        goal_usage = team_daily_goal_usage(date, team, resource, goal_settings)

        # check if the manual entry time is within the target time,
        # otherwise can not determine the actual usage
        if not goal_settings.manual_entry or  \
            (goal_settings.manual_entry_time.hour <= resource_data.time.hour and\
             resource_data.time.hour <= (goal_settings.manual_entry_time.hour + 1)):
            actual_usage = resource_data.usage

    count = 0

    goal = _get_resource_goal(resource)
    goal, _ = goal.objects.get_or_create(team=team, date=date)

    if actual_usage:
        if actual_usage <= goal_usage:
            # if already awarded, do nothing
            if goal.goal_status != "Below the goal":
                goal.goal_status = "Below the goal"

                # record the reduction percentage
                goal.percent_reduction = (goal_usage - actual_usage) * 100 / goal_usage

                # adjust the current goal percentage
                _adjust_goal_percent(date, team, resource, goal_settings, goal)

                # Award points to the members of the team.
                goal_points = goal_settings.goal_points
                for profile in team.profile_set.all():
                    if profile.setup_complete:
                        today = datetime.datetime.today()
                        # Hack to get around executing this script at midnight.  We want to award
                        # points earlier to ensure they are within the round they were completed.
                        if today.hour == 0:
                            today = today - datetime.timedelta(hours=1)

                        date = "%d/%d/%d" % (today.month, today.day, today.year)
                        profile.add_points(goal_points, today,
                                           "Team %s Goal for %s" % (resource, date), goal)
                        profile.save()
                        count += 1
        else:
            goal.goal_status = "Over the goal"
    else:
        # if can not determine the actual usage, set the status to unknown
        goal.goal_status = "Unknown"

    goal.save()

    return count
def check_team_resource_goal(resource, team, date):
    """Check the daily goal, award points to the team members if the goal is met.
    Returns the number of players in the team that got the award."""
    count = 0
    goal = get_resource_goal(resource)
    goal, _ = goal.objects.get_or_create(team=team, date=date)

    if goal.actual_usage:
        # if there is already actual_usage in the goal, do nothing
        print "=== %s %s goal already checked." % (date, team.name)
        return 0

    goal_settings = team_goal_settings(team, resource)
    goal.current_goal_percent_reduction = get_goal_percent(date, team, resource, goal_settings)

    goal.baseline_usage = team_daily_resource_baseline(date, team, resource)
    goal.goal_usage = goal.baseline_usage * (100 - goal.current_goal_percent_reduction) / 100

    resource_data = resource_mgr.team_resource_data(date, team, resource)
    # check if the manual entry time is within the target time,
    # otherwise can not determine the actual usage
    if resource_data and (not goal_settings.manual_entry or
        goal_settings.manual_entry_time.hour == resource_data.time.hour):
        goal.actual_usage = resource_data.usage
    else:
        goal.actual_usage = 0

    if not goal.actual_usage:
        # if can not determine the actual usage, set the status to unknown
        goal.goal_status = "Unknown"
    elif not goal.baseline_usage:
        # if no baseline, set the status to not available
        goal.goal_status = "Not available"
    else:
        # there are actual and goal usage
        if goal.actual_usage <= goal.goal_usage:
            # if already awarded, do nothing
            if goal.goal_status != "Below the goal":
                goal.goal_status = "Below the goal"

                # record the reduction percentage
                goal.percent_reduction = (goal.goal_usage -
                                          goal.actual_usage) * 100 / goal.goal_usage

                #adjust the dynamimc goal percent.
                #the current goal percent is 1 percent less from the previous day's goal percent
                # unless the previous goal percent is already 1.
                if goal.current_goal_percent_reduction > 1:
                    goal.current_goal_percent_reduction -= 1

                count = _award_goal_points(team, resource, goal_settings.goal_points, goal, date)
        else:
            goal.goal_status = "Over the goal"

    print "=== %s %s actual: %d, goal_usage: %d, reduction: %d, goal: %d" % (
        date, team.name, goal.actual_usage, goal.goal_usage,
        goal.percent_reduction, goal.current_goal_percent_reduction
    )

    goal.save()

    return count