clearer names for actions, and infer actions better
[monitor.git] / monitor / generic.py
1 import sys
2 from datetime import datetime, timedelta
3
4 def d_from_l(l, key, using=None, key_as=str, using_as=None):
5         d = {}
6         for obj in l:
7                 if not str(obj[key]) in d:
8                         if using is None:
9                                 d[key_as(obj[key])] = obj
10                         else:
11                                 if using_as is None:
12                                         d[key_as(obj[key])] = obj[using]
13                                 else:
14                                         d[key_as(obj[key])] = using_as(obj[using])
15                 else:
16                         print "Two objects have the same %s key %s!" % (key, obj[key])
17                         continue
18         return d
19
20 def dpcus_from_lpcus(l_pcus):
21         d_pcus = d_from_l(l_pcus, 'pcu_id')
22         return d_pcus
23
24 def dnodes_from_lnodes(l_nodes):
25         d_nodes = d_from_l(l_nodes, 'hostname')
26         return d_nodes
27
28 def dsites_from_lsites(l_sites):
29         d_sites = d_from_l(l_sites, 'login_base')
30         return d_sites 
31
32 def dsites_from_lsites_id(l_sites):
33         d_sites = d_from_l(l_sites, 'login_base')
34         id2lb = d_from_l(l_sites, 'site_id', 'login_base', int, str)
35         return (d_sites, id2lb)
36
37 def dsn_from_dsln(d_sites, id2lb, l_nodes):
38         lb2hn = {}
39         dsn = {}
40         hn2lb = {}
41         exclude = []
42         for id in id2lb:
43                 if id2lb[id] not in lb2hn:
44                         lb2hn[id2lb[id]] = []
45
46         for node in l_nodes:
47                 # this won't reach sites without nodes, which I guess isn't a problem.
48                 if node['site_id'] in id2lb.keys():
49                         login_base = id2lb[node['site_id']]
50                 else:
51                         print >>sys.stderr, "%s has a foreign site_id %s" % (node['hostname'], node['site_id'])
52                         exclude.append(node['hostname'])
53                         continue
54                         for i in id2lb:
55                                 print i, " ", id2lb[i]
56                         raise Exception, "Node has missing site id!! %s %d" %(node['hostname'], node['site_id'])
57                 if not login_base in dsn:
58                         lb2hn[login_base] = []
59                         dsn[login_base] = {}
60                         dsn[login_base]['plc'] = d_sites[login_base]
61                         dsn[login_base]['monitor'] = {} # event log, or something
62
63                 hostname = node['hostname']
64                 lb2hn[login_base].append(node)
65                 dsn[login_base][hostname] = {}
66                 dsn[login_base][hostname]['plc'] = node
67                 dsn[login_base][hostname]['comon'] = {}
68                 dsn[login_base][hostname]['monitor'] = {}
69
70                 hn2lb[hostname] = login_base
71         return (dsn, hn2lb, lb2hn, exclude)
72
73
74 class Time:
75     @classmethod
76     def dt_to_ts(cls, dt):
77         t = time.mktime(dt.timetuple())
78         return t
79
80     @classmethod
81     def ts_to_dt(cls, ts):
82         d = datetime.fromtimestamp(ts)
83         return d
84
85     @classmethod
86     def str_to_dt(cls, date_str, format="%Y-%m-%d %H:%M:%S"):
87         dt = datetime.strptime(date_str[:date_str.find('.')], format)
88         return dt
89
90     @classmethod
91     def str_to_ts(cls, date_str, format="%Y-%m-%d %H:%M:%S"):
92         ts = time.mktime(time.strptime(date_str[:date_str.find('.')], format))
93         return ts