Esempio n. 1
0
def get_stats(session, tid, week_delta):
    """
    :param week_delta: commonly is 0, mean that you're taking this
        week. -1 is the previous week.
    At the moment do not support negative number and change of the year.
    """
    now = datetime_now()
    week_delta = abs(int(week_delta))

    target_week = datetime_now()
    if week_delta > 0:
        # delta week in the past
        target_week -= timedelta(hours=week_delta * 24 * 7)

    looked_week = target_week.isocalendar()[1]
    looked_year = target_week.isocalendar()[0]

    current_wday = now.weekday()
    current_hour = now.hour
    current_week = now.isocalendar()[1]

    lower_bound = iso_to_gregorian(looked_year, looked_week, 1)
    upper_bound = iso_to_gregorian(looked_year, looked_week + 1, 1)

    hourlyentries = session.query(Stats).filter(Stats.tid == tid,
                                                Stats.start >= lower_bound,
                                                Stats.start <= upper_bound)

    week_entries = 0
    week_map = [[dict() for i in range(24)] for j in range(7)]

    # Loop over the DB stats to fill the appropriate heatmap
    for hourdata in hourlyentries:
        # .weekday() return be 0..6
        stats_day = int(hourdata.start.weekday())
        stats_hour = int(hourdata.start.isoformat()[11:13])

        week_map[stats_day][stats_hour] = {
            'hour': stats_hour,
            'day': stats_day,
            'summary': hourdata.summary,
            'valid': 0  # 0 means valid data
        }

        week_entries += 1

    # if all the hourly element is available
    if week_entries != (7 * 24):
        for day in range(7):
            for hour in range(24):
                if week_map[day][hour]:
                    continue

                # valid is used as status variable.
                # in the case the stats for the hour are missing it
                # assumes the following values:
                #  the hour is lacking from the results: -1
                #  the hour is in the future: -2
                #  the hour is the current hour (in the current day): -3
                if current_week != looked_week:
                    marker = -1
                elif day > current_wday or \
                    (day == current_wday and hour > current_hour):
                    marker = -2
                elif current_wday == day and hour == current_hour:
                    marker = -3
                else:
                    marker = -1

                week_map[day][hour] = {
                    'hour': hour,
                    'day': day,
                    'summary': {},
                    'free_disk_space': 0,
                    'valid': marker
                }

    return {
        'complete': week_entries == (7 * 24),
        'week': datetime_to_ISO8601(target_week),
        'heatmap': weekmap_to_heatmap(week_map)
    }
Esempio n. 2
0
def get_stats(store, week_delta):
    """
    :param week_delta: commonly is 0, mean that you're taking this
        week. -1 is the previous week.
    At the moment do not support negative number and change of the year.
    """

    now = datetime_now()
    week_delta = abs(week_delta)

    if week_delta > 0:
        # delta week in the past
        target_week = utc_past_date(hours=(week_delta * 24 * 7))
    else:
        # taking current time!
        target_week = datetime_now()

    looked_week = target_week.isocalendar()[1]
    looked_year = target_week.isocalendar()[0]

    current_wday = now.weekday()
    current_hour = now.hour
    current_week = now.isocalendar()[1]

    lower_bound = iso_to_gregorian(looked_year, looked_week, 1)
    upper_bound = iso_to_gregorian(looked_year, looked_week, 7)

    hourlyentries = store.find(Stats, And(Stats.start >= lower_bound, Stats.start <= upper_bound))

    week_entries = 0
    week_map = [[dict() for i in xrange(24)] for j in xrange(7)]

    # Loop over the DB stats to fill the appropriate heatmap
    for hourdata in hourlyentries:

        # .weekday() return be 0..6
        stats_day = int(hourdata.start.weekday())
        stats_hour = int(hourdata.start.isoformat()[11:13])

        hourly_dict = {
            'hour': stats_hour,
            'day': stats_day,
            'summary': hourdata.summary,
            'free_disk_space': hourdata.free_disk_space,
            'valid': 0  # 0 means valid data
        }

        if week_map[stats_day][stats_hour]:
            continue

        week_map[stats_day][stats_hour] = hourly_dict
        week_entries += 1

    # if all the hourly element are avail
    if week_entries == (7 * 24):
        return {
            'complete': True,
            'associated_date': datetime_to_ISO8601(target_week),
            'heatmap': weekmap_to_heatmap(week_map)
        }

    # else, supply default for the missing hour.
    # an hour can miss for two reason: the node was down (alarm)
    # or the hour is in the future (just don't display nothing)
    # -- this can be moved in the initialization phases ?
    for day in xrange(7):
        for hour in xrange(24):

            if week_map[day][hour]:
                continue

            # valid is used as status variable.
            # in the case the stats for the hour are missing it
            # assumes the following values:
            #  the hour is lacking from the results: -1
            #  the hour is in the future: -2
            #  the hour is the current hour (in the current day): -3
            if current_week != looked_week:
                marker = -1
            elif day > current_wday or \
                (day == current_wday and hour > current_hour):
                marker = -2
            elif current_wday == day and hour == current_hour:
                marker = -3
            else:
                marker = -1

            week_map[day][hour] = {
                'hour': hour,
                'day': day,
                'summary': {},
                'free_disk_space': 0,
                'valid': marker
            }

    return {
        'complete': False,
        'associated_date': datetime_to_ISO8601(target_week),
        'heatmap': weekmap_to_heatmap(week_map)
    }
Esempio n. 3
0
def get_stats(store, week_delta):
    """
    :param week_delta: commonly is 0, mean that you're taking this
        week. -1 is the previous week.
    At the moment do not support negative number and change of the year.
    """
    now = datetime_now()
    week_delta = abs(week_delta)

    if week_delta > 0:
        # delta week in the past
        target_week = utc_past_date(hours=(week_delta * 24 * 7))
    else:
        # taking current time!
        target_week = datetime_now()

    looked_week = target_week.isocalendar()[1]
    looked_year = target_week.isocalendar()[0]

    current_wday = now.weekday()
    current_hour = now.hour
    current_week = now.isocalendar()[1]

    lower_bound = iso_to_gregorian(looked_year, looked_week, 1)
    upper_bound = iso_to_gregorian(looked_year, looked_week + 1, 1)

    hourlyentries = store.find(
        Stats, And(Stats.start >= lower_bound, Stats.start <= upper_bound))

    week_entries = 0
    week_map = [[dict() for i in xrange(24)] for j in xrange(7)]

    # Loop over the DB stats to fill the appropriate heatmap
    for hourdata in hourlyentries:
        # .weekday() return be 0..6
        stats_day = int(hourdata.start.weekday())
        stats_hour = int(hourdata.start.isoformat()[11:13])

        hourly_dict = {
            'hour': stats_hour,
            'day': stats_day,
            'summary': hourdata.summary,
            'free_disk_space': hourdata.free_disk_space,
            'valid': 0  # 0 means valid data
        }

        if week_map[stats_day][stats_hour]:
            continue

        week_map[stats_day][stats_hour] = hourly_dict
        week_entries += 1

    # if all the hourly element are avail
    if week_entries == (7 * 24):
        return {
            'complete': True,
            'week': datetime_to_ISO8601(target_week),
            'heatmap': weekmap_to_heatmap(week_map)
        }

    # else, supply default for the missing hour.
    # an hour can miss for two reason: the node was down (alarm)
    # or the hour is in the future (just don't display nothing)
    # -- this can be moved in the initialization phases ?
    for day in xrange(7):
        for hour in xrange(24):

            if week_map[day][hour]:
                continue

            # valid is used as status variable.
            # in the case the stats for the hour are missing it
            # assumes the following values:
            #  the hour is lacking from the results: -1
            #  the hour is in the future: -2
            #  the hour is the current hour (in the current day): -3
            if current_week != looked_week:
                marker = -1
            elif day > current_wday or \
                (day == current_wday and hour > current_hour):
                marker = -2
            elif current_wday == day and hour == current_hour:
                marker = -3
            else:
                marker = -1

            week_map[day][hour] = {
                'hour': hour,
                'day': day,
                'summary': {},
                'free_disk_space': 0,
                'valid': marker
            }

    return {
        'complete': False,
        'week': datetime_to_ISO8601(target_week),
        'heatmap': weekmap_to_heatmap(week_map)
    }
Esempio n. 4
0
def get_stats(session, tid, week_delta):
    """
    :param week_delta: commonly is 0, mean that you're taking this
        week. -1 is the previous week.
    At the moment do not support negative number and change of the year.
    """
    now = datetime_now()
    week_delta = abs(int(week_delta))

    target_week = datetime_now()
    if week_delta > 0:
        # delta week in the past
        target_week -= timedelta(hours=week_delta * 24 * 7)

    looked_week = target_week.isocalendar()[1]
    looked_year = target_week.isocalendar()[0]

    current_wday = now.weekday()
    current_hour = now.hour
    current_week = now.isocalendar()[1]

    lower_bound = iso_to_gregorian(looked_year, looked_week, 1)
    upper_bound = iso_to_gregorian(looked_year, looked_week + 1, 1)

    hourlyentries = session.query(Stats).filter(Stats.tid == tid,
                                              Stats.start >= lower_bound,
                                              Stats.start <= upper_bound)

    week_entries = 0
    week_map = [[dict() for i in range(24)] for j in range(7)]

    # Loop over the DB stats to fill the appropriate heatmap
    for hourdata in hourlyentries:
        # .weekday() return be 0..6
        stats_day = int(hourdata.start.weekday())
        stats_hour = int(hourdata.start.isoformat()[11:13])

        week_map[stats_day][stats_hour] = {
            'hour': stats_hour,
            'day': stats_day,
            'summary': hourdata.summary,
            'valid': 0  # 0 means valid data
        }

        week_entries += 1

    # if all the hourly element is available
    if week_entries != (7 * 24):
        for day in range(7):
            for hour in range(24):
                if week_map[day][hour]:
                    continue

                # valid is used as status variable.
                # in the case the stats for the hour are missing it
                # assumes the following values:
                #  the hour is lacking from the results: -1
                #  the hour is in the future: -2
                #  the hour is the current hour (in the current day): -3
                marker = -1
                if current_week != looked_week:
                    pass
                elif day > current_wday or \
                    (day == current_wday and hour > current_hour):
                    marker = -2
                elif current_wday == day and hour == current_hour:
                    marker = -3

                week_map[day][hour] = {
                    'hour': hour,
                    'day': day,
                    'summary': {},
                    'free_disk_space': 0,
                    'valid': marker
                }

    return {
        'complete': week_entries == (7 * 24),
        'week': datetime_to_ISO8601(target_week),
        'heatmap': weekmap_to_heatmap(week_map)
    }