1#!/usr/bin/python
2print "Content-type: text/html\n"
3import cgi, cgitb, os, sys, re
4sys.stdout.flush()
5cgitb.enable()
6
7import common
8from autotest_lib.tko import db, display, frontend
9
10db = db.db()
11
12benchmark_key = {
13'kernbench' : ["elapsed"],
14'dbench' : ["throughput"],
15'tbench' : ["throughput"],
16}
17
18def main():
19    display.print_main_header()
20    ## it is table only; mouse hovering off
21    display.set_brief_mode()
22
23    ## getting available tests
24    rows = db.select('test', 'tko_tests', {}, distinct=True)
25    all_benchmarks = []
26    for row in rows:
27        benchmark = row[0]
28        testname = re.sub(r'\..*', '', benchmark)
29        all_benchmarks.append(benchmark)
30    all_benchmarks = display.sort_tests(all_benchmarks)
31
32    available_params = set()
33    for benchmark in all_benchmarks:
34        fields_tests = 'test_idx, count(status_word)'
35        where_tests = { 'subdir': benchmark, 'status_word' : 'GOOD' }
36        fields_params = 'attribute'
37        for (id, count) in db.select(fields_tests, 'tko_test_view',
38                                     where_tests, group_by='machine_hostname'):
39            where_params = {'test_idx': id}
40            for (attribute) in db.select(fields_params, 'tko_iteration_result',
41                                         where_params):
42                available_params.add("%s - %s" % (benchmark,
43                                                     attribute[0]))
44    available_params = list(available_params)
45    #process form submit
46    cleared = ""
47    attributes = ""
48    params = []
49    attr = cgi.FieldStorage()
50    if attr.has_key("cleared"):
51        cleared = attr["cleared"].value
52    if attr.has_key("reset"):
53        cleared = ""
54    if attr.has_key("clear") or cleared == "true":
55        benchmark_key.clear()
56        cleared = "true"
57    else:
58        attributes = "|".join(["%s:%s" % (key, value[0]) for key, value in benchmark_key.items()])
59
60    if attr.has_key("add"):
61        val = attr["key"].value.split("-")
62        test = val[0].strip()
63        key = val[1].strip()
64        attributes = attr.getvalue("attributes", "")
65        tk = "%s:%s" % (test, key)
66        if len(attributes) == 0:
67            attributes = tk
68        elif attributes.find(tk) == -1:
69            attributes += "|%s" % (tk)
70
71        params = attributes.split("|")
72
73    print '<h1>Add tests</h1>'
74    display.print_add_test_form(available_params, attributes, cleared)
75
76    #convert params to a dictionary
77    for param in params:
78        test_attributes = param.split(":")
79        if not benchmark_key.has_key(test_attributes[0]):
80            benchmark_key[test_attributes[0]] = []
81        if benchmark_key[test_attributes[0]].count(test_attributes[1]) == 0:
82            benchmark_key[test_attributes[0]].append(test_attributes[1])
83
84    machine_idx = {}
85    benchmark_data = {}
86    for benchmark in benchmark_key:
87        fields = 'machine_idx,machine_hostname,count(status_word)'
88        where = { 'subdir': benchmark, 'status_word' : 'GOOD' }
89        data = {}
90        for (idx, machine, count) in db.select(fields, 'tko_test_view',
91                                            where, group_by='machine_hostname'):
92            data[machine] = count
93            machine_idx[machine] = idx
94        benchmark_data[benchmark] = data
95
96
97    print '<h1>Performance</h1>'
98
99    header_row = [ display.box('Benchmark', header=True) ]
100    for benchmark in benchmark_key:
101        header_row += [ display.box("%s - %s" % (re.sub(r'\.', '<br>', benchmark),key), header=True) for key in benchmark_key[benchmark] ]
102
103    matrix = [header_row]
104    for machine in machine_idx:
105        row = [display.box(machine)]
106        for benchmark in benchmark_key:
107            count = benchmark_data[benchmark].get(machine, None)
108            if not count:
109                row.append(display.box(None))
110                continue
111            for key in benchmark_key[re.sub(r'\..*', '', benchmark)]:
112                url = 'machine_test_attribute_graph.cgi'
113                url += '?machine=' + str(machine_idx[machine])
114                url += '&benchmark=' + benchmark
115                url += '&key=' + key
116                html = '<a href="%s">%d</a>' % (url, count)
117                row.append(display.box(html))
118        matrix.append(row)
119    matrix.append(header_row)
120
121    display.print_table(matrix)
122
123main()
124