clearer names for actions, and infer actions better
[monitor.git] / statistics / monitorstats.py
1
2 from monitor import database
3 from datetime import datetime, timedelta
4 import os
5 import glob
6 import time
7
8 from monitor import config
9
10 def datetime_fromstr(str):
11         if '-' in str:
12                 try:
13                         tup = time.strptime(str, "%Y-%m-%d")
14                 except:
15                         tup = time.strptime(str, "%Y-%m-%d-%H:%M")
16         elif '/' in str:
17                 tup = time.strptime(str, "%m/%d/%Y")
18         else:
19                 tup = time.strptime(str, "%m/%d/%Y")
20         ret = datetime.fromtimestamp(time.mktime(tup))
21         return ret
22
23 def get_filefromglob(d, str, path="archive-pdb", returnlist=False):
24         # TODO: This is aweful.
25         startpath = os.getcwd()
26         os.chdir(config.MONITOR_SCRIPT_ROOT + "/" + path)
27
28         #archive = database.SPickle(path)
29         glob_str = "%s*.%s.pkl" % (d.strftime("%Y-%m-%d"), str)
30         fg_list = [ x[:-4] for x in glob.glob(glob_str) ]
31
32         os.chdir(startpath)
33
34         if returnlist:
35                 return sorted(fg_list)
36         else:
37                 return fg_list[0]
38
39 def get_archive(path):
40         full_path = config.MONITOR_SCRIPT_ROOT + "/" + path
41         return database.SPickle(full_path)
42         
43 def print_graph(data, begin, end, xaxis, offset=500, window=100):
44         s1=[]
45         s2=[]
46         s3=[]
47         for row in data:
48                 s1.append(row[0])
49                 s2.append(row[1])
50                 s3.append(row[2])
51         
52         delta=offset
53         s1 = map(lambda x: x-delta, s1)
54         rlow= zip(s1,s3)
55         rhigh = zip(s1,s2)
56         diff_low  = map(lambda x: x[0]-x[1], rlow)
57         diff_high = map(lambda x: x[0]+x[1], rhigh)
58         s1 = map(lambda x: str(x), s1)
59         diff_low = map(lambda x: str(x), diff_low)
60         diff_high = map(lambda x: str(x), diff_high)
61         print s1
62         print diff_low
63         print diff_high
64         print "http://chart.apis.google.com/chart?cht=lc&chds=0,100&chxt=x,y&chxl=0:%s1:|500|550|600&chs=700x200&chm=F,aaaaaa,1,-1,2&chd=t1:%s" % (xaxis, ",".join(s1) + "|" + ",".join(diff_low) + "|" + ",".join(s1) + "|" + ",".join(s1) +"|" + ",".join(diff_high) )
65
66 def get_xaxis(list, width=16, wide=False):
67         # 3 for odd
68         # 4 for even
69         # 5 for wide odd
70         # 6 for wide even
71         list_len = len(list)
72         if list_len == 0: return "||"
73
74         is_even = list_len % 2 == 0
75         #if is_even:
76         #       xaxis = "|" + list[0][:width] + "|" + list[-1][:width] + "|"
77         #else:
78         xaxis = "|" + list[0][:width] + "|" + list[list_len/2 + 1][:width] + "|" + list[-1][:width] + "|"
79         return xaxis
80