blob: 70ee2fc7d021acbccb4a36a3a7a04f3d6372bf36 [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',
mbligh8d88a6d2009-02-05 21:37:12 +0000108 'start_time' : 'test_started_time',
jadmanski0afbb632008-06-06 21:10:57 +0000109 'time_daily' : 'DATE(test_finished_time)'
mbligh2ba3e732008-01-16 01:30:19 +0000110}
mbligh2b672532007-11-05 19:24:51 +0000111
mbligh31260692008-04-16 23:12:12 +0000112
mbligh2ba3e732008-01-16 01:30:19 +0000113def smart_sort(list, field):
jadmanski0afbb632008-06-06 21:10:57 +0000114 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
mbligh2e4e5df2007-11-05 17:22:46 +0000137
mbligh2aaeb672007-10-01 14:54:18 +0000138
mblighcff2d212007-10-07 00:11:10 +0000139class group:
jadmanski0afbb632008-06-06 21:10:57 +0000140 @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]
mbligh83f63a02007-12-12 19:13:04 +0000147
148
jadmanski0afbb632008-06-06 21:10:57 +0000149 def __init__(self, db, name):
150 self.name = name
151 self.db = db
mblighcff2d212007-10-07 00:11:10 +0000152
153
jadmanski0afbb632008-06-06 21:10:57 +0000154 def machines(self):
155 return machine.select(self.db, { 'machine_group' : self.name })
mbligh2e4e5df2007-11-05 17:22:46 +0000156
mblighcff2d212007-10-07 00:11:10 +0000157
jadmanski0afbb632008-06-06 21:10:57 +0000158 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)
mblighcff2d212007-10-07 00:11:10 +0000166
mbligh2e4e5df2007-11-05 17:22:46 +0000167
mbligh2aaeb672007-10-01 14:54:18 +0000168class machine:
jadmanski0afbb632008-06-06 21:10:57 +0000169 @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
mbligh2aaeb672007-10-01 14:54:18 +0000176
177
jadmanski0afbb632008-06-06 21:10:57 +0000178 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
mbligh2e4e5df2007-11-05 17:22:46 +0000184
mbligh250300e2007-09-18 00:50:57 +0000185
mbligh9bb92fe2007-09-12 15:54:23 +0000186class kernel:
jadmanski0afbb632008-06-06 21:10:57 +0000187 @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]
mbligh9bb92fe2007-09-12 15:54:23 +0000192
mbligh8e1ab172007-09-13 17:29:56 +0000193
jadmanski0afbb632008-06-06 21:10:57 +0000194 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!
mbligh9bb92fe2007-09-12 15:54:23 +0000201
202
203class test:
jadmanski0afbb632008-06-06 21:10:57 +0000204 @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
mbligh8e1ab172007-09-13 17:29:56 +0000213
214
jadmanski0afbb632008-06-06 21:10:57 +0000215 @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]
mbligh16ae9262007-09-21 00:53:08 +0000222
mbligh50a25252007-09-27 15:26:17 +0000223
jadmanski0afbb632008-06-06 21:10:57 +0000224 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
mbligh50a25252007-09-27 15:26:17 +0000243
mbligh250300e2007-09-18 00:50:57 +0000244
jadmanski0afbb632008-06-06 21:10:57 +0000245 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
mbligh2aaeb672007-10-01 14:54:18 +0000279
280
mbligh250300e2007-09-18 00:50:57 +0000281class job:
jadmanski0afbb632008-06-06 21:10:57 +0000282 def __init__(self, db, job_idx):
283 where = {'job_idx' : job_idx}
284 rows = db.select('tag, machine_idx', 'jobs', where)
mblighb33e53e2008-06-17 19:41:26 +0000285 if rows:
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