#!/usr/bin/env python # Count messages per month in a mailbox and produce a gnuplot graph. # Adam Sampson import sys, rfc822, time, os, math PERIOD_LENGTH = 60 * 60 * 24 def readposts(times): while 1: while 1: l = sys.stdin.readline() if l == "": return if l.startswith("From "): break while 1: l = sys.stdin.readline() if l == "": return if l.lower().startswith("date: "): date = l[6:].strip() dt = rfc822.parsedate(date) if dt is None: print "failed to parse", date else: times.append(time.mktime(dt)) if l == "\n": break def period_of(t): return int(t / PERIOD_LENGTH) * PERIOD_LENGTH def main(): times = [] readposts(times) times.sort() periods = {} for i in xrange(times[0], times[-1] + PERIOD_LENGTH, PERIOD_LENGTH): periods[period_of(i)] = [] for t in times: periods[period_of(t)].append(t) ps = periods.keys() ps.sort() # Discard first and last data points, since they'll be incomplete. ps = ps[1:-2] fn = "/tmp/mboxcount.data" fmt = "%Y-%m-%dT%H:%M:%S" f = open(fn, "w") for p in ps: t = time.strftime(fmt, time.gmtime(p)) v = len(periods[p]) print >>f, "%s\t%f" % (t, v) f.close() f = os.popen("gnuplot", "w") # print >>f, "set terminal postscript eps color solid" # print >>f, "set output \"out.ps\"" print >>f, "set terminal png small size 800,600 xffffff x000000 x000000 xee7777 x000077" print >>f, "set output \"out.png\"" print >>f, "set xdata time" print >>f, "set data style lines" print >>f, "set timefmt \"" + fmt + "\"" print >>f, "set format x \"%Y-%m\"" print >>f, "set xlabel \"Date\"" print >>f, "set ylabel \"Number of posts per %dh\"" % (PERIOD_LENGTH / (60 * 60),) print >>f, "plot \"" + fn + "\" using 1:2 title 'Counts', \"" + fn + "\" using 1:2 smooth bezier title 'Smoothed'" f.close() os.unlink(fn) if __name__ == "__main__": main()