]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/database.py
Add measurable check while initialization
[linux-conf-perf.git] / scripts / database.py
1 import os
2 import datetime
3 import postgresql
4 import collections
5
6 import utils
7 import exceptions
8 from conf import conf
9 from conf import sf
10
11 def __git_describe__():
12         return utils.callsubprocess('git_describe',
13                         conf.git_describe_cmd, False, True)[0]
14
15 def __git_commit__():
16         return utils.callsubprocess('git_rev_parse',
17                         conf.git_commit_cmd, False, True)[0]
18
19 def __timestamp__():
20         return datetime.datetime.now().strftime('%y-%m-%d-%H-%M-%S')
21
22 Config = collections.namedtuple('Config', 'id hash config') # Named tuple for configuration
23 Measure = collections.namedtuple('Measure', 'id conf_id output value') # Named tuple for measurement
24
25 class database:
26         "Class used for accessing PostgreSQL project database."
27         def __init__(self):
28                 self.db = postgresql.open(database = conf.db_database,
29                                 user = conf.db_user,
30                                 password = conf.db_password,
31                                 host = conf.db_host,
32                                 port = conf.db_port
33                                 )
34                 # check if tables are present
35                 tables = ('toolsgit', 'configurations', 'measure')
36                 for tab in tables:
37                         val = self.db.prepare("""SELECT COUNT(*) FROM pg_class
38                                                            WHERE relname = $1""")(tab)[0][0]
39                         if val < 1:
40                                 raise exceptions.DatabaseUninitialized()
41
42         def check_toolsgit(self):
43                 "Return id of toolsgit row. If missing, it is inserted"
44                 ds = __git_describe__()
45                 cm = __git_commit__()
46                 ps = self.db.prepare("""SELECT id FROM toolsgit
47                                                           WHERE git_describe = $1 AND git_commit = $2
48                                                           """)
49                 id = ps(ds, cm)
50                 if id:
51                         return id[0][0]
52                 ps = self.db.prepare("""INSERT INTO toolsgit
53                                                    (git_describe, git_commit)
54                                                    VALUES
55                                                    ($1, $2);
56                                                    """)
57                 ps(ds, cm)
58                 return self.check_toolsgit()
59
60         def check_linuxgit(self):
61                 "Return id of linuxgit row. If missing, it is inserted."
62                 wd = os.getcwd()
63                 os.chdir(sf(conf.linux_sources))
64                 ds = __git_describe__()
65                 cm = __git_commit__()
66                 os.chdir(wd)
67                 ps = self.db.prepare("""SELECT id FROM linuxgit
68                                                           WHERE git_describe = $1 AND git_commit = $2
69                                                           """)
70                 id = ps(ds, cm)
71                 if id:
72                         return id[0][0]
73                 ps = self.db.prepare("""INSERT INTO linuxgit
74                                                    (git_describe, git_commit)
75                                                    VALUES
76                                                    ($1, $2);
77                                                    """)
78                 ps(ds, cm)
79                 return self.check_linuxgit()
80
81         def add_configuration(self, hash, txtconfig, generator):
82                 "Add configuration to database."
83                 ps = self.db.prepare("""INSERT INTO configurations
84                                                                 (hash, config, gtime, toolgit, linuxgit, generator)
85                                                                 VALUES
86                                                                 ($1, $2, $3, $4, $5, $6);
87                                                                 """)
88                 gt = self.check_toolsgit()
89                 lgt = self.check_linuxgit()
90                 tm = datetime.datetime.now()
91                 ps(hash, '\n'.join(txtconfig), tm, gt, lgt, generator)
92
93         def get_configration(self, hash):
94                 "Return configration id for inserted hash."
95                 ps = self.db.prepare("""SELECT id, config FROM configurations
96                                                                 WHERE hash = $1""")
97                 rtn = []
98                 for dt in ps(hash):
99                         rtn.append(Config(dt[0], hash, dt[1].split('\n')))
100                 return rtn
101
102         def add_measure(self, output, result, conf_id, value = None):
103                 "Add measurement."
104                 ps = self.db.prepare("""INSERT INTO measure
105                                                                 (conf, output, value, mtime, toolgit,
106                                                                 linuxgit, measurement, result)
107                                                                 VALUES
108                                                                 ($1, $2, $3, $4, $5, $6, $7, $8);
109                                                                 """)
110                 gt = self.check_toolsgit()
111                 lgt = self.check_linuxgit()
112                 tm = datetime.datetime.now()
113                 ps(conf_id, output, value, tm, gt, lgt, conf.measure_identifier, result)
114
115         def update_measure(self, measure_id, value):
116                 "Update measured value"
117                 ps = self.db.prepare("""UPDATE measure SET
118                                                                 (value) = ($2)
119                                                                 WHERE
120                                                                 id = $1;
121                                                                 """)
122                 ps(measure_id, value)
123
124         def get_measures(self, conf_id):
125                 "Get measures for configuration with conf_id id"
126                 ps = self.db.prepare("""SELECT id, output, value FROM measure
127                                                                 WHERE conf = $1;
128                                                                 """)
129                 rtn = []
130                 for dt in ps(conf_id):
131                         rtn.append(Measure(dt[0], conf_id, dt[1], dt[2]))
132                 return rtn
133
134         def get_unmeasured(self):
135                 "Returns list of all unmeasured configurations."
136                 ps = self.db.prepare("""SELECT id, hash, config FROM configurations
137                                                                 WHERE id NOT IN
138                                                                 (SELECT conf FROM measure)
139                                                                 """)
140                 rtn = []
141                 for dt in ps():
142                         rtn.append(Config(dt[0], dt[1], dt[2].split('\n')))
143                 return rtn
144
145         def add_configsort(self, configopt):
146                 "Add configuration option to sorted list"
147                 ps = self.db.prepare("""INSERT INTO configopt
148                                                                 (configopt) VALUES ($1)
149                                                                 """)
150                 ps(configopt)
151
152         def get_configsort(self):
153                 "Returns sorted list of all configuration options"
154                 ps = self.db.prepare("""SELECT id, configopt FROM configopt
155                                                                 ORDER BY id ASC
156                                                                 """)
157                 rtn = []
158                 itms = ps()
159                 for id, config in itms:
160                         rtn.append(config)
161                 return rtn