blob: 96f8489b3ee5f8184dea235301e829921841d634 [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
68
69def parse_field(form, form_field, field_default):
70 if not form_field in form:
71 return field_default
72 field_input = form[form_field].value.lower()
73 if field_input and field_input in field_dict:
74 return field_input
75 return field_default
76
77
78def parse_condition(form, form_field, field_default):
79 if not form_field in form:
80 return field_default
81 return form[form_field].value
82
83
84form = cgi.FieldStorage()
85row_field = parse_field(form, 'rows', 'kernel')
86column_field = parse_field(form, 'columns', 'machine_group')
87condition_field = parse_condition(form, 'condition', '')
mbligh190a81d2007-11-05 20:40:38 +000088
mbligh2e4e5df2007-11-05 17:22:46 +000089cgitb.enable()
90db = db.db()
91
mbligh12eebfa2008-01-03 02:01:53 +000092
93def get_value(test, field):
94 if field == 'kernel':
95 return test.kernel_printable
96 if field == 'hostname':
97 return test.machine_hostname
98 if field == 'test':
99 return test.testname
100 if field == 'label':
101 return test.job_label
102 if field == 'machine_group':
103 return test.machine_group
104 if field == 'reason':
105 return test.reason
106 raise "Unknown field"
107
108
109def create_select_options(selected_val):
mbligh190a81d2007-11-05 20:40:38 +0000110 ret = ""
mbligh190a81d2007-11-05 20:40:38 +0000111
mbligh12eebfa2008-01-03 02:01:53 +0000112 for option in sorted(field_dict.keys()):
mbligh190a81d2007-11-05 20:40:38 +0000113 if selected_val == option:
114 selected = " SELECTED"
115 else:
116 selected = ""
117
118 ret += '<OPTION VALUE="%s"%s>%s</OPTION>\n' % (option,
119 selected,
120 option)
121
122 return ret
123
124
mbligh12eebfa2008-01-03 02:01:53 +0000125def smart_sort(list, field):
126 if field == 'kernel':
127 def kernel_encode(kernel):
128 return kernel_versions.version_encode(kernel)
129 list.sort(key = kernel_encode, reverse = True)
130 else:
131 list.sort()
132
133
134def gen_matrix():
mbligh1405f4e2007-11-05 19:26:23 +0000135 display.print_main_header()
mbligh2e4e5df2007-11-05 17:22:46 +0000136
mbligh12eebfa2008-01-03 02:01:53 +0000137 where = None
138 if condition_field.strip() != '':
139 where = query_lib.parse_scrub_and_gen_condition(
140 condition_field, field_dict)
141 print "<!-- where clause: %s -->" % (where,)
mbligh2e4e5df2007-11-05 17:22:46 +0000142
mbligh12eebfa2008-01-03 02:01:53 +0000143 ret = frontend.get_matrix_data(db, field_dict[column_field],
144 field_dict[row_field], where)
145 (data, column_list, row_list, stat_list) = ret
mbligh2e4e5df2007-11-05 17:22:46 +0000146
mbligh12eebfa2008-01-03 02:01:53 +0000147 if not row_list:
148 msg = "There are no results for this query (yet?)."
149 return [[display.box(msg)]]
mbligh2e4e5df2007-11-05 17:22:46 +0000150
mbligh12eebfa2008-01-03 02:01:53 +0000151 smart_sort(row_list, row_field)
152 smart_sort(column_list, column_field)
mbligh2e4e5df2007-11-05 17:22:46 +0000153
mbligh12eebfa2008-01-03 02:01:53 +0000154 header_row = [display.box("", header=True)]
155 for column in column_list:
156 header_row.append(display.box(column, header=True))
mbligh2e4e5df2007-11-05 17:22:46 +0000157
158 matrix = [header_row]
mbligh12eebfa2008-01-03 02:01:53 +0000159 for row in row_list:
160 cur_row = [display.box(row)]
161 for column in column_list:
162 try:
163 box_data = data[column][row]
164 except:
165 cur_row.append(display.box(None, None))
166 continue
167 cur_row.append(display.status_precounted_box(db,
168 box_data,
169 ""))
170 matrix.append(cur_row)
mbligh2e4e5df2007-11-05 17:22:46 +0000171
mbligh12eebfa2008-01-03 02:01:53 +0000172 return matrix
mbligh2b672532007-11-05 19:24:51 +0000173
mbligh2b672532007-11-05 19:24:51 +0000174
mbligh12eebfa2008-01-03 02:01:53 +0000175def main():
mbligh190a81d2007-11-05 20:40:38 +0000176 # create the actual page
mbligh190a81d2007-11-05 20:40:38 +0000177 print '<html><head><title>'
178 print 'Filtered Autotest Results'
179 print '</title></head><body>'
mbligh12eebfa2008-01-03 02:01:53 +0000180 print html_header % (create_select_options(column_field),
181 create_select_options(row_field),
182 condition_field)
183 display.print_table(gen_matrix())
mbligh190a81d2007-11-05 20:40:38 +0000184 print '</body></html>'
mbligh2e4e5df2007-11-05 17:22:46 +0000185
186
187main()