blob: 08fec3d7820f9538203f860fea909fe7803ca128 [file] [log] [blame]
mbligh2e4e5df2007-11-05 17:22:46 +00001#!/usr/bin/python
2
3"""
4Selects all rows and columns that satisfy the condition specified
5and draws the matrix. There is a seperate SQL query made for every (x,y)
6in the matrix.
7"""
8
9
10print "Content-type: text/html\n"
11import cgi, cgitb, re
mbligha4266932007-11-05 18:12:16 +000012import sys, os
mblighb180f6c2008-01-04 20:24:41 +000013import urllib
mbligh2e4e5df2007-11-05 17:22:46 +000014
15tko = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
16sys.path.insert(0, tko)
17
mbligh2b672532007-11-05 19:24:51 +000018import display, frontend, db, query_lib
mbligh12eebfa2008-01-03 02:01:53 +000019client_bin = os.path.abspath(os.path.join(tko, '../client/bin'))
20sys.path.insert(0, client_bin)
21import kernel_versions
22
mbligh2e4e5df2007-11-05 17:22:46 +000023
mbligh190a81d2007-11-05 20:40:38 +000024html_header = """\
25<form action="compose_query.cgi" method="get">
26<table border="0">
27<tr>
28 <td>Column: </td>
29 <td>Row: </td>
30 <td>Condition: </td>
31 <td align="center"><a href="index.html">Help</a></td>
32</tr>
33<tr>
34 <td>
35 <SELECT NAME="columns">
36 %s
37 </SELECT>
38 </td>
39 <td>
40 <SELECT NAME="rows">
41 %s
42 </SELECT>
43 </td>
44 <td>
mbligh12eebfa2008-01-03 02:01:53 +000045 <input type="text" name="condition" size="30" maxlength="200" value="%s">
mbligh190a81d2007-11-05 20:40:38 +000046 <input type="hidden" name="title" value="Report">
47 </td>
48 <td align="center"><input type="submit" value="Submit">
49 </td>
50</tr>
51</table>
52</form>
53"""
54
mbligh12eebfa2008-01-03 02:01:53 +000055
56# dictionary used simply for fast lookups
57field_dict = {
58 'kernel': 'kernel_printable',
59 'hostname': 'machine_hostname',
60 'test': 'test',
61 'label': 'job_label',
62 'machine_group': 'machine_group',
63 'reason': 'reason',
64 'tag': 'job_tag',
65 'user': 'job_username',
66 'status': 'status_word',
67}
68
mbligh5dd503b2008-01-03 16:35:27 +000069next_field = {
70 'machine_group': 'hostname',
71 'hostname': 'tag',
72 'tag': 'tag',
73
74 'kernel': 'test',
75 'test': 'label',
76 'label': 'label',
77
78 'reason': 'reason',
79 'user': 'user',
80 'status': 'status',
81}
82
83
mbligh12eebfa2008-01-03 02:01:53 +000084
85def parse_field(form, form_field, field_default):
86 if not form_field in form:
87 return field_default
88 field_input = form[form_field].value.lower()
89 if field_input and field_input in field_dict:
90 return field_input
91 return field_default
92
93
94def parse_condition(form, form_field, field_default):
95 if not form_field in form:
96 return field_default
97 return form[form_field].value
98
99
100form = cgi.FieldStorage()
101row_field = parse_field(form, 'rows', 'kernel')
102column_field = parse_field(form, 'columns', 'machine_group')
103condition_field = parse_condition(form, 'condition', '')
mbligh190a81d2007-11-05 20:40:38 +0000104
mbligh2e4e5df2007-11-05 17:22:46 +0000105cgitb.enable()
106db = db.db()
107
mbligh12eebfa2008-01-03 02:01:53 +0000108
109def get_value(test, field):
110 if field == 'kernel':
111 return test.kernel_printable
112 if field == 'hostname':
113 return test.machine_hostname
114 if field == 'test':
115 return test.testname
116 if field == 'label':
117 return test.job_label
118 if field == 'machine_group':
119 return test.machine_group
120 if field == 'reason':
121 return test.reason
122 raise "Unknown field"
123
124
mbligh5dd503b2008-01-03 16:35:27 +0000125def construct_link(row_val, column_val):
126 next_row = row_field
127 next_column = column_field
128 condition_list = []
129 if condition_field != '':
130 condition_list.append(condition_field)
131 if row_val:
132 next_row = next_field[row_field]
mblighb180f6c2008-01-04 20:24:41 +0000133 condition_list.append("%s='%s'" % (row_field, row_val))
mbligh5dd503b2008-01-03 16:35:27 +0000134 if column_val:
135 next_column = next_field[column_field]
mblighb180f6c2008-01-04 20:24:41 +0000136 condition_list.append("%s='%s'" % (column_field, column_val))
137 next_condition = '&'.join(condition_list)
138 return 'compose_query.cgi?' + urllib.urlencode({'columns': next_column,
139 'rows': next_row, 'condition': next_condition})
mbligh5dd503b2008-01-03 16:35:27 +0000140
141
mbligh12eebfa2008-01-03 02:01:53 +0000142def create_select_options(selected_val):
mbligh190a81d2007-11-05 20:40:38 +0000143 ret = ""
mbligh190a81d2007-11-05 20:40:38 +0000144
mbligh12eebfa2008-01-03 02:01:53 +0000145 for option in sorted(field_dict.keys()):
mbligh190a81d2007-11-05 20:40:38 +0000146 if selected_val == option:
147 selected = " SELECTED"
148 else:
149 selected = ""
150
151 ret += '<OPTION VALUE="%s"%s>%s</OPTION>\n' % (option,
152 selected,
153 option)
154
155 return ret
156
157
mbligh12eebfa2008-01-03 02:01:53 +0000158def smart_sort(list, field):
159 if field == 'kernel':
160 def kernel_encode(kernel):
161 return kernel_versions.version_encode(kernel)
162 list.sort(key = kernel_encode, reverse = True)
163 else:
164 list.sort()
165
166
167def gen_matrix():
mbligh1405f4e2007-11-05 19:26:23 +0000168 display.print_main_header()
mbligh2e4e5df2007-11-05 17:22:46 +0000169
mbligh12eebfa2008-01-03 02:01:53 +0000170 where = None
171 if condition_field.strip() != '':
172 where = query_lib.parse_scrub_and_gen_condition(
173 condition_field, field_dict)
174 print "<!-- where clause: %s -->" % (where,)
mbligh2e4e5df2007-11-05 17:22:46 +0000175
mbligh12eebfa2008-01-03 02:01:53 +0000176 ret = frontend.get_matrix_data(db, field_dict[column_field],
177 field_dict[row_field], where)
mbligh5dd503b2008-01-03 16:35:27 +0000178 (data, column_list, row_list, stat_list, job_tags) = ret
mbligh2e4e5df2007-11-05 17:22:46 +0000179
mbligh12eebfa2008-01-03 02:01:53 +0000180 if not row_list:
181 msg = "There are no results for this query (yet?)."
182 return [[display.box(msg)]]
mbligh2e4e5df2007-11-05 17:22:46 +0000183
mbligh12eebfa2008-01-03 02:01:53 +0000184 smart_sort(row_list, row_field)
185 smart_sort(column_list, column_field)
mbligh2e4e5df2007-11-05 17:22:46 +0000186
mbligh5dd503b2008-01-03 16:35:27 +0000187 link = 'compose_query.cgi?columns=%s&rows=%s&condition=%s' % (
188 row_field, column_field, condition_field)
189 header_row = [display.box("<center>(Flip Axis)</center>", link=link)]
190
mbligh12eebfa2008-01-03 02:01:53 +0000191 for column in column_list:
mbligh5dd503b2008-01-03 16:35:27 +0000192 link = construct_link(None, column)
193 header_row.append(display.box(column, header=True, link=link))
mbligh2e4e5df2007-11-05 17:22:46 +0000194
195 matrix = [header_row]
mbligh12eebfa2008-01-03 02:01:53 +0000196 for row in row_list:
mbligh5dd503b2008-01-03 16:35:27 +0000197 link = construct_link(row, None)
198 cur_row = [display.box(row, header=True, link=link)]
mbligh12eebfa2008-01-03 02:01:53 +0000199 for column in column_list:
200 try:
201 box_data = data[column][row]
202 except:
203 cur_row.append(display.box(None, None))
204 continue
mbligh5dd503b2008-01-03 16:35:27 +0000205 job_tag = job_tags[column][row]
206 if job_tag:
207 link = '/results/%s/' % job_tag
208 else:
209 link = construct_link(row, column)
mbligh12eebfa2008-01-03 02:01:53 +0000210 cur_row.append(display.status_precounted_box(db,
mbligh5dd503b2008-01-03 16:35:27 +0000211 box_data,
212 link))
mbligh12eebfa2008-01-03 02:01:53 +0000213 matrix.append(cur_row)
mbligh2e4e5df2007-11-05 17:22:46 +0000214
mbligh12eebfa2008-01-03 02:01:53 +0000215 return matrix
mbligh2b672532007-11-05 19:24:51 +0000216
mbligh2b672532007-11-05 19:24:51 +0000217
mbligh12eebfa2008-01-03 02:01:53 +0000218def main():
mbligh190a81d2007-11-05 20:40:38 +0000219 # create the actual page
mbligh190a81d2007-11-05 20:40:38 +0000220 print '<html><head><title>'
221 print 'Filtered Autotest Results'
222 print '</title></head><body>'
mbligh12eebfa2008-01-03 02:01:53 +0000223 print html_header % (create_select_options(column_field),
224 create_select_options(row_field),
225 condition_field)
226 display.print_table(gen_matrix())
mbligh190a81d2007-11-05 20:40:38 +0000227 print '</body></html>'
mbligh2e4e5df2007-11-05 17:22:46 +0000228
229
230main()