blob: 4ffef26a3cd1fb6495479687424f3095965bd54d [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#!/usr/bin/python
2"""
3Selects all rows and columns that satisfy the condition specified
4and prints the matrix.
5"""
showardd9e341e2010-01-12 18:56:42 +00006import optparse
mblighbe630eb2008-08-01 16:41:48 +00007import common
8from autotest_lib.cli import rpc
mbligh789da1e2009-04-01 18:35:12 +00009from autotest_lib.database import database_connection
mblighbe630eb2008-08-01 16:41:48 +000010
11
12# First do all the options parsing
13parser = optparse.OptionParser()
showardd9e341e2010-01-12 18:56:42 +000014parser.add_option(
15 '-C', '--columns', action='store', dest='columns',
16 default='test_name,reason,test_started_time,test_finished_time,job_tag,'
17 'job_name,hostname,platform,kernel,status',
18 help='Comma-separated list of column names to display')
19parser.add_option('-w', '--where', action='store', dest='condition',
mbligh8dd5c6c2008-09-04 16:46:34 +000020 help=("The WHERE condition for the query witten in the 'new style' "
mbligh28afa732008-09-30 20:29:46 +000021 "condition syntax for new tko (see "
showardd9e341e2010-01-12 18:56:42 +000022 "http://autotest.kernel.org/wiki/TkoHowTo for more info)"))
23parser.add_option(
jamesren708f1c02010-03-31 21:43:57 +000024 '--test-attribute-field', action='append', default=[],
showardd9e341e2010-01-12 18:56:42 +000025 help='Specifies a test attribute to include as a field. The attribute '
26 'value will be available as a field named attribute_<attribute '
27 'name>. This option may be specified multiple times. Filtering '
28 'must be done slightly differently -- see '
29 'http://autotest.kernel.org/wiki/TkoHowTo#attribute_filtering '
30 'for more details.')
jamesren708f1c02010-03-31 21:43:57 +000031parser.add_option('--test-label-field', action='append', default=[],
showardd9e341e2010-01-12 18:56:42 +000032 help='Specifies a test label to include as a field. See '
33 '--attribute-field for more details')
jamesren708f1c02010-03-31 21:43:57 +000034parser.add_option('--iteration-result-field', action='append', default=[],
showardd9e341e2010-01-12 18:56:42 +000035 help='Specifies an iteration result to include as a field. '
36 'See --attribute-field for more details. Note that '
37 'this causes the rows returned to represent iterations '
38 'rather than plain test results.')
39parser.add_option('--machine-label-field', action='append', default=[],
40 help='Specifies a machine label to include as a field. See '
41 '--attribute-field for more details')
jamesren708f1c02010-03-31 21:43:57 +000042parser.add_option('--job-keyval-field', action='append', default=[],
43 help='Specifies a job keyval to include as a field. See '
44 '--attribute-field for more details')
45parser.add_option('--iteration-attribute-field', action='append', default=[],
46 help='Specifies an iteration attribute to include as a '
47 'field. See --attribute-field for more details. Note '
48 'that this causes the rows returned to represent '
49 'iterations rather than plain test results.')
mblighbe630eb2008-08-01 16:41:48 +000050parser.add_option('-s', '--separator', action='store', default = ' | ',
51 dest='separator', help = 'output separator')
52parser.add_option('-n', '--nocount', action='store_true', default=False,
53 help='Do not display line counts before each line')
54parser.add_option('-l', '--logpath', action='store_true', default=False,
55 help='Reformats the the tag column into a URL \
56 like http://autotest/results/[tag]. \
57 This will append the tag column if it isn\'t provided.')
mbligh789da1e2009-04-01 18:35:12 +000058parser.add_option('--host-label', action='store', dest='host_label',
59 help=('Return results only for machines currently '
60 'in the specified label'))
mblighbe630eb2008-08-01 16:41:48 +000061
62(options, args) = parser.parse_args()
63
showardd9e341e2010-01-12 18:56:42 +000064if not options.condition:
65 parser.error('You must specify a condition.')
66
67where = options.condition.replace('%', '%%')
68tag = 'job_tag'
mblighdeb758e2008-08-11 19:56:23 +000069
mblighbe630eb2008-08-01 16:41:48 +000070columns = options.columns.split(',')
71
72url_prefix = rpc.get_autotest_server() + '/results/'
73if options.logpath:
mbligh8dd5c6c2008-09-04 16:46:34 +000074 if tag not in columns:
75 columns.append(tag)
76 tag_index=columns.index(tag)
mblighbe630eb2008-08-01 16:41:48 +000077
mbligh789da1e2009-04-01 18:35:12 +000078if options.host_label:
79 database = database_connection.DatabaseConnection("AUTOTEST_WEB")
80 database.connect()
showardeab66ce2009-12-23 00:03:56 +000081 sql = ("SELECT hostname FROM afe_labels JOIN afe_hosts_labels "
82 "ON afe_labels.id=afe_hosts_labels.label_id JOIN afe_hosts "
83 "ON afe_hosts_labels.host_id=afe_hosts.id WHERE name=%s")
mbligh789da1e2009-04-01 18:35:12 +000084 results = database.execute(sql, options.host_label)
85 hosts = [row[0] for row in results]
86 where += " AND hostname IN ('" + "','".join(hosts) + "')"
87
mblighbe630eb2008-08-01 16:41:48 +000088# Grab the data
showardd9e341e2010-01-12 18:56:42 +000089tko = rpc.tko_comm()
mblighbe630eb2008-08-01 16:41:48 +000090count = 0
jamesren708f1c02010-03-31 21:43:57 +000091test_views = tko.run(
92 'get_test_views', extra_where=where,
93 test_attribute_fields=options.test_attribute_field,
94 test_label_fields=options.test_label_field,
95 iteration_result_fields=options.iteration_result_field,
96 machine_label_fields=options.machine_label_field,
97 job_keyval_fields=options.job_keyval_field,
98 iteration_attribute_fields=options.iteration_attribute_field)
showardd9e341e2010-01-12 18:56:42 +000099for test_view in test_views:
100 values = [str(test_view[column]) for column in columns]
mblighbe630eb2008-08-01 16:41:48 +0000101 if options.logpath:
102 values[tag_index] = url_prefix + values[tag_index]
103 if not options.nocount:
104 print '[%d] ' % count,
105 count += 1
106 print options.separator.join(values)