]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/database.py
Fix big amount of bugs in recently commited code
[linux-conf-perf.git] / scripts / database.py
1 import datetime
2 import postgresql
3 import collections
4
5 import utils
6 import exceptions
7 from conf import conf
8
9 def __git_describe__():
10         return utils.callsubprocess('git_describe',
11                         conf.git_describe_cmd, False, True)[0]
12
13 def __git_commit__():
14         return utils.callsubprocess('git_rev_parse',
15                         conf.git_commit_cmd, False, True)[0]
16
17 def __timestamp__():
18         return datetime.datetime.now().strftime('%y-%m-%d-%H-%M-%S')
19
20 Config = collections.namedtuple('Config', 'id hash cfile') # Named tuple for configuration
21 Measure = collections.namedtuple('Measure', 'id conf_id mfile value') # Named tuple for measurement
22
23 class database:
24         "Class used for accessing PostgreSQL project database."
25         def __init__(self):
26                 self.db = postgresql.open(database = conf.db_database,
27                                 user = conf.db_user,
28                                 password = conf.db_password,
29                                 host = conf.db_host,
30                                 port = conf.db_port
31                                 )
32                 # check if tables are present
33                 tables = ('toolsgit', 'configurations', 'measure')
34                 for tab in tables:
35                         val = self.db.prepare("""SELECT COUNT(*) FROM pg_class
36                                                            WHERE relname = $1""")(tab)[0][0]
37                         if val < 1:
38                                 raise exceptions.DatabaseUninitialized()
39
40         def check_toolsgit(self):
41                 "Return id of toolsgit row. If missing, it is inserted"
42                 ds = __git_describe__()
43                 cm = __git_commit__()
44                 ps = self.db.prepare("""SELECT id FROM toolsgit
45                                                           WHERE git_describe = $1 AND git_commit = $2
46                                                           """)
47                 id = ps(ds, cm)
48                 if id:
49                         return id[0][0]
50                 ps = self.db.prepare("""INSERT INTO toolsgit
51                                                    (git_describe, git_commit)
52                                                    VALUES
53                                                    ($1, $2);
54                                                    """)
55                 ps(ds, cm)
56                 return self.check_toolsgit()
57
58         def add_configuration(self, hash, cfile):
59                 "Add configuration to database."
60                 ps = self.db.prepare("""INSERT INTO configurations
61                                                                 (hash, cfile, gtime, toolgit)
62                                                                 VALUES
63                                                                 ($1, $2, $3, $4);
64                                                                 """)
65                 gt = self.check_toolsgit()
66                 tm = datetime.datetime.now()
67                 ps(hash, cfile, tm, gt)
68
69         def get_configration(self, hash):
70                 "Return configration id for inserted hash."
71                 ps = self.db.prepare("""SELECT id, cfile FROM configurations
72                                                                 WHERE hash = $1""")
73                 rtn = []
74                 for dt in ps(hash):
75                         rtn.append(Config(dt[0], hash, dt[1]))
76                 return rtn
77
78         def add_measure(self, mfile, conf_id, value = None):
79                 "Add measurement."
80                 ps = self.db.prepare("""INSERT INTO measure
81                                                                 (conf, mfile, value, mtime, toolgit)
82                                                                 VALUES
83                                                                 ($1, $2, $3, $4, $5);
84                                                                 """)
85                 gt = self.check_toolsgit()
86                 tm = datetime.datetime.now()
87                 ps(conf_id, mfile, value, tm, gt)
88
89         def update_measure(self, measure_id, value):
90                 "Update measured value"
91                 ps = self.db.prepare("""UPDATE measure SET
92                                                                 (value) = ($2)
93                                                                 WHERE
94                                                                 id = $1;
95                                                                 """)
96                 ps(measure_id, value)
97
98         def get_measures(self, conf_id):
99                 "Get measures for configuration with conf_id id"
100                 ps = self.db.prepare("""SELECT id, mfile, value FROM measure
101                                                                 WHERE conf = $1;
102                                                                 """)
103                 rtn = []
104                 for dt in ps(conf_id):
105                         rtn.append(Measure(dt[0], conf_id, dt[1], dt[2]))
106                 return rtn
107
108         def get_unmeasured(self):
109                 "Returns list of all unmeasured configurations."
110                 ps = self.db.prepare("""SELECT c.id, c.hash, c.cfile
111                                                                 FROM configurations AS c, measure AS m
112                                                                 WHERE c.id NOT IN m.conf;
113                                                                 """)
114                 rtn = []
115                 for dt in ps():
116                         rtn.append(Config(dt[0], dt[1], dt[2]))
117                 return rtn