diff options
-rw-r--r-- | cli/query_keyvals | 74 | ||||
-rw-r--r-- | new_tko/tko/models.py | 7 | ||||
-rw-r--r-- | new_tko/tko/rpc_interface.py | 48 |
3 files changed, 122 insertions, 7 deletions
diff --git a/cli/query_keyvals b/cli/query_keyvals new file mode 100644 index 00000000..25f34640 --- /dev/null +++ b/cli/query_keyvals @@ -0,0 +1,74 @@ +#!/usr/bin/python + +import sys, optparse +import common +from autotest_lib.cli import rpc + + +def parse_options(): + usage = "usage: %prog [options] job_id" + parser = optparse.OptionParser(usage=usage) + parser.add_option("-m", "--machine", dest="machine") + parser.add_option("-t", "--test", dest="test") + parser.add_option("-T", "--type", dest="type", type="choice", + choices=["all", "test", "iteration", "attr", "perf"]) + parser.add_option("-k", "--key", dest="key") + parser.set_defaults(type="all") + options, args = parser.parse_args() + options.show_test_keyvals = options.type in ("all", "test") + options.show_attr_keyvals = options.type in ("all", "iteration", "attr") + options.show_perf_keyvals = options.type in ("all", "iteration", "perf") + options.show_iter_keyvals = ( + options.show_perf_keyvals or options.show_attr_keyvals) + return parser, options, args + + +def print_keyvals(keyval, format_string, options): + for key, value in keyval.iteritems(): + if not options.key or key == options.key: + print format_string % (key, value) + + +def print_views(test_views, options): + for view in test_views: + if not options.machine: + print "Machine: %s" % view["hostname"] + if not options.test: + print "Test: %s" % view["test_name"] + if options.show_test_keyvals: + print "Test Attributes:" + print_keyvals(view["attributes"], "\t%s = %s", options) + if options.show_iter_keyvals: + print "Iteration Attributes:" + for i, iteration in enumerate(view["iterations"]): + print "\tIteration #%d:" % (i + 1) + if options.show_attr_keyvals: + print_keyvals(iteration["attr"], "\t\t%s(attr) = %s", + options) + if options.show_perf_keyvals: + print_keyvals(iteration["perf"], "\t\t%s(perf) = %s", + options) + print + + +def main(): + parser, options, args = parse_options() + if len(args) != 1: + print "Invalid number of arguments" + print + parser.print_help() + sys.exit(1) + + query_filter = {"job_tag__startswith": "%s-" % args[0]} + if options.machine: + query_filter["hostname"] = options.machine + if options.test: + query_filter["test_name"] = options.test + + comm = rpc.tko_comm("dunsel") + test_views = comm.run("get_detailed_test_views", **query_filter) + print_views(test_views, options) + + +if __name__ == "__main__": + main() diff --git a/new_tko/tko/models.py b/new_tko/tko/models.py index b16a3c34..27b02a99 100644 --- a/new_tko/tko/models.py +++ b/new_tko/tko/models.py @@ -177,7 +177,7 @@ class TestAttribute(dbmodels.Model, model_logic.ModelExtensions): db_table = 'test_attributes' -class IterationAttribute(dbmodels.Model): +class IterationAttribute(dbmodels.Model, model_logic.ModelExtensions): # see comment on TestAttribute regarding primary_key=True test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) iteration = dbmodels.IntegerField() @@ -188,8 +188,9 @@ class IterationAttribute(dbmodels.Model): db_table = 'iteration_attributes' -class IterationResult(dbmodels.Model): - test = dbmodels.ForeignKey(Test, db_column='test_idx') +class IterationResult(dbmodels.Model, model_logic.ModelExtensions): + # see comment on TestAttribute regarding primary_key=True + test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) iteration = dbmodels.IntegerField() attribute = dbmodels.CharField(maxlength=90) value = dbmodels.FloatField(null=True, max_digits=12, decimal_places=31, diff --git a/new_tko/tko/rpc_interface.py b/new_tko/tko/rpc_interface.py index 296b275a..1765b7b6 100644 --- a/new_tko/tko/rpc_interface.py +++ b/new_tko/tko/rpc_interface.py @@ -1,4 +1,4 @@ -import os, pickle, datetime +import os, pickle, datetime, itertools, operator from django.db import models as dbmodels from autotest_lib.frontend import thread_local from autotest_lib.frontend.afe import rpc_utils, model_logic @@ -151,15 +151,55 @@ def get_job_ids(**filter_data): # test detail view +def _itermodel_to_list(test_id, iteration_model): + return iteration_model.list_objects( + dict(test__test_idx=test_id), + fields=('iteration', 'attribute', 'value')) + + +def _attributes_to_dict(attribute_list): + return dict((attribute_dict['attribute'], attribute_dict['value']) + for attribute_dict in attribute_list) + + +def _iteration_attributes_to_dict(attribute_list): + iter_keyfunc = operator.itemgetter('iteration') + attribute_list.sort(key=iter_keyfunc) + iterations = {} + for key, group in itertools.groupby(attribute_list, iter_keyfunc): + iterations[key] = _attributes_to_dict(group) + return iterations + + def get_detailed_test_views(**filter_data): test_views = models.TestView.list_objects(filter_data) for test_view in test_views: test_id = test_view['test_idx'] + + # load in the test keyvals attribute_dicts = models.TestAttribute.list_objects( dict(test__test_idx=test_id), fields=('attribute', 'value')) - test_view['attributes'] = dict( - (attribute_dict['attribute'], attribute_dict['value']) - for attribute_dict in attribute_dicts) + test_view['attributes'] = _attributes_to_dict(attribute_dicts) + + # load in the iteration keyvals + attr_dicts = _itermodel_to_list(test_id, models.IterationAttribute) + perf_dicts = _itermodel_to_list(test_id, models.IterationResult) + + # convert the iterations into dictionarys and count total iterations + iteration_attr = _iteration_attributes_to_dict(attr_dicts) + iteration_perf = _iteration_attributes_to_dict(perf_dicts) + all_dicts = attr_dicts + perf_dicts + if all_dicts: + max_iterations = max(row['iteration'] for row in all_dicts) + else: + max_iterations = 0 + + # merge the iterations into a single list of attr & perf dicts + test_view['iterations'] = [{'attr': iteration_attr.get(index, {}), + 'perf': iteration_perf.get(index, {})} + for index in xrange(1, max_iterations + 1)] + + # load in the test labels label_dicts = models.TestLabel.list_objects( dict(tests__test_idx=test_id), fields=('name',)) test_view['labels'] = [label_dict['name'] for label_dict in label_dicts] |