blob: dc440bf13872f8ff51a9430ee700fa84867d1a48 [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'],
mbligh05067a32007-12-03 17:48:04 +000021 'user': ['username', 'job_idx', 'job_idx'],
mbligh2e4e5df2007-11-05 17:22:46 +000022 'test': ['test', 'test', 'test'],
23 'status': ['word', 'status_idx', 'status'],
24 'reason': ['reason', 'test_idx', 'test_idx'] }
25 table = { 'kernel': 'kernels',
26 'machine_group': 'machines',
27 'hostname': 'machines',
28 'label': 'jobs',
29 'user': 'jobs',
30 'test': 'tests',
31 'status': 'status',
32 'reason': 'tests' }
33
34 lookup_field, idx_field, field_name_in_main_table = fields[field]
35 tablename = table[field]
36 # select all the index values that match the given field name.
37 sql = ""
38 if distinct:
39 sql += " distinct "
40 if not value:
41 sql += " %s , %s " % (lookup_field, idx_field)
42 where = " %s is not null " % lookup_field
43 else:
44 sql += "%s " % idx_field
45 where = " %s = %s " % (lookup_field, value)
46
47 match = db.select(sql, tablename, where)
48 # returns the value and its field name
49 return match, field_name_in_main_table
50
mbligh83f63a02007-12-12 19:13:04 +000051
52def get_axis_data(axis):
53 rows = db.select(axis , 'test_view', distinct = True)
54 # Need to do a magic sort here if axis == 'kernel_printable'
55 return sorted([row[0] for row in rows])
56
57
58def get_matrix_data(db, x_axis, y_axis, where = None):
59 # Return a 3-d hash of data - [x-value][y-value][status_word]
60 # Searches on the test_view table - x_axis and y_axis must both be
61 # column names in that table.
62 assert x_axis != y_axis
63 fields = '%s, %s, status, COUNT(status_word)' % (x_axis, y_axis)
64 group_by = '%s, %s, status' % (x_axis, y_axis)
65 rows = db.select(fields, 'test_view', where=where, group_by=group_by)
66
67 data = {}
68 for (x, y, status, count) in rows:
69 if not data.has_key(x):
70 data[x] = {}
71 if not data[x].has_key(y):
72 data[x][y] = {}
73 data[x][y][status] = count
74 return data
75
76
mbligh2e4e5df2007-11-05 17:22:46 +000077class anygroup:
78 @classmethod
79 def selectunique(klass, db, field):
80 """Return unique values for all possible groups within
81 the table."""
82 rows, field_name_in_main_table = select(db, field, value=None, distinct=True)
83 groupnames = sorted([row for row in rows])
mbligh2b672532007-11-05 19:24:51 +000084
85 # collapse duplicates where records have the same name but
86 # multiple index values
87 headers = {}
88 for field_name, idx_value in groupnames:
89 if headers.has_key(field_name):
90 headers[field_name].append(idx_value)
91 else:
92 headers[field_name] = [idx_value]
93 headers = headers.items()
94 headers.sort()
95 return [klass(db, field_name_in_main_table, groupname) for groupname in headers]
mbligh2e4e5df2007-11-05 17:22:46 +000096
97
98 def __init__(self, db, idx_name, name):
99 self.db = db
100 self.name = name[0]
101 self.idx_name = idx_name
102 self.idx_value = name[1]
103
mbligh2aaeb672007-10-01 14:54:18 +0000104
mblighcff2d212007-10-07 00:11:10 +0000105class group:
106 @classmethod
107 def select(klass, db):
108 """Return all possible machine groups"""
109 rows = db.select('distinct machine_group', 'machines',
110 'machine_group is not null')
111 groupnames = sorted([row[0] for row in rows])
112 return [klass(db, groupname) for groupname in groupnames]
mbligh83f63a02007-12-12 19:13:04 +0000113
114
mblighcff2d212007-10-07 00:11:10 +0000115 def __init__(self, db, name):
116 self.name = name
117 self.db = db
118
119
120 def machines(self):
121 return machine.select(self.db, { 'machine_group' : self.name })
mbligh2e4e5df2007-11-05 17:22:46 +0000122
mblighcff2d212007-10-07 00:11:10 +0000123
124 def tests(self, where = {}):
125 values = [self.name]
126 sql = 't inner join machines m on m.machine_idx=t.machine_idx where m.machine_group=%s'
127 for key in where.keys():
128 sql += ' and %s=%%s' % key
129 values.append(where[key])
130 return test.select_sql(self.db, sql, values)
131
mbligh2e4e5df2007-11-05 17:22:46 +0000132
mbligh2aaeb672007-10-01 14:54:18 +0000133class machine:
134 @classmethod
135 def select(klass, db, where = {}):
136 fields = ['machine_idx', 'hostname', 'machine_group', 'owner']
137 machines = []
138 for row in db.select(','.join(fields), 'machines', where):
139 machines.append(klass(db, *row))
140 return machines
141
142
143 def __init__(self, db, idx, hostname, group, owner):
144 self.db = db
145 self.idx = idx
146 self.hostname = hostname
147 self.group = group
mblighf1898822007-12-07 16:32:03 +0000148 if owner:
149 if len(owner) > 3:
150 self.owner = owner.capitalize()
151 else:
152 # capitalize acroymns
153 self.owner = owner.upper()
mbligh8174ce32007-11-30 17:57:55 +0000154 else:
mblighf1898822007-12-07 16:32:03 +0000155 self.owner = None
mbligh2e4e5df2007-11-05 17:22:46 +0000156
mbligh250300e2007-09-18 00:50:57 +0000157
mbligh9bb92fe2007-09-12 15:54:23 +0000158class kernel:
mbligh8e1ab172007-09-13 17:29:56 +0000159 @classmethod
160 def select(klass, db, where = {}):
161 fields = ['kernel_idx', 'kernel_hash', 'base', 'printable']
mbligh83f63a02007-12-12 19:13:04 +0000162 rows = db.select(','.join(fields), 'kernels', where)
163 return [klass(db, *row) for row in rows]
mbligh9bb92fe2007-09-12 15:54:23 +0000164
mbligh8e1ab172007-09-13 17:29:56 +0000165
166 def __init__(self, db, idx, hash, base, printable):
mbligh9bb92fe2007-09-12 15:54:23 +0000167 self.db = db
mbligh8e1ab172007-09-13 17:29:56 +0000168 self.idx = idx
mbligh9bb92fe2007-09-12 15:54:23 +0000169 self.hash = hash
mbligh8e1ab172007-09-13 17:29:56 +0000170 self.base = base
171 self.printable = printable
172 self.patches = [] # THIS SHOULD PULL IN PATCHES!
mbligh9bb92fe2007-09-12 15:54:23 +0000173
174
175class test:
mbligh8e1ab172007-09-13 17:29:56 +0000176 @classmethod
mbligh85952b42007-12-07 16:28:33 +0000177 def select(klass, db, where = {}, wherein = {}, distinct = False):
mbligh8e1ab172007-09-13 17:29:56 +0000178 fields = ['test_idx', 'job_idx', 'test', 'subdir',
mbligh2aaeb672007-10-01 14:54:18 +0000179 'kernel_idx', 'status', 'reason', 'machine_idx']
mbligh8e1ab172007-09-13 17:29:56 +0000180 tests = []
mbligh85952b42007-12-07 16:28:33 +0000181 for row in db.select(','.join(fields), 'tests', where, wherein,distinct):
mbligh8e1ab172007-09-13 17:29:56 +0000182 tests.append(klass(db, *row))
183 return tests
184
185
mbligh414c69e2007-10-05 15:13:06 +0000186 @classmethod
187 def select_sql(klass, db, sql, values):
188 fields = ['test_idx', 'job_idx', 'test', 'subdir',
189 'kernel_idx', 'status', 'reason', 'machine_idx']
190 fields = ['t.'+field for field in fields]
191 rows = db.select_sql(','.join(fields), 'tests', sql, values)
192 return [klass(db, *row) for row in rows]
193
194
mbligh2aaeb672007-10-01 14:54:18 +0000195 def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, status_num, reason, machine_idx):
mbligh8e1ab172007-09-13 17:29:56 +0000196 self.idx = test_idx
mbligh250300e2007-09-18 00:50:57 +0000197 self.job = job(db, job_idx)
mblighde7335d2007-09-26 16:53:20 +0000198 self.testname = testname
mbligh8e1ab172007-09-13 17:29:56 +0000199 self.subdir = subdir
mbligh50a25252007-09-27 15:26:17 +0000200 self.kernel_idx = kernel_idx
201 self.__kernel = None
202 self.__iterations = None
mbligh2aaeb672007-10-01 14:54:18 +0000203 self.machine_idx = machine_idx
204 self.__machine = None
mbligh8e1ab172007-09-13 17:29:56 +0000205 self.status_num = status_num
206 self.status_word = db.status_word[status_num]
mbligh9bb92fe2007-09-12 15:54:23 +0000207 self.reason = reason
mbligh50a25252007-09-27 15:26:17 +0000208 self.db = db
mblighde7335d2007-09-26 16:53:20 +0000209 if self.subdir:
210 self.url = html_root + self.job.tag + '/' + self.subdir
211 else:
mbligh676510c2007-09-28 01:28:12 +0000212 self.url = None
mbligh16ae9262007-09-21 00:53:08 +0000213
mbligh50a25252007-09-27 15:26:17 +0000214
215
216 def iterations(self):
217 """
218 Caching function for iterations
219 """
220 if not self.__iterations:
221 self.__iterations = {}
222 # A dictionary - dict{key} = [value1, value2, ....]
223 where = {'test_idx' : self.idx}
224 for i in iteration.select(self.db, where):
225 if self.__iterations.has_key(i.key):
226 self.__iterations[i.key].append(i.value)
227 else:
228 self.__iterations[i.key] = [i.value]
229 return self.__iterations
230
231
232 def kernel(self):
233 """
234 Caching function for kernels
235 """
236 if not self.__kernel:
237 where = {'kernel_idx' : self.kernel_idx}
238 self.__kernel = kernel.select(self.db, where)[0]
239 return self.__kernel
240
mbligh250300e2007-09-18 00:50:57 +0000241
mbligh2aaeb672007-10-01 14:54:18 +0000242 def machine(self):
243 """
244 Caching function for kernels
245 """
246 if not self.__machine:
247 where = {'machine_idx' : self.machine_idx}
248 self.__machine = machine.select(self.db, where)[0]
249 return self.__machine
250
251
mbligh250300e2007-09-18 00:50:57 +0000252class job:
253 def __init__(self, db, job_idx):
254 where = {'job_idx' : job_idx}
mbligh2aaeb672007-10-01 14:54:18 +0000255 rows = db.select('tag, machine_idx', 'jobs', where)
mbligh250300e2007-09-18 00:50:57 +0000256 if not rows:
257 return None
mbligh2aaeb672007-10-01 14:54:18 +0000258 (self.tag, self.machine_idx) = rows[0]
mbligh2b672532007-11-05 19:24:51 +0000259 self.job_idx = job_idx
mbligh250300e2007-09-18 00:50:57 +0000260
mbligh8e1ab172007-09-13 17:29:56 +0000261
mbligh16ae9262007-09-21 00:53:08 +0000262class iteration:
263 @classmethod
264 def select(klass, db, where):
265 fields = ['iteration', 'attribute', 'value']
266 iterations = []
267 rows = db.select(','.join(fields), 'iteration_result', where)
268 for row in rows:
269 iterations.append(klass(*row))
270 return iterations
271
272
273 def __init__(self, iteration, key, value):
274 self.iteration = iteration
275 self.key = key
276 self.value = value
277
mbligh8e1ab172007-09-13 17:29:56 +0000278# class patch:
279# def __init__(self):
280# self.spec = None