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 | 27eab24 | 2008-05-21 18:24:10 +0000 | [diff] [blame] | 3 | MAX_RECORDS = 50000L |
| 4 | MAX_CELLS = 500000L |
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): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +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: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +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: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 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.reasons_list = [] |
| 24 | self.job_tag = None |
| 25 | self.job_tag_count = 0 |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 26 | |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 27 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 28 | def add(self, status, count, job_tags, reasons = None): |
| 29 | assert count > 0 |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 30 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 31 | self.job_tag = job_tags |
| 32 | self.job_tag_count += count |
| 33 | if self.job_tag_count > 1: |
| 34 | self.job_tag = None |
| 35 | |
| 36 | self.status_count[status] = count |
| 37 | ### status == 6 means 'GOOD' |
| 38 | if status != 6: |
| 39 | ## None implies sorting problems and extra CRs in a cell |
| 40 | if reasons: |
| 41 | self.reasons_list.append(reasons) |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | class status_data: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 45 | def __init__(self, sql_rows, x_field, y_field, query_reasons = False): |
| 46 | data = {} |
| 47 | y_values = set() |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 48 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 49 | # Walk through the query, filing all results by x, y info |
| 50 | for row in sql_rows: |
| 51 | if query_reasons: |
| 52 | (x,y, status, count, job_tags, reasons) = row |
| 53 | else: |
| 54 | (x,y, status, count, job_tags) = row |
| 55 | reasons = None |
| 56 | if not data.has_key(x): |
| 57 | data[x] = {} |
| 58 | if not data[x].has_key(y): |
| 59 | y_values.add(y) |
| 60 | data[x][y] = status_cell() |
| 61 | data[x][y].add(status, count, job_tags, reasons) |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 62 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 63 | # 2-d hash of data - [x-value][y-value] |
| 64 | self.data = data |
| 65 | # List of possible columns (x-values) |
| 66 | self.x_values = smart_sort(data.keys(), x_field) |
| 67 | # List of rows columns (y-values) |
| 68 | self.y_values = smart_sort(list(y_values), y_field) |
| 69 | nCells = len(self.y_values)*len(self.x_values) |
| 70 | if nCells > MAX_CELLS: |
| 71 | msg = 'Exceeded allowed number of cells in a table' |
| 72 | raise db.MySQLTooManyRows(msg) |
| 73 | |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 74 | |
mbligh | 3126069 | 2008-04-16 23:12:12 +0000 | [diff] [blame] | 75 | def get_matrix_data(db_obj, x_axis, y_axis, where = None, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 76 | query_reasons = False): |
| 77 | # Searches on the test_view table - x_axis and y_axis must both be |
| 78 | # column names in that table. |
| 79 | x_field = test_view_field_dict[x_axis] |
| 80 | y_field = test_view_field_dict[y_axis] |
| 81 | query_fields_list = [x_field, y_field, 'status','COUNT(status)'] |
| 82 | query_fields_list.append("LEFT(GROUP_CONCAT(job_tag),100)") |
| 83 | if query_reasons: |
| 84 | query_fields_list.append( |
| 85 | "LEFT(GROUP_CONCAT(DISTINCT reason SEPARATOR '|'),500)" |
| 86 | ) |
| 87 | fields = ','.join(query_fields_list) |
mbligh | 3126069 | 2008-04-16 23:12:12 +0000 | [diff] [blame] | 88 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 89 | group_by = '%s, %s, status' % (x_field, y_field) |
| 90 | rows = db_obj.select(fields, 'test_view', |
| 91 | where=where, group_by=group_by, max_rows = MAX_RECORDS) |
| 92 | return status_data(rows, x_field, y_field, query_reasons) |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 93 | |
| 94 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 95 | # Dictionary used simply for fast lookups from short reference names for users |
| 96 | # to fieldnames in test_view |
| 97 | test_view_field_dict = { |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 98 | 'kernel' : 'kernel_printable', |
| 99 | 'hostname' : 'machine_hostname', |
| 100 | 'test' : 'test', |
| 101 | 'label' : 'job_label', |
| 102 | 'machine_group' : 'machine_group', |
| 103 | 'reason' : 'reason', |
| 104 | 'tag' : 'job_tag', |
| 105 | 'user' : 'job_username', |
| 106 | 'status' : 'status_word', |
| 107 | 'time' : 'test_finished_time', |
mbligh | 8d88a6d | 2009-02-05 21:37:12 +0000 | [diff] [blame] | 108 | 'start_time' : 'test_started_time', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 109 | 'time_daily' : 'DATE(test_finished_time)' |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 110 | } |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 111 | |
mbligh | 3126069 | 2008-04-16 23:12:12 +0000 | [diff] [blame] | 112 | |
mbligh | 2ba3e73 | 2008-01-16 01:30:19 +0000 | [diff] [blame] | 113 | def smart_sort(list, field): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 114 | if field == 'kernel_printable': |
| 115 | def kernel_encode(kernel): |
| 116 | return kernel_versions.version_encode(kernel) |
| 117 | list.sort(key = kernel_encode, reverse = True) |
| 118 | return list |
| 119 | ## old records may contain time=None |
| 120 | ## make None comparable with timestamp datetime or date |
| 121 | elif field == 'test_finished_time': |
| 122 | def convert_None_to_datetime(date_time): |
| 123 | if not date_time: |
| 124 | return datetime.datetime(1970, 1, 1, 0, 0, 0) |
| 125 | else: |
| 126 | return date_time |
| 127 | list = map(convert_None_to_datetime, list) |
| 128 | elif field == 'DATE(test_finished_time)': |
| 129 | def convert_None_to_date(date): |
| 130 | if not date: |
| 131 | return datetime.date(1970, 1, 1) |
| 132 | else: |
| 133 | return date |
| 134 | list = map(convert_None_to_date, list) |
| 135 | list.sort() |
| 136 | return list |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 137 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 138 | |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 139 | class group: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 140 | @classmethod |
| 141 | def select(klass, db): |
| 142 | """Return all possible machine groups""" |
| 143 | rows = db.select('distinct machine_group', 'machines', |
| 144 | 'machine_group is not null') |
| 145 | groupnames = sorted([row[0] for row in rows]) |
| 146 | return [klass(db, groupname) for groupname in groupnames] |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 147 | |
| 148 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 149 | def __init__(self, db, name): |
| 150 | self.name = name |
| 151 | self.db = db |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 152 | |
| 153 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 154 | def machines(self): |
| 155 | return machine.select(self.db, { 'machine_group' : self.name }) |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 156 | |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 157 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 158 | def tests(self, where = {}): |
| 159 | values = [self.name] |
| 160 | sql = 't inner join machines m on m.machine_idx=t.machine_idx' |
| 161 | sql += ' where m.machine_group=%s' |
| 162 | for key in where.keys(): |
| 163 | sql += ' and %s=%%s' % key |
| 164 | values.append(where[key]) |
| 165 | return test.select_sql(self.db, sql, values) |
mbligh | cff2d21 | 2007-10-07 00:11:10 +0000 | [diff] [blame] | 166 | |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 167 | |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 168 | class machine: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 169 | @classmethod |
| 170 | def select(klass, db, where = {}): |
| 171 | fields = ['machine_idx', 'hostname', 'machine_group', 'owner'] |
| 172 | machines = [] |
| 173 | for row in db.select(','.join(fields), 'machines', where): |
| 174 | machines.append(klass(db, *row)) |
| 175 | return machines |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 176 | |
| 177 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 178 | def __init__(self, db, idx, hostname, group, owner): |
| 179 | self.db = db |
| 180 | self.idx = idx |
| 181 | self.hostname = hostname |
| 182 | self.group = group |
| 183 | self.owner = owner |
mbligh | 2e4e5df | 2007-11-05 17:22:46 +0000 | [diff] [blame] | 184 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 185 | |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 186 | class kernel: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 187 | @classmethod |
| 188 | def select(klass, db, where = {}): |
| 189 | fields = ['kernel_idx', 'kernel_hash', 'base', 'printable'] |
| 190 | rows = db.select(','.join(fields), 'kernels', where) |
| 191 | return [klass(db, *row) for row in rows] |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 192 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 193 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 194 | def __init__(self, db, idx, hash, base, printable): |
| 195 | self.db = db |
| 196 | self.idx = idx |
| 197 | self.hash = hash |
| 198 | self.base = base |
| 199 | self.printable = printable |
| 200 | self.patches = [] # THIS SHOULD PULL IN PATCHES! |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | class test: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 204 | @classmethod |
| 205 | def select(klass, db, where = {}, wherein = {}, distinct = False): |
| 206 | fields = ['test_idx', 'job_idx', 'test', 'subdir', |
| 207 | 'kernel_idx', 'status', 'reason', 'machine_idx'] |
| 208 | tests = [] |
| 209 | for row in db.select(','.join(fields), 'tests', where, |
| 210 | wherein,distinct): |
| 211 | tests.append(klass(db, *row)) |
| 212 | return tests |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 213 | |
| 214 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 215 | @classmethod |
| 216 | def select_sql(klass, db, sql, values): |
| 217 | fields = ['test_idx', 'job_idx', 'test', 'subdir', |
| 218 | 'kernel_idx', 'status', 'reason', 'machine_idx'] |
| 219 | fields = ['t.'+field for field in fields] |
| 220 | rows = db.select_sql(','.join(fields), 'tests', sql, values) |
| 221 | return [klass(db, *row) for row in rows] |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 222 | |
mbligh | 50a2525 | 2007-09-27 15:26:17 +0000 | [diff] [blame] | 223 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 224 | def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, |
| 225 | status_num, reason, machine_idx): |
| 226 | self.idx = test_idx |
| 227 | self.job = job(db, job_idx) |
| 228 | self.testname = testname |
| 229 | self.subdir = subdir |
| 230 | self.kernel_idx = kernel_idx |
| 231 | self.__kernel = None |
| 232 | self.__iterations = None |
| 233 | self.machine_idx = machine_idx |
| 234 | self.__machine = None |
| 235 | self.status_num = status_num |
| 236 | self.status_word = db.status_word[status_num] |
| 237 | self.reason = reason |
| 238 | self.db = db |
| 239 | if self.subdir: |
| 240 | self.url = html_root + self.job.tag + '/' + self.subdir |
| 241 | else: |
| 242 | self.url = None |
mbligh | 50a2525 | 2007-09-27 15:26:17 +0000 | [diff] [blame] | 243 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 244 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 245 | def iterations(self): |
| 246 | """ |
| 247 | Caching function for iterations |
| 248 | """ |
| 249 | if not self.__iterations: |
| 250 | self.__iterations = {} |
| 251 | # A dictionary - dict{key} = [value1, value2, ....] |
| 252 | where = {'test_idx' : self.idx} |
| 253 | for i in iteration.select(self.db, where): |
| 254 | if self.__iterations.has_key(i.key): |
| 255 | self.__iterations[i.key].append(i.value) |
| 256 | else: |
| 257 | self.__iterations[i.key] = [i.value] |
| 258 | return self.__iterations |
| 259 | |
| 260 | |
| 261 | def kernel(self): |
| 262 | """ |
| 263 | Caching function for kernels |
| 264 | """ |
| 265 | if not self.__kernel: |
| 266 | where = {'kernel_idx' : self.kernel_idx} |
| 267 | self.__kernel = kernel.select(self.db, where)[0] |
| 268 | return self.__kernel |
| 269 | |
| 270 | |
| 271 | def machine(self): |
| 272 | """ |
| 273 | Caching function for kernels |
| 274 | """ |
| 275 | if not self.__machine: |
| 276 | where = {'machine_idx' : self.machine_idx} |
| 277 | self.__machine = machine.select(self.db, where)[0] |
| 278 | return self.__machine |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 279 | |
| 280 | |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 281 | class job: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 282 | def __init__(self, db, job_idx): |
| 283 | where = {'job_idx' : job_idx} |
| 284 | rows = db.select('tag, machine_idx', 'jobs', where) |
mbligh | b33e53e | 2008-06-17 19:41:26 +0000 | [diff] [blame] | 285 | if rows: |
| 286 | self.tag, self.machine_idx = rows[0] |
| 287 | self.job_idx = job_idx |
mbligh | 250300e | 2007-09-18 00:50:57 +0000 | [diff] [blame] | 288 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 289 | |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 290 | class iteration: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 291 | @classmethod |
| 292 | def select(klass, db, where): |
| 293 | fields = ['iteration', 'attribute', 'value'] |
| 294 | iterations = [] |
| 295 | rows = db.select(','.join(fields), 'iteration_result', where) |
| 296 | for row in rows: |
| 297 | iterations.append(klass(*row)) |
| 298 | return iterations |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 299 | |
| 300 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 301 | def __init__(self, iteration, key, value): |
| 302 | self.iteration = iteration |
| 303 | self.key = key |
| 304 | self.value = value |
mbligh | 16ae926 | 2007-09-21 00:53:08 +0000 | [diff] [blame] | 305 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 306 | # class patch: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 307 | # def __init__(self): |
| 308 | # self.spec = None |