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