blob: 28d66bab33246211a2c38ff7a13541a73d15749e [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
mbligh2e4e5df2007-11-05 17:22:46 +000013
14tko = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
15sys.path.insert(0, tko)
16
mbligh2b672532007-11-05 19:24:51 +000017import display, frontend, db, query_lib
mbligh12eebfa2008-01-03 02:01:53 +000018client_bin = os.path.abspath(os.path.join(tko, '../client/bin'))
19sys.path.insert(0, client_bin)
20import kernel_versions
21
mbligh2e4e5df2007-11-05 17:22:46 +000022
mbligh190a81d2007-11-05 20:40:38 +000023html_header = """\
24<form action="compose_query.cgi" method="get">
25<table border="0">
26<tr>
27 <td>Column: </td>
28 <td>Row: </td>
29 <td>Condition: </td>
30 <td align="center"><a href="index.html">Help</a></td>
31</tr>
32<tr>
33 <td>
34 <SELECT NAME="columns">
35 %s
36 </SELECT>
37 </td>
38 <td>
39 <SELECT NAME="rows">
40 %s
41 </SELECT>
42 </td>
43 <td>
mbligh12eebfa2008-01-03 02:01:53 +000044 <input type="text" name="condition" size="30" maxlength="200" value="%s">
mbligh190a81d2007-11-05 20:40:38 +000045 <input type="hidden" name="title" value="Report">
46 </td>
47 <td align="center"><input type="submit" value="Submit">
48 </td>
49</tr>
50</table>
51</form>
52"""
53
mbligh12eebfa2008-01-03 02:01:53 +000054
55# dictionary used simply for fast lookups
56field_dict = {
57 'kernel': 'kernel_printable',
58 'hostname': 'machine_hostname',
59 'test': 'test',
60 'label': 'job_label',
61 'machine_group': 'machine_group',
62 'reason': 'reason',
63 'tag': 'job_tag',
64 'user': 'job_username',
65 'status': 'status_word',
66}
67
mbligh5dd503b2008-01-03 16:35:27 +000068next_field = {
69 'machine_group': 'hostname',
70 'hostname': 'tag',
71 'tag': 'tag',
72
73 'kernel': 'test',
74 'test': 'label',
75 'label': 'label',
76
77 'reason': 'reason',
78 'user': 'user',
79 'status': 'status',
80}
81
82
mbligh12eebfa2008-01-03 02:01:53 +000083
84def parse_field(form, form_field, field_default):
85 if not form_field in form:
86 return field_default
87 field_input = form[form_field].value.lower()
88 if field_input and field_input in field_dict:
89 return field_input
90 return field_default
91
92
93def parse_condition(form, form_field, field_default):
94 if not form_field in form:
95 return field_default
96 return form[form_field].value
97
98
99form = cgi.FieldStorage()
100row_field = parse_field(form, 'rows', 'kernel')
101column_field = parse_field(form, 'columns', 'machine_group')
102condition_field = parse_condition(form, 'condition', '')
mbligh190a81d2007-11-05 20:40:38 +0000103
mbligh2e4e5df2007-11-05 17:22:46 +0000104cgitb.enable()
105db = db.db()
106
mbligh12eebfa2008-01-03 02:01:53 +0000107
108def get_value(test, field):
109 if field == 'kernel':
110 return test.kernel_printable
111 if field == 'hostname':
112 return test.machine_hostname
113 if field == 'test':
114 return test.testname
115 if field == 'label':
116 return test.job_label
117 if field == 'machine_group':
118 return test.machine_group
119 if field == 'reason':
120 return test.reason
121 raise "Unknown field"
122
123
mbligh5dd503b2008-01-03 16:35:27 +0000124def construct_link(row_val, column_val):
125 next_row = row_field
126 next_column = column_field
127 condition_list = []
128 if condition_field != '':
129 condition_list.append(condition_field)
130 if row_val:
131 next_row = next_field[row_field]
132 condition_list.append('%s%s%s%s' % (row_field, '%3D%27',
133 row_val, '%27'))
134 if column_val:
135 next_column = next_field[column_field]
136 condition_list.append('%s%s%s%s' % (column_field, '%3D%27',
137 column_val, '%27'))
138 next_condition = '%26'.join(condition_list)
139 return 'compose_query.cgi?columns=%s&rows=%s&condition=%s' % (
140 next_column, next_row, next_condition)
141
142
mbligh12eebfa2008-01-03 02:01:53 +0000143def create_select_options(selected_val):
mbligh190a81d2007-11-05 20:40:38 +0000144 ret = ""
mbligh190a81d2007-11-05 20:40:38 +0000145
mbligh12eebfa2008-01-03 02:01:53 +0000146 for option in sorted(field_dict.keys()):
mbligh190a81d2007-11-05 20:40:38 +0000147 if selected_val == option:
148 selected = " SELECTED"
149 else:
150 selected = ""
151
152 ret += '<OPTION VALUE="%s"%s>%s</OPTION>\n' % (option,
153 selected,
154 option)
155
156 return ret
157
158
mbligh12eebfa2008-01-03 02:01:53 +0000159def smart_sort(list, field):
160 if field == 'kernel':
161 def kernel_encode(kernel):
162 return kernel_versions.version_encode(kernel)
163 list.sort(key = kernel_encode, reverse = True)
164 else:
165 list.sort()
166
167
168def gen_matrix():
mbligh1405f4e2007-11-05 19:26:23 +0000169 display.print_main_header()
mbligh2e4e5df2007-11-05 17:22:46 +0000170
mbligh12eebfa2008-01-03 02:01:53 +0000171 where = None
172 if condition_field.strip() != '':
173 where = query_lib.parse_scrub_and_gen_condition(
174 condition_field, field_dict)
175 print "<!-- where clause: %s -->" % (where,)
mbligh2e4e5df2007-11-05 17:22:46 +0000176
mbligh12eebfa2008-01-03 02:01:53 +0000177 ret = frontend.get_matrix_data(db, field_dict[column_field],
178 field_dict[row_field], where)
mbligh5dd503b2008-01-03 16:35:27 +0000179 (data, column_list, row_list, stat_list, job_tags) = ret
mbligh2e4e5df2007-11-05 17:22:46 +0000180
mbligh12eebfa2008-01-03 02:01:53 +0000181 if not row_list:
182 msg = "There are no results for this query (yet?)."
183 return [[display.box(msg)]]
mbligh2e4e5df2007-11-05 17:22:46 +0000184
mbligh12eebfa2008-01-03 02:01:53 +0000185 smart_sort(row_list, row_field)
186 smart_sort(column_list, column_field)
mbligh2e4e5df2007-11-05 17:22:46 +0000187
mbligh5dd503b2008-01-03 16:35:27 +0000188 link = 'compose_query.cgi?columns=%s&rows=%s&condition=%s' % (
189 row_field, column_field, condition_field)
190 header_row = [display.box("<center>(Flip Axis)</center>", link=link)]
191
mbligh12eebfa2008-01-03 02:01:53 +0000192 for column in column_list:
mbligh5dd503b2008-01-03 16:35:27 +0000193 link = construct_link(None, column)
194 header_row.append(display.box(column, header=True, link=link))
mbligh2e4e5df2007-11-05 17:22:46 +0000195
196 matrix = [header_row]
mbligh12eebfa2008-01-03 02:01:53 +0000197 for row in row_list:
mbligh5dd503b2008-01-03 16:35:27 +0000198 link = construct_link(row, None)
199 cur_row = [display.box(row, header=True, link=link)]
mbligh12eebfa2008-01-03 02:01:53 +0000200 for column in column_list:
201 try:
202 box_data = data[column][row]
203 except:
204 cur_row.append(display.box(None, None))
205 continue
mbligh5dd503b2008-01-03 16:35:27 +0000206 job_tag = job_tags[column][row]
207 if job_tag:
208 link = '/results/%s/' % job_tag
209 else:
210 link = construct_link(row, column)
mbligh12eebfa2008-01-03 02:01:53 +0000211 cur_row.append(display.status_precounted_box(db,
mbligh5dd503b2008-01-03 16:35:27 +0000212 box_data,
213 link))
mbligh12eebfa2008-01-03 02:01:53 +0000214 matrix.append(cur_row)
mbligh2e4e5df2007-11-05 17:22:46 +0000215
mbligh12eebfa2008-01-03 02:01:53 +0000216 return matrix
mbligh2b672532007-11-05 19:24:51 +0000217
mbligh2b672532007-11-05 19:24:51 +0000218
mbligh12eebfa2008-01-03 02:01:53 +0000219def main():
mbligh190a81d2007-11-05 20:40:38 +0000220 # create the actual page
mbligh190a81d2007-11-05 20:40:38 +0000221 print '<html><head><title>'
222 print 'Filtered Autotest Results'
223 print '</title></head><body>'
mbligh12eebfa2008-01-03 02:01:53 +0000224 print html_header % (create_select_options(column_field),
225 create_select_options(row_field),
226 condition_field)
227 display.print_table(gen_matrix())
mbligh190a81d2007-11-05 20:40:38 +0000228 print '</body></html>'
mbligh2e4e5df2007-11-05 17:22:46 +0000229
230
231main()