blob: 45de298fecf5a77ab72b1fa263e544a571d09fa1 [file] [log] [blame]
mbligh9bb92fe2007-09-12 15:54:23 +00001#!/usr/bin/python
mbligh4a370cf2008-04-01 19:56:05 +00002import os, re, db, sys, datetime
mbligh27eab242008-05-21 18:24:10 +00003MAX_RECORDS = 50000L
4MAX_CELLS = 500000L
mbligh9bb92fe2007-09-12 15:54:23 +00005
mbligh2ba3e732008-01-16 01:30:19 +00006tko = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
7client_bin = os.path.abspath(os.path.join(tko, '../client/bin'))
8sys.path.insert(0, client_bin)
9import kernel_versions
10
mbligh2aaeb672007-10-01 14:54:18 +000011root_url_file = os.path.join(tko, '.root_url')
12if os.path.exists(root_url_file):
jadmanski0afbb632008-06-06 21:10:57 +000013 html_root = open(root_url_file, 'r').readline().rstrip()
mbligh2aaeb672007-10-01 14:54:18 +000014else:
jadmanski0afbb632008-06-06 21:10:57 +000015 html_root = '/results/'
mbligh2aaeb672007-10-01 14:54:18 +000016
mbligh2e4e5df2007-11-05 17:22:46 +000017
mbligh2ba3e732008-01-16 01:30:19 +000018class status_cell:
jadmanski0afbb632008-06-06 21:10:57 +000019 # 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
mbligh2e4e5df2007-11-05 17:22:46 +000026
mbligh83f63a02007-12-12 19:13:04 +000027
jadmanski0afbb632008-06-06 21:10:57 +000028 def add(self, status, count, job_tags, reasons = None):
29 assert count > 0
mbligh2ba3e732008-01-16 01:30:19 +000030
jadmanski0afbb632008-06-06 21:10:57 +000031 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)
mbligh2ba3e732008-01-16 01:30:19 +000042
43
44class status_data:
jadmanski0afbb632008-06-06 21:10:57 +000045 def __init__(self, sql_rows, x_field, y_field, query_reasons = False):
46 data = {}
47 y_values = set()
mbligh2ba3e732008-01-16 01:30:19 +000048
jadmanski0afbb632008-06-06 21:10:57 +000049 # 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)
mbligh2ba3e732008-01-16 01:30:19 +000062
jadmanski0afbb632008-06-06 21:10:57 +000063 # 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
mbligh83f63a02007-12-12 19:13:04 +000074
mbligh31260692008-04-16 23:12:12 +000075def get_matrix_data(db_obj, x_axis, y_axis, where = None,
jadmanski0afbb632008-06-06 21:10:57 +000076 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)
mbligh31260692008-04-16 23:12:12 +000088
jadmanski0afbb632008-06-06 21:10:57 +000089 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)
mbligh83f63a02007-12-12 19:13:04 +000093
94
mbligh2ba3e732008-01-16 01:30:19 +000095# Dictionary used simply for fast lookups from short reference names for users
96# to fieldnames in test_view
97test_view_field_dict = {
jadmanski0afbb632008-06-06 21:10:57 +000098 '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',
108 'time_daily' : 'DATE(test_finished_time)'
mbligh2ba3e732008-01-16 01:30:19 +0000109}
mbligh2b672532007-11-05 19:24:51 +0000110
mbligh31260692008-04-16 23:12:12 +0000111
mbligh2ba3e732008-01-16 01:30:19 +0000112def smart_sort(list, field):
jadmanski0afbb632008-06-06 21:10:57 +0000113 if field == 'kernel_printable':
114 def kernel_encode(kernel):
115 return kernel_versions.version_encode(kernel)
116 list.sort(key = kernel_encode, reverse = True)
117 return list
118 ## old records may contain time=None
119 ## make None comparable with timestamp datetime or date
120 elif field == 'test_finished_time':
121 def convert_None_to_datetime(date_time):
122 if not date_time:
123 return datetime.datetime(1970, 1, 1, 0, 0, 0)
124 else:
125 return date_time
126 list = map(convert_None_to_datetime, list)
127 elif field == 'DATE(test_finished_time)':
128 def convert_None_to_date(date):
129 if not date:
130 return datetime.date(1970, 1, 1)
131 else:
132 return date
133 list = map(convert_None_to_date, list)
134 list.sort()
135 return list
mbligh2e4e5df2007-11-05 17:22:46 +0000136
mbligh2aaeb672007-10-01 14:54:18 +0000137
mblighcff2d212007-10-07 00:11:10 +0000138class group:
jadmanski0afbb632008-06-06 21:10:57 +0000139 @classmethod
140 def select(klass, db):
141 """Return all possible machine groups"""
142 rows = db.select('distinct machine_group', 'machines',
143 'machine_group is not null')
144 groupnames = sorted([row[0] for row in rows])
145 return [klass(db, groupname) for groupname in groupnames]
mbligh83f63a02007-12-12 19:13:04 +0000146
147
jadmanski0afbb632008-06-06 21:10:57 +0000148 def __init__(self, db, name):
149 self.name = name
150 self.db = db
mblighcff2d212007-10-07 00:11:10 +0000151
152
jadmanski0afbb632008-06-06 21:10:57 +0000153 def machines(self):
154 return machine.select(self.db, { 'machine_group' : self.name })
mbligh2e4e5df2007-11-05 17:22:46 +0000155
mblighcff2d212007-10-07 00:11:10 +0000156
jadmanski0afbb632008-06-06 21:10:57 +0000157 def tests(self, where = {}):
158 values = [self.name]
159 sql = 't inner join machines m on m.machine_idx=t.machine_idx'
160 sql += ' where m.machine_group=%s'
161 for key in where.keys():
162 sql += ' and %s=%%s' % key
163 values.append(where[key])
164 return test.select_sql(self.db, sql, values)
mblighcff2d212007-10-07 00:11:10 +0000165
mbligh2e4e5df2007-11-05 17:22:46 +0000166
mbligh2aaeb672007-10-01 14:54:18 +0000167class machine:
jadmanski0afbb632008-06-06 21:10:57 +0000168 @classmethod
169 def select(klass, db, where = {}):
170 fields = ['machine_idx', 'hostname', 'machine_group', 'owner']
171 machines = []
172 for row in db.select(','.join(fields), 'machines', where):
173 machines.append(klass(db, *row))
174 return machines
mbligh2aaeb672007-10-01 14:54:18 +0000175
176
jadmanski0afbb632008-06-06 21:10:57 +0000177 def __init__(self, db, idx, hostname, group, owner):
178 self.db = db
179 self.idx = idx
180 self.hostname = hostname
181 self.group = group
182 self.owner = owner
mbligh2e4e5df2007-11-05 17:22:46 +0000183
mbligh250300e2007-09-18 00:50:57 +0000184
mbligh9bb92fe2007-09-12 15:54:23 +0000185class kernel:
jadmanski0afbb632008-06-06 21:10:57 +0000186 @classmethod
187 def select(klass, db, where = {}):
188 fields = ['kernel_idx', 'kernel_hash', 'base', 'printable']
189 rows = db.select(','.join(fields), 'kernels', where)
190 return [klass(db, *row) for row in rows]
mbligh9bb92fe2007-09-12 15:54:23 +0000191
mbligh8e1ab172007-09-13 17:29:56 +0000192
jadmanski0afbb632008-06-06 21:10:57 +0000193 def __init__(self, db, idx, hash, base, printable):
194 self.db = db
195 self.idx = idx
196 self.hash = hash
197 self.base = base
198 self.printable = printable
199 self.patches = [] # THIS SHOULD PULL IN PATCHES!
mbligh9bb92fe2007-09-12 15:54:23 +0000200
201
202class test:
jadmanski0afbb632008-06-06 21:10:57 +0000203 @classmethod
204 def select(klass, db, where = {}, wherein = {}, distinct = False):
205 fields = ['test_idx', 'job_idx', 'test', 'subdir',
206 'kernel_idx', 'status', 'reason', 'machine_idx']
207 tests = []
208 for row in db.select(','.join(fields), 'tests', where,
209 wherein,distinct):
210 tests.append(klass(db, *row))
211 return tests
mbligh8e1ab172007-09-13 17:29:56 +0000212
213
jadmanski0afbb632008-06-06 21:10:57 +0000214 @classmethod
215 def select_sql(klass, db, sql, values):
216 fields = ['test_idx', 'job_idx', 'test', 'subdir',
217 'kernel_idx', 'status', 'reason', 'machine_idx']
218 fields = ['t.'+field for field in fields]
219 rows = db.select_sql(','.join(fields), 'tests', sql, values)
220 return [klass(db, *row) for row in rows]
mbligh16ae9262007-09-21 00:53:08 +0000221
mbligh50a25252007-09-27 15:26:17 +0000222
jadmanski0afbb632008-06-06 21:10:57 +0000223 def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx,
224 status_num, reason, machine_idx):
225 self.idx = test_idx
226 self.job = job(db, job_idx)
227 self.testname = testname
228 self.subdir = subdir
229 self.kernel_idx = kernel_idx
230 self.__kernel = None
231 self.__iterations = None
232 self.machine_idx = machine_idx
233 self.__machine = None
234 self.status_num = status_num
235 self.status_word = db.status_word[status_num]
236 self.reason = reason
237 self.db = db
238 if self.subdir:
239 self.url = html_root + self.job.tag + '/' + self.subdir
240 else:
241 self.url = None
mbligh50a25252007-09-27 15:26:17 +0000242
mbligh250300e2007-09-18 00:50:57 +0000243
jadmanski0afbb632008-06-06 21:10:57 +0000244 def iterations(self):
245 """
246 Caching function for iterations
247 """
248 if not self.__iterations:
249 self.__iterations = {}
250 # A dictionary - dict{key} = [value1, value2, ....]
251 where = {'test_idx' : self.idx}
252 for i in iteration.select(self.db, where):
253 if self.__iterations.has_key(i.key):
254 self.__iterations[i.key].append(i.value)
255 else:
256 self.__iterations[i.key] = [i.value]
257 return self.__iterations
258
259
260 def kernel(self):
261 """
262 Caching function for kernels
263 """
264 if not self.__kernel:
265 where = {'kernel_idx' : self.kernel_idx}
266 self.__kernel = kernel.select(self.db, where)[0]
267 return self.__kernel
268
269
270 def machine(self):
271 """
272 Caching function for kernels
273 """
274 if not self.__machine:
275 where = {'machine_idx' : self.machine_idx}
276 self.__machine = machine.select(self.db, where)[0]
277 return self.__machine
mbligh2aaeb672007-10-01 14:54:18 +0000278
279
mbligh250300e2007-09-18 00:50:57 +0000280class job:
jadmanski0afbb632008-06-06 21:10:57 +0000281 def __init__(self, db, job_idx):
282 where = {'job_idx' : job_idx}
283 rows = db.select('tag, machine_idx', 'jobs', where)
284 if not rows:
285 return None
286 (self.tag, self.machine_idx) = rows[0]
287 self.job_idx = job_idx
mbligh250300e2007-09-18 00:50:57 +0000288
jadmanski0afbb632008-06-06 21:10:57 +0000289
mbligh16ae9262007-09-21 00:53:08 +0000290class iteration:
jadmanski0afbb632008-06-06 21:10:57 +0000291 @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
mbligh16ae9262007-09-21 00:53:08 +0000299
300
jadmanski0afbb632008-06-06 21:10:57 +0000301 def __init__(self, iteration, key, value):
302 self.iteration = iteration
303 self.key = key
304 self.value = value
mbligh16ae9262007-09-21 00:53:08 +0000305
mbligh8e1ab172007-09-13 17:29:56 +0000306# class patch:
jadmanski0afbb632008-06-06 21:10:57 +0000307# def __init__(self):
308# self.spec = None