def top_crashes(top=10, start="", end="", *args): start, end = parse_start_end(start, end) top = int(top) if "force" in args or not r.exists(top_crashes_key(start, end)): r.delete(top_crashes_key(start, end)) with timer(): map_data(_top_crashes, start, end, start=datetime.strptime(start, "%Y%m%d"), end=datetime.strptime(end, "%Y%m%d")) with timer(): for i in sorted(r.hgetall(top_crashes_key(start, end)).items(), key=lambda x: int(x[1]), reverse=True)[:top]: print i
def crash_counts(self, start="", end=""): if not (isinstance(start, datetime) and isinstance(end, datetime)): start, end = parse_start_end(start, end) start, end = datetime(int(start[:4]), int(start[4:6]), int(start[6:8])), datetime(int(end[:4]), int(end[4:6]), int(end[6:8])) start, end = int(buckify(self.hours, start)), int(buckify(self.hours, end)) crashes = self.items() t = [] crash_counts = [] for time, count in crashes: time = int(time) if time >= start and time <= end: t.append(time) crash_counts.append(int(count)) return t, crash_counts
def crashes_time_series(type="global", start="", end="", hours=24, *args): start, end = parse_start_end(start, end) hours = int(hours) cc = CrashCounts(type, hours) t, counts = cc.crash_counts(start, end) elementary_stats(counts) plt.title("{} crashes from {} to {} (bin={} hours)".format(type, start, end, hours)) plt.xlabel("Time") plt.ylabel("Crashes") plt.xlim(min(t), max(t)) plt.ylim(0, max(counts) * 1.2) plt.grid(True) plt.plot(t, counts) plt.show()