blob: 5f431f35f3f8f3aa92302e5a866a7c979b0727d4 [file] [log] [blame]
mbligh9bb92fe2007-09-12 15:54:23 +00001#!/usr/bin/python
mbligh2aaeb672007-10-01 14:54:18 +00002import os, re, db, sys
mbligh9bb92fe2007-09-12 15:54:23 +00003
mbligh2aaeb672007-10-01 14:54:18 +00004tko = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
5root_url_file = os.path.join(tko, '.root_url')
6if os.path.exists(root_url_file):
7 html_root = open(root_url_file, 'r').readline().rstrip()
8else:
mblighc959f4f2007-10-25 14:47:17 +00009 html_root = '/results/'
mbligh2aaeb672007-10-01 14:54:18 +000010
mbligh2e4e5df2007-11-05 17:22:46 +000011def select(db, field, value=None, distinct=False):
12 """ returns the relevant index values where the field value matches the
13 input value to the function.
14 If there is no value passed, then it returns the index values and the
15 field values corresponsing to them. """
16
17 fields = { 'kernel': ['printable', 'kernel_idx', 'kernel_idx'],
18 'machine_group': ['machine_group', 'machine_idx', 'machine_idx'],
19 'hostname': ['hostname', 'machine_idx', 'machine_idx'],
20 'label': ['label', 'job_idx', 'job_idx'],
mblighe6413db2007-12-13 16:07:26 +000021 'tag': ['tag', 'job_idx', 'job_idx'],
22 'job': ['job_idx', 'job_idx', 'job_idx'],
mbligh05067a32007-12-03 17:48:04 +000023 'user': ['username', 'job_idx', 'job_idx'],
mbligh2e4e5df2007-11-05 17:22:46 +000024 'test': ['test', 'test', 'test'],
25 'status': ['word', 'status_idx', 'status'],
26 'reason': ['reason', 'test_idx', 'test_idx'] }
27 table = { 'kernel': 'kernels',
28 'machine_group': 'machines',
29 'hostname': 'machines',
30 'label': 'jobs',
mblighe6413db2007-12-13 16:07:26 +000031 'tag': 'jobs',
32 'job': 'jobs',
mbligh2e4e5df2007-11-05 17:22:46 +000033 'user': 'jobs',
34 'test': 'tests',
35 'status': 'status',
36 'reason': 'tests' }
37
38 lookup_field, idx_field, field_name_in_main_table = fields[field]
39 tablename = table[field]
40 # select all the index values that match the given field name.
41 sql = ""
42 if distinct:
43 sql += " distinct "
44 if not value:
45 sql += " %s , %s " % (lookup_field, idx_field)
46 where = " %s is not null " % lookup_field
47 else:
48 sql += "%s " % idx_field
mblighe6413db2007-12-13 16:07:26 +000049 if field == 'tag':
50 where = " %s LIKE %s " % (lookup_field, value)
51 else:
52 where = " %s = %s " % (lookup_field, value)
mbligh2e4e5df2007-11-05 17:22:46 +000053
54 match = db.select(sql, tablename, where)
55 # returns the value and its field name
56 return match, field_name_in_main_table
57
mbligh83f63a02007-12-12 19:13:04 +000058
59def get_axis_data(axis):
60 rows = db.select(axis , 'test_view', distinct = True)
61 # Need to do a magic sort here if axis == 'kernel_printable'
62 return sorted([row[0] for row in rows])
63
64
65def get_matrix_data(db, x_axis, y_axis, where = None):
66 # Return a 3-d hash of data - [x-value][y-value][status_word]
67 # Searches on the test_view table - x_axis and y_axis must both be
68 # column names in that table.
mbligh5dd503b2008-01-03 16:35:27 +000069 fields = ('%s, %s, status, COUNT(status_word), ' +
70 'LEFT(GROUP_CONCAT(job_tag), 100)' # limit what's returned
71 ) % (x_axis, y_axis)
mbligh83f63a02007-12-12 19:13:04 +000072 group_by = '%s, %s, status' % (x_axis, y_axis)
73 rows = db.select(fields, 'test_view', where=where, group_by=group_by)
74
75 data = {}
mbligh5dd503b2008-01-03 16:35:27 +000076 job_tags = {}
mbligh12eebfa2008-01-03 02:01:53 +000077 x_set = set()
78 y_set = set()
79 status_set = set()
mbligh5dd503b2008-01-03 16:35:27 +000080 for (x, y, status, count, job_tag) in rows:
mbligh83f63a02007-12-12 19:13:04 +000081 if not data.has_key(x):
82 data[x] = {}
mbligh5dd503b2008-01-03 16:35:27 +000083 job_tags[x] = {}
mbligh83f63a02007-12-12 19:13:04 +000084 if not data[x].has_key(y):
85 data[x][y] = {}
86 data[x][y][status] = count
mbligh5dd503b2008-01-03 16:35:27 +000087 if job_tags[x].has_key(y) or count != 1:
88 job_tags[x][y] = None
89 else:
90 job_tags[x][y] = job_tag
mbligh12eebfa2008-01-03 02:01:53 +000091 x_set.add(x)
92 y_set.add(y)
93 status_set.add(status)
mbligh5dd503b2008-01-03 16:35:27 +000094 return (data, list(x_set), list(y_set), list(status_set), job_tags)
mbligh83f63a02007-12-12 19:13:04 +000095
96
mbligh2e4e5df2007-11-05 17:22:46 +000097class anygroup:
98 @classmethod
99 def selectunique(klass, db, field):
100 """Return unique values for all possible groups within
101 the table."""
102 rows, field_name_in_main_table = select(db, field, value=None, distinct=True)
103 groupnames = sorted([row for row in rows])
mbligh2b672532007-11-05 19:24:51 +0000104
105 # collapse duplicates where records have the same name but
106 # multiple index values
107 headers = {}
108 for field_name, idx_value in groupnames:
109 if headers.has_key(field_name):
110 headers[field_name].append(idx_value)
111 else:
112 headers[field_name] = [idx_value]
113 headers = headers.items()
114 headers.sort()
115 return [klass(db, field_name_in_main_table, groupname) for groupname in headers]
mbligh2e4e5df2007-11-05 17:22:46 +0000116
117
118 def __init__(self, db, idx_name, name):
119 self.db = db
120 self.name = name[0]
121 self.idx_name = idx_name
122 self.idx_value = name[1]
123
mbligh2aaeb672007-10-01 14:54:18 +0000124
mblighcff2d212007-10-07 00:11:10 +0000125class group:
126 @classmethod
127 def select(klass, db):
128 """Return all possible machine groups"""
129 rows = db.select('distinct machine_group', 'machines',
130 'machine_group is not null')
131 groupnames = sorted([row[0] for row in rows])
132 return [klass(db, groupname) for groupname in groupnames]
mbligh83f63a02007-12-12 19:13:04 +0000133
134
mblighcff2d212007-10-07 00:11:10 +0000135 def __init__(self, db, name):
136 self.name = name
137 self.db = db
138
139
140 def machines(self):
141 return machine.select(self.db, { 'machine_group' : self.name })
mbligh2e4e5df2007-11-05 17:22:46 +0000142
mblighcff2d212007-10-07 00:11:10 +0000143
144 def tests(self, where = {}):
145 values = [self.name]
146 sql = 't inner join machines m on m.machine_idx=t.machine_idx where m.machine_group=%s'
147 for key in where.keys():
148 sql += ' and %s=%%s' % key
149 values.append(where[key])
150 return test.select_sql(self.db, sql, values)
151
mbligh2e4e5df2007-11-05 17:22:46 +0000152
mbligh2aaeb672007-10-01 14:54:18 +0000153class machine:
154 @classmethod
155 def select(klass, db, where = {}):
156 fields = ['machine_idx', 'hostname', 'machine_group', 'owner']
157 machines = []
158 for row in db.select(','.join(fields), 'machines', where):
159 machines.append(klass(db, *row))
160 return machines
161
162
163 def __init__(self, db, idx, hostname, group, owner):
164 self.db = db
165 self.idx = idx
166 self.hostname = hostname
167 self.group = group
mblighf736b332007-12-18 20:56:51 +0000168 self.owner = owner
mbligh2e4e5df2007-11-05 17:22:46 +0000169
mbligh250300e2007-09-18 00:50:57 +0000170
mbligh9bb92fe2007-09-12 15:54:23 +0000171class kernel:
mbligh8e1ab172007-09-13 17:29:56 +0000172 @classmethod
173 def select(klass, db, where = {}):
174 fields = ['kernel_idx', 'kernel_hash', 'base', 'printable']
mbligh83f63a02007-12-12 19:13:04 +0000175 rows = db.select(','.join(fields), 'kernels', where)
176 return [klass(db, *row) for row in rows]
mbligh9bb92fe2007-09-12 15:54:23 +0000177
mbligh8e1ab172007-09-13 17:29:56 +0000178
179 def __init__(self, db, idx, hash, base, printable):
mbligh9bb92fe2007-09-12 15:54:23 +0000180 self.db = db
mbligh8e1ab172007-09-13 17:29:56 +0000181 self.idx = idx
mbligh9bb92fe2007-09-12 15:54:23 +0000182 self.hash = hash
mbligh8e1ab172007-09-13 17:29:56 +0000183 self.base = base
184 self.printable = printable
185 self.patches = [] # THIS SHOULD PULL IN PATCHES!
mbligh9bb92fe2007-09-12 15:54:23 +0000186
187
188class test:
mbligh8e1ab172007-09-13 17:29:56 +0000189 @classmethod
mbligh85952b42007-12-07 16:28:33 +0000190 def select(klass, db, where = {}, wherein = {}, distinct = False):
mbligh8e1ab172007-09-13 17:29:56 +0000191 fields = ['test_idx', 'job_idx', 'test', 'subdir',
mbligh2aaeb672007-10-01 14:54:18 +0000192 'kernel_idx', 'status', 'reason', 'machine_idx']
mbligh8e1ab172007-09-13 17:29:56 +0000193 tests = []
mbligh85952b42007-12-07 16:28:33 +0000194 for row in db.select(','.join(fields), 'tests', where, wherein,distinct):
mbligh8e1ab172007-09-13 17:29:56 +0000195 tests.append(klass(db, *row))
196 return tests
197
198
mbligh414c69e2007-10-05 15:13:06 +0000199 @classmethod
200 def select_sql(klass, db, sql, values):
201 fields = ['test_idx', 'job_idx', 'test', 'subdir',
202 'kernel_idx', 'status', 'reason', 'machine_idx']
203 fields = ['t.'+field for field in fields]
204 rows = db.select_sql(','.join(fields), 'tests', sql, values)
205 return [klass(db, *row) for row in rows]
206
207
mbligh2aaeb672007-10-01 14:54:18 +0000208 def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, status_num, reason, machine_idx):
mbligh8e1ab172007-09-13 17:29:56 +0000209 self.idx = test_idx
mbligh250300e2007-09-18 00:50:57 +0000210 self.job = job(db, job_idx)
mblighde7335d2007-09-26 16:53:20 +0000211 self.testname = testname
mbligh8e1ab172007-09-13 17:29:56 +0000212 self.subdir = subdir
mbligh50a25252007-09-27 15:26:17 +0000213 self.kernel_idx = kernel_idx
214 self.__kernel = None
215 self.__iterations = None
mbligh2aaeb672007-10-01 14:54:18 +0000216 self.machine_idx = machine_idx
217 self.__machine = None
mbligh8e1ab172007-09-13 17:29:56 +0000218 self.status_num = status_num
219 self.status_word = db.status_word[status_num]
mbligh9bb92fe2007-09-12 15:54:23 +0000220 self.reason = reason
mbligh50a25252007-09-27 15:26:17 +0000221 self.db = db
mblighde7335d2007-09-26 16:53:20 +0000222 if self.subdir:
223 self.url = html_root + self.job.tag + '/' + self.subdir
224 else:
mbligh676510c2007-09-28 01:28:12 +0000225 self.url = None
mbligh16ae9262007-09-21 00:53:08 +0000226
mbligh50a25252007-09-27 15:26:17 +0000227
228
229 def iterations(self):
230 """
231 Caching function for iterations
232 """
233 if not self.__iterations:
234 self.__iterations = {}
235 # A dictionary - dict{key} = [value1, value2, ....]
236 where = {'test_idx' : self.idx}
237 for i in iteration.select(self.db, where):
238 if self.__iterations.has_key(i.key):
239 self.__iterations[i.key].append(i.value)
240 else:
241 self.__iterations[i.key] = [i.value]
242 return self.__iterations
243
244
245 def kernel(self):
246 """
247 Caching function for kernels
248 """
249 if not self.__kernel:
250 where = {'kernel_idx' : self.kernel_idx}
251 self.__kernel = kernel.select(self.db, where)[0]
252 return self.__kernel
253
mbligh250300e2007-09-18 00:50:57 +0000254
mbligh2aaeb672007-10-01 14:54:18 +0000255 def machine(self):
256 """
257 Caching function for kernels
258 """
259 if not self.__machine:
260 where = {'machine_idx' : self.machine_idx}
261 self.__machine = machine.select(self.db, where)[0]
262 return self.__machine
263
264
mbligh250300e2007-09-18 00:50:57 +0000265class job:
266 def __init__(self, db, job_idx):
267 where = {'job_idx' : job_idx}
mbligh2aaeb672007-10-01 14:54:18 +0000268 rows = db.select('tag, machine_idx', 'jobs', where)
mbligh250300e2007-09-18 00:50:57 +0000269 if not rows:
270 return None
mbligh2aaeb672007-10-01 14:54:18 +0000271 (self.tag, self.machine_idx) = rows[0]
mbligh2b672532007-11-05 19:24:51 +0000272 self.job_idx = job_idx
mbligh250300e2007-09-18 00:50:57 +0000273
mbligh8e1ab172007-09-13 17:29:56 +0000274
mbligh16ae9262007-09-21 00:53:08 +0000275class iteration:
276 @classmethod
277 def select(klass, db, where):
278 fields = ['iteration', 'attribute', 'value']
279 iterations = []
280 rows = db.select(','.join(fields), 'iteration_result', where)
281 for row in rows:
282 iterations.append(klass(*row))
283 return iterations
284
285
286 def __init__(self, iteration, key, value):
287 self.iteration = iteration
288 self.key = key
289 self.value = value
290
mbligh8e1ab172007-09-13 17:29:56 +0000291# class patch:
292# def __init__(self):
293# self.spec = None