mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 2 | import os, re, db, sys, datetime |
mbligh | 5bb5586 | 2008-04-16 23:09:31 +0000 | [diff] [blame^] | 3 | MAX_RECORDS = 20000L |
| 4 | MAX_CELLS = 300000L |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 5 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 6 | tko = os.path.dirname(os.path.realpath(os.path.abspath(__file__))) |
| 7 | client_bin = os.path.abspath(os.path.join(tko, '../client/bin')) |
| 8 | sys.path.insert(0, client_bin) |
| 9 | import kernel_versions |
| 10 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 11 | root_url_file = os.path.join(tko, '.root_url') |
| 12 | if os.path.exists(root_url_file): |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 13 | html_root = open(root_url_file, 'r').readline().rstrip() |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 14 | else: |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 15 | html_root = '/results/' |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 16 | |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 17 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 18 | class status_cell: |
| 19 | # One cell in the matrix of status data. |
| 20 | def __init__(self): |
| 21 | # Count is a dictionary: status -> count of tests with status |
| 22 | self.status_count = {} |
| 23 | self.job_tag = None |
| 24 | self.job_tag_count = 0 |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 25 | |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 26 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 27 | def add(self, status, count, job_tags): |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 28 | assert count > 0 |
| 29 | |
| 30 | self.job_tag = job_tags |
| 31 | self.job_tag_count += count |
| 32 | if self.job_tag_count > 1: |
| 33 | self.job_tag = None |
| 34 | |
| 35 | self.status_count[status] = count |
| 36 | |
| 37 | |
| 38 | class status_data: |
| 39 | def __init__(self, sql_rows, x_field, y_field): |
| 40 | data = {} |
| 41 | y_values = set() |
| 42 | |
| 43 | # Walk through the query, filing all results by x, y info |
| 44 | for (x, y, status, count, job_tags) in sql_rows: |
| 45 | if not data.has_key(x): |
| 46 | data[x] = {} |
| 47 | if not data[x].has_key(y): |
| 48 | y_values.add(y) |
| 49 | data[x][y] = status_cell() |
| 50 | data[x][y].add(status, count, job_tags) |
| 51 | |
| 52 | # 2-d hash of data - [x-value][y-value] |
| 53 | self.data = data |
| 54 | # List of possible columns (x-values) |
| 55 | self.x_values = smart_sort(data.keys(), x_field) |
| 56 | # List of rows columns (y-values) |
| 57 | self.y_values = smart_sort(list(y_values), y_field) |
mbligh | 5bb5586 | 2008-04-16 23:09:31 +0000 | [diff] [blame^] | 58 | nCells = len(self.y_values)*len(self.x_values) |
| 59 | if nCells > MAX_CELLS: |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 60 | msg = 'Exceeded allowed number of cells in a table' |
| 61 | raise db.MySQLTooManyRows(msg) |
| 62 | |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 63 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 64 | def get_matrix_data(db_obj, x_axis, y_axis, where = None): |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 65 | # Searches on the test_view table - x_axis and y_axis must both be |
| 66 | # column names in that table. |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 67 | x_field = test_view_field_dict[x_axis] |
| 68 | y_field = test_view_field_dict[y_axis] |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 69 | fields = ( '%s, %s, status, COUNT(status), ' + |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 70 | 'LEFT(GROUP_CONCAT(job_tag), 100)' # limit what's returned |
| 71 | ) % (x_field, y_field) |
| 72 | group_by = '%s, %s, status' % (x_field, y_field) |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 73 | rows = db_obj.select(fields, 'test_view', |
| 74 | where=where, group_by=group_by, max_rows = MAX_RECORDS) |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 75 | return status_data(rows, x_field, y_field) |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 76 | |
| 77 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 78 | # Dictionary used simply for fast lookups from short reference names for users |
| 79 | # to fieldnames in test_view |
| 80 | test_view_field_dict = { |
| 81 | 'kernel' : 'kernel_printable', |
| 82 | 'hostname' : 'machine_hostname', |
| 83 | 'test' : 'test', |
| 84 | 'label' : 'job_label', |
| 85 | 'machine_group' : 'machine_group', |
| 86 | 'reason' : 'reason', |
| 87 | 'tag' : 'job_tag', |
| 88 | 'user' : 'job_username', |
| 89 | 'status' : 'status_word', |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 90 | 'time' : 'test_finished_time', |
mbligh | 5bb5586 | 2008-04-16 23:09:31 +0000 | [diff] [blame^] | 91 | 'time_daily' : 'DATE(test_finished_time)' |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 92 | } |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 93 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 94 | def smart_sort(list, field): |
| 95 | if field == 'kernel_printable': |
| 96 | def kernel_encode(kernel): |
| 97 | return kernel_versions.version_encode(kernel) |
| 98 | list.sort(key = kernel_encode, reverse = True) |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 99 | elif field.endswith('time'): |
| 100 | ## old records may contain time=None |
| 101 | ## make None comparable with timestamp as string |
| 102 | def handleNone(date_time): |
| 103 | if date_time==None: |
| 104 | return datetime.datetime(1970,1,1,0,0,0) |
| 105 | else: |
| 106 | return date_time |
| 107 | list = map(handleNone,list) |
| 108 | list.sort() |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 109 | return list |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 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 | |
mbligh | 50a2525 | 2007-09-27 15:26:17 +0000 | [diff] [blame] | 215 | def iterations(self): |
| 216 | """ |
| 217 | Caching function for iterations |
| 218 | """ |
| 219 | if not self.__iterations: |
| 220 | self.__iterations = {} |
| 221 | # A dictionary - dict{key} = [value1, value2, ....] |
| 222 | where = {'test_idx' : self.idx} |
| 223 | for i in iteration.select(self.db, where): |
| 224 | if self.__iterations.has_key(i.key): |
| 225 | self.__iterations[i.key].append(i.value) |
| 226 | else: |
| 227 | self.__iterations[i.key] = [i.value] |
| 228 | return self.__iterations |
| 229 | |
| 230 | |
| 231 | def kernel(self): |
| 232 | """ |
| 233 | Caching function for kernels |
| 234 | """ |
| 235 | if not self.__kernel: |
| 236 | where = {'kernel_idx' : self.kernel_idx} |
| 237 | self.__kernel = kernel.select(self.db, where)[0] |
| 238 | return self.__kernel |
| 239 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 240 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 241 | def machine(self): |
| 242 | """ |
| 243 | Caching function for kernels |
| 244 | """ |
| 245 | if not self.__machine: |
| 246 | where = {'machine_idx' : self.machine_idx} |
| 247 | self.__machine = machine.select(self.db, where)[0] |
| 248 | return self.__machine |
| 249 | |
| 250 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 251 | class job: |
| 252 | def __init__(self, db, job_idx): |
| 253 | where = {'job_idx' : job_idx} |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 254 | rows = db.select('tag, machine_idx', 'jobs', where) |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 255 | if not rows: |
| 256 | return None |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 257 | (self.tag, self.machine_idx) = rows[0] |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 258 | self.job_idx = job_idx |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 259 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 260 | |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 261 | class iteration: |
| 262 | @classmethod |
| 263 | def select(klass, db, where): |
| 264 | fields = ['iteration', 'attribute', 'value'] |
| 265 | iterations = [] |
| 266 | rows = db.select(','.join(fields), 'iteration_result', where) |
| 267 | for row in rows: |
| 268 | iterations.append(klass(*row)) |
| 269 | return iterations |
| 270 | |
| 271 | |
| 272 | def __init__(self, iteration, key, value): |
mbligh | 4a370cf | 2008-04-01 19:56:05 +0000 | [diff] [blame] | 273 | self.iteration = iteration |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 274 | self.key = key |
| 275 | self.value = value |
| 276 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 277 | # class patch: |
| 278 | # def __init__(self): |
| 279 | # self.spec = None |