mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """ |
| 3 | Selects all rows and columns that satisfy the condition specified |
| 4 | and prints the matrix. |
| 5 | """ |
showard | d9e341e | 2010-01-12 18:56:42 +0000 | [diff] [blame^] | 6 | import optparse |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 7 | import common |
| 8 | from autotest_lib.cli import rpc |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame] | 9 | from autotest_lib.database import database_connection |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | # First do all the options parsing |
| 13 | parser = optparse.OptionParser() |
showard | d9e341e | 2010-01-12 18:56:42 +0000 | [diff] [blame^] | 14 | parser.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') |
| 19 | parser.add_option('-w', '--where', action='store', dest='condition', |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 20 | help=("The WHERE condition for the query witten in the 'new style' " |
mbligh | 28afa73 | 2008-09-30 20:29:46 +0000 | [diff] [blame] | 21 | "condition syntax for new tko (see " |
showard | d9e341e | 2010-01-12 18:56:42 +0000 | [diff] [blame^] | 22 | "http://autotest.kernel.org/wiki/TkoHowTo for more info)")) |
| 23 | parser.add_option( |
| 24 | '--attribute-field', action='append', default=[], |
| 25 | 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.') |
| 31 | parser.add_option('--label-field', action='append', default=[], |
| 32 | help='Specifies a test label to include as a field. See ' |
| 33 | '--attribute-field for more details') |
| 34 | parser.add_option('--iteration-field', action='append', default=[], |
| 35 | 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.') |
| 39 | parser.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') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 42 | parser.add_option('-s', '--separator', action='store', default = ' | ', |
| 43 | dest='separator', help = 'output separator') |
| 44 | parser.add_option('-n', '--nocount', action='store_true', default=False, |
| 45 | help='Do not display line counts before each line') |
| 46 | parser.add_option('-l', '--logpath', action='store_true', default=False, |
| 47 | help='Reformats the the tag column into a URL \ |
| 48 | like http://autotest/results/[tag]. \ |
| 49 | This will append the tag column if it isn\'t provided.') |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame] | 50 | parser.add_option('--host-label', action='store', dest='host_label', |
| 51 | help=('Return results only for machines currently ' |
| 52 | 'in the specified label')) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 53 | |
| 54 | (options, args) = parser.parse_args() |
| 55 | |
showard | d9e341e | 2010-01-12 18:56:42 +0000 | [diff] [blame^] | 56 | if not options.condition: |
| 57 | parser.error('You must specify a condition.') |
| 58 | |
| 59 | where = options.condition.replace('%', '%%') |
| 60 | tag = 'job_tag' |
mbligh | deb758e | 2008-08-11 19:56:23 +0000 | [diff] [blame] | 61 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 62 | columns = options.columns.split(',') |
| 63 | |
| 64 | url_prefix = rpc.get_autotest_server() + '/results/' |
| 65 | if options.logpath: |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 66 | if tag not in columns: |
| 67 | columns.append(tag) |
| 68 | tag_index=columns.index(tag) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 69 | |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame] | 70 | if options.host_label: |
| 71 | database = database_connection.DatabaseConnection("AUTOTEST_WEB") |
| 72 | database.connect() |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 73 | sql = ("SELECT hostname FROM afe_labels JOIN afe_hosts_labels " |
| 74 | "ON afe_labels.id=afe_hosts_labels.label_id JOIN afe_hosts " |
| 75 | "ON afe_hosts_labels.host_id=afe_hosts.id WHERE name=%s") |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame] | 76 | results = database.execute(sql, options.host_label) |
| 77 | hosts = [row[0] for row in results] |
| 78 | where += " AND hostname IN ('" + "','".join(hosts) + "')" |
| 79 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 80 | # Grab the data |
showard | d9e341e | 2010-01-12 18:56:42 +0000 | [diff] [blame^] | 81 | tko = rpc.tko_comm() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 82 | count = 0 |
showard | d9e341e | 2010-01-12 18:56:42 +0000 | [diff] [blame^] | 83 | test_views = tko.run('get_test_views', extra_where=where, |
| 84 | test_attribute_fields=options.attribute_field, |
| 85 | test_label_fields=options.label_field, |
| 86 | iteration_fields=options.iteration_field, |
| 87 | machine_label_fields=options.machine_label_field) |
| 88 | for test_view in test_views: |
| 89 | values = [str(test_view[column]) for column in columns] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 90 | if options.logpath: |
| 91 | values[tag_index] = url_prefix + values[tag_index] |
| 92 | if not options.nocount: |
| 93 | print '[%d] ' % count, |
| 94 | count += 1 |
| 95 | print options.separator.join(values) |