def test_utc_past_date(self): a = utility.datetime_now() b = utility.utc_past_date(seconds=99) c = utility.utc_past_date(minutes=99) d = utility.utc_past_date(hours=99) self.assertTrue(a > b) self.assertTrue(b > c) self.assertTrue(c > d)
def test_utc_past_date(self): a = utility.datetime_now() b = utility.utc_past_date(seconds=99) c = utility.utc_past_date(minutes=99) d = utility.utc_past_date(hours=99) self.assertTrue(a>b) self.assertTrue(b>c) self.assertTrue(c>d)
def do_statspollute(dbfile): # source gl_database = create_database("sqlite:%s" % dbfile) source_store = Store(gl_database) stats = source_store.find(models.Stats) counter = 0 for s in stats: source_store.remove(s) counter += 1 print "removed %d entry in stats" % counter counter = 0 # 21 days in the past for past_hours in xrange(24 * 7 * 3): past_hours += 4 when = utc_past_date(hours=past_hours) newstat = models.Stats() newstat.freemb = randint(1000, 1050) newstat.year = when.isocalendar()[0] newstat.week = when.isocalendar()[1] level = round((randint(0, 1000) / 240.0), 1) - 2 def random_pollution(): return int(randint(0,11) + (5 * level)) activity_fake = { 'successfull_logins': random_pollution(), 'failed_logins': random_pollution(), 'started_submissions': random_pollution(), 'completed_submissions': random_pollution(), 'uploaded_files': int(randint(0,11) + (5 * level)), 'appended_files': random_pollution(), 'wb_comments': random_pollution(), 'wb_messages': random_pollution(), 'receiver_comments': random_pollution(), 'receiver_messages': random_pollution() } for k, v in activity_fake.iteritems(): if v < 0: activity_fake[k] = 0 newstat.start = when newstat.summary = activity_fake counter += 1 source_store.add(newstat) print "Committing %d stats" % counter source_store.commit()
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) }
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) }