moved Time() class to generic.py
[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         for id in id2lb:
42                 if id2lb[id] not in lb2hn:
43                         lb2hn[id2lb[id]] = []
44
45         for node in l_nodes:
46                 # this won't reach sites without nodes, which I guess isn't a problem.
47                 if node['site_id'] in id2lb.keys():
48                         login_base = id2lb[node['site_id']]
49                 else:
50                         print >>sys.stderr, "%s has a foreign site_id %s" % (node['hostname'], node['site_id'])
51                         continue
52                         for i in id2lb:
53                                 print i, " ", id2lb[i]
54                         raise Exception, "Node has missing site id!! %s %d" %(node['hostname'], node['site_id'])
55                 if not login_base in dsn:
56                         lb2hn[login_base] = []
57                         dsn[login_base] = {}
58                         dsn[login_base]['plc'] = d_sites[login_base]
59                         dsn[login_base]['monitor'] = {} # event log, or something
60
61                 hostname = node['hostname']
62                 lb2hn[login_base].append(node)
63                 dsn[login_base][hostname] = {}
64                 dsn[login_base][hostname]['plc'] = node
65                 dsn[login_base][hostname]['comon'] = {}
66                 dsn[login_base][hostname]['monitor'] = {}
67
68                 hn2lb[hostname] = login_base
69         return (dsn, hn2lb, lb2hn)
70
71
72 class Time:
73     @classmethod
74     def dt_to_ts(cls, dt):
75         t = time.mktime(dt.timetuple())
76         return t
77
78     @classmethod
79     def ts_to_dt(cls, ts):
80         d = datetime.fromtimestamp(ts)
81         return d
82
83     @classmethod
84     def str_to_dt(cls, date_str, format="%Y-%m-%d %H:%M:%S"):
85         dt = datetime.strptime(date_str[:date_str.find('.')], format)
86         return dt
87
88     @classmethod
89     def str_to_ts(cls, date_str, format="%Y-%m-%d %H:%M:%S"):
90         ts = time.mktime(time.strptime(date_str[:date_str.find('.')], format))
91         return ts