mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 2 | import os, re, db, sys |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 3 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 4 | tko = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) |
| 5 | root_url_file = os.path.join(tko, '.root_url') |
| 6 | if os.path.exists(root_url_file): |
| 7 | html_root = open(root_url_file, 'r').readline().rstrip() |
| 8 | else: |
mbligh | c959f4f | 2007-10-25 14:47:17 +0000 | [diff] [blame] | 9 | html_root = '/results/' |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 10 | |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 11 | def 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'], |
mbligh | e6413db | 2007-12-13 16:07:26 +0000 | [diff] [blame] | 21 | 'tag': ['tag', 'job_idx', 'job_idx'], |
| 22 | 'job': ['job_idx', 'job_idx', 'job_idx'], |
mbligh | 05067a3 | 2007-12-03 17:48:04 +0000 | [diff] [blame] | 23 | 'user': ['username', 'job_idx', 'job_idx'], |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 24 | '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', |
mbligh | e6413db | 2007-12-13 16:07:26 +0000 | [diff] [blame] | 31 | 'tag': 'jobs', |
| 32 | 'job': 'jobs', |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 33 | '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 |
mbligh | e6413db | 2007-12-13 16:07:26 +0000 | [diff] [blame] | 49 | if field == 'tag': |
| 50 | where = " %s LIKE %s " % (lookup_field, value) |
| 51 | else: |
| 52 | where = " %s = %s " % (lookup_field, value) |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 53 | |
| 54 | match = db.select(sql, tablename, where) |
| 55 | # returns the value and its field name |
| 56 | return match, field_name_in_main_table |
| 57 | |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 58 | |
| 59 | def 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 | |
| 65 | def 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. |
| 69 | assert x_axis != y_axis |
| 70 | fields = '%s, %s, status, COUNT(status_word)' % (x_axis, y_axis) |
| 71 | group_by = '%s, %s, status' % (x_axis, y_axis) |
| 72 | rows = db.select(fields, 'test_view', where=where, group_by=group_by) |
| 73 | |
| 74 | data = {} |
| 75 | for (x, y, status, count) in rows: |
| 76 | if not data.has_key(x): |
| 77 | data[x] = {} |
| 78 | if not data[x].has_key(y): |
| 79 | data[x][y] = {} |
| 80 | data[x][y][status] = count |
| 81 | return data |
| 82 | |
| 83 | |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 84 | class anygroup: |
| 85 | @classmethod |
| 86 | def selectunique(klass, db, field): |
| 87 | """Return unique values for all possible groups within |
| 88 | the table.""" |
| 89 | rows, field_name_in_main_table = select(db, field, value=None, distinct=True) |
| 90 | groupnames = sorted([row for row in rows]) |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 91 | |
| 92 | # collapse duplicates where records have the same name but |
| 93 | # multiple index values |
| 94 | headers = {} |
| 95 | for field_name, idx_value in groupnames: |
| 96 | if headers.has_key(field_name): |
| 97 | headers[field_name].append(idx_value) |
| 98 | else: |
| 99 | headers[field_name] = [idx_value] |
| 100 | headers = headers.items() |
| 101 | headers.sort() |
| 102 | return [klass(db, field_name_in_main_table, groupname) for groupname in headers] |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def __init__(self, db, idx_name, name): |
| 106 | self.db = db |
| 107 | self.name = name[0] |
| 108 | self.idx_name = idx_name |
| 109 | self.idx_value = name[1] |
| 110 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 111 | |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 112 | class group: |
| 113 | @classmethod |
| 114 | def select(klass, db): |
| 115 | """Return all possible machine groups""" |
| 116 | rows = db.select('distinct machine_group', 'machines', |
| 117 | 'machine_group is not null') |
| 118 | groupnames = sorted([row[0] for row in rows]) |
| 119 | return [klass(db, groupname) for groupname in groupnames] |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 120 | |
| 121 | |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 122 | def __init__(self, db, name): |
| 123 | self.name = name |
| 124 | self.db = db |
| 125 | |
| 126 | |
| 127 | def machines(self): |
| 128 | return machine.select(self.db, { 'machine_group' : self.name }) |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 129 | |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 130 | |
| 131 | def tests(self, where = {}): |
| 132 | values = [self.name] |
| 133 | sql = 't inner join machines m on m.machine_idx=t.machine_idx where m.machine_group=%s' |
| 134 | for key in where.keys(): |
| 135 | sql += ' and %s=%%s' % key |
| 136 | values.append(where[key]) |
| 137 | return test.select_sql(self.db, sql, values) |
| 138 | |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 139 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 140 | class machine: |
| 141 | @classmethod |
| 142 | def select(klass, db, where = {}): |
| 143 | fields = ['machine_idx', 'hostname', 'machine_group', 'owner'] |
| 144 | machines = [] |
| 145 | for row in db.select(','.join(fields), 'machines', where): |
| 146 | machines.append(klass(db, *row)) |
| 147 | return machines |
| 148 | |
| 149 | |
| 150 | def __init__(self, db, idx, hostname, group, owner): |
| 151 | self.db = db |
| 152 | self.idx = idx |
| 153 | self.hostname = hostname |
| 154 | self.group = group |
mbligh | f736b33 | 2007-12-18 20:56:51 +0000 | [diff] [blame^] | 155 | self.owner = owner |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 156 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 157 | |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 158 | class kernel: |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 159 | @classmethod |
| 160 | def select(klass, db, where = {}): |
| 161 | fields = ['kernel_idx', 'kernel_hash', 'base', 'printable'] |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 162 | rows = db.select(','.join(fields), 'kernels', where) |
| 163 | return [klass(db, *row) for row in rows] |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 164 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 165 | |
| 166 | def __init__(self, db, idx, hash, base, printable): |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 167 | self.db = db |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 168 | self.idx = idx |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 169 | self.hash = hash |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 170 | self.base = base |
| 171 | self.printable = printable |
| 172 | self.patches = [] # THIS SHOULD PULL IN PATCHES! |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | class test: |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 176 | @classmethod |
mbligh | 85952b4 | 2007-12-07 16:28:33 +0000 | [diff] [blame] | 177 | def select(klass, db, where = {}, wherein = {}, distinct = False): |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 178 | fields = ['test_idx', 'job_idx', 'test', 'subdir', |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 179 | 'kernel_idx', 'status', 'reason', 'machine_idx'] |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 180 | tests = [] |
mbligh | 85952b4 | 2007-12-07 16:28:33 +0000 | [diff] [blame] | 181 | for row in db.select(','.join(fields), 'tests', where, wherein,distinct): |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 182 | tests.append(klass(db, *row)) |
| 183 | return tests |
| 184 | |
| 185 | |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 186 | @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 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 195 | def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, status_num, reason, machine_idx): |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 196 | self.idx = test_idx |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 197 | self.job = job(db, job_idx) |
mbligh | de7335d | 2007-09-26 16:53:20 +0000 | [diff] [blame] | 198 | self.testname = testname |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 199 | self.subdir = subdir |
mbligh | 50a2525 | 2007-09-27 15:26:17 +0000 | [diff] [blame] | 200 | self.kernel_idx = kernel_idx |
| 201 | self.__kernel = None |
| 202 | self.__iterations = None |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 203 | self.machine_idx = machine_idx |
| 204 | self.__machine = None |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 205 | self.status_num = status_num |
| 206 | self.status_word = db.status_word[status_num] |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 207 | self.reason = reason |
mbligh | 50a2525 | 2007-09-27 15:26:17 +0000 | [diff] [blame] | 208 | self.db = db |
mbligh | de7335d | 2007-09-26 16:53:20 +0000 | [diff] [blame] | 209 | if self.subdir: |
| 210 | self.url = html_root + self.job.tag + '/' + self.subdir |
| 211 | else: |
mbligh | 676510c | 2007-09-28 01:28:12 +0000 | [diff] [blame] | 212 | self.url = None |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 213 | |
mbligh | 50a2525 | 2007-09-27 15:26:17 +0000 | [diff] [blame] | 214 | |
| 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 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 241 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 242 | 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 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 252 | class job: |
| 253 | def __init__(self, db, job_idx): |
| 254 | where = {'job_idx' : job_idx} |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 255 | rows = db.select('tag, machine_idx', 'jobs', where) |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 256 | if not rows: |
| 257 | return None |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 258 | (self.tag, self.machine_idx) = rows[0] |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 259 | self.job_idx = job_idx |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 260 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 261 | |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 262 | class 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 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 278 | # class patch: |
| 279 | # def __init__(self): |
| 280 | # self.spec = None |