aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Braun <matze@braunis.de>2017-11-17 00:36:32 +0000
committerMatthias Braun <matze@braunis.de>2017-11-17 00:36:32 +0000
commite293a6e0eed7cade22096a6c5d50ea095155a8d9 (patch)
tree85960ebb446fb6738895137e236bda24b414cec5
parent7656244e328d698fb1441d28365c84d85da2066f (diff)
Add a rest API to query the test suite schema
This helps users get things like display_names and units for the metrics. git-svn-id: https://llvm.org/svn/llvm-project/lnt/trunk@318485 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--docs/api.rst2
-rw-r--r--lnt/server/db/testsuite.py42
-rw-r--r--lnt/server/ui/api.py10
-rw-r--r--tests/server/ui/test_api.py31
4 files changed, 81 insertions, 4 deletions
diff --git a/docs/api.rst b/docs/api.rst
index da1789b..1b4a965 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -36,6 +36,8 @@ once.
+---------------------------+------------------------------------------------------------------------------------------+
| /samples/`id` | Get all non-empty sample info for Sample `id`. |
+---------------------------+------------------------------------------------------------------------------------------+
+| /schema | Return test suite schema. |
++---------------------------+------------------------------------------------------------------------------------------+
.. _auth_tokens:
diff --git a/lnt/server/db/testsuite.py b/lnt/server/db/testsuite.py
index 8806295..47abb87 100644
--- a/lnt/server/db/testsuite.py
+++ b/lnt/server/db/testsuite.py
@@ -164,6 +164,48 @@ class TestSuite(Base):
ts.jsonschema = data
return ts
+ def __json__(self):
+ metrics = []
+ for sample_field in self.sample_fields:
+ metric = {
+ 'bigger_is_better': (sample_field.bigger_is_better != 0),
+ 'display_name': sample_field.display_name,
+ 'name': sample_field.name,
+ 'type': sample_field.type.name,
+ 'unit': sample_field.unit,
+ 'unit_abbrev': sample_field.unit_abbrev,
+ }
+ metrics.append(metric)
+ machine_fields = []
+ for machine_field in self.machine_fields:
+ field = {
+ 'name': machine_field.name
+ }
+ machine_fields.append(field)
+ run_fields = []
+ for run_field in self.run_fields:
+ field = {
+ 'name': run_field.name
+ }
+ run_fields.append(field)
+ for order_field in self.order_fields:
+ field = {
+ 'name': order_field.name,
+ 'order': True,
+ }
+ run_fields.append(field)
+ metrics.sort(key=lambda x: x['name'])
+ machine_fields.sort(key=lambda x: x['name'])
+ run_fields.sort(key=lambda x: x['name'])
+
+ return {
+ 'format_version': '2',
+ 'machine_fields': machine_fields,
+ 'metrics': metrics,
+ 'name': self.name,
+ 'run_fields': run_fields,
+ }
+
class FieldMixin(object):
@property
diff --git a/lnt/server/ui/api.py b/lnt/server/ui/api.py
index ee0060a..537f6db 100644
--- a/lnt/server/ui/api.py
+++ b/lnt/server/ui/api.py
@@ -318,6 +318,15 @@ class Order(Resource):
return result
+class Schema(Resource):
+ method_decorators = [in_db]
+
+ @staticmethod
+ def get():
+ ts = request.get_testsuite()
+ return ts.test_suite
+
+
class SampleData(Resource):
method_decorators = [in_db]
@@ -498,6 +507,7 @@ def load_api_resources(api):
api.add_resource(Run, ts_path("runs/<int:run_id>"))
api.add_resource(SamplesData, ts_path("samples"), ts_path("samples/"))
api.add_resource(SampleData, ts_path("samples/<sample_id>"))
+ api.add_resource(Schema, ts_path("schema"), ts_path("schema/"))
api.add_resource(Order, ts_path("orders/<int:order_id>"))
graph_url = "graph/<int:machine_id>/<int:test_id>/<int:field_index>"
api.add_resource(Graph, ts_path(graph_url))
diff --git a/tests/server/ui/test_api.py b/tests/server/ui/test_api.py
index 50026a8..290a54a 100644
--- a/tests/server/ui/test_api.py
+++ b/tests/server/ui/test_api.py
@@ -7,13 +7,13 @@
#
# RUN: python %s %t.instance
+from V4Pages import check_json
+import lnt.server.db.migrate
+import lnt.server.ui.app
import logging
import sys
import unittest
-
-import lnt.server.db.migrate
-import lnt.server.ui.app
-from V4Pages import check_json
+import yaml
logging.basicConfig(level=logging.DEBUG)
@@ -113,6 +113,7 @@ class JSONAPITester(unittest.TestCase):
def setUp(self):
"""Bind to the LNT test instance."""
_, instance_path = sys.argv
+ self.instance_path = instance_path
app = lnt.server.ui.app.App.create_standalone(instance_path)
app.testing = True
self.client = app.test_client()
@@ -241,6 +242,28 @@ class JSONAPITester(unittest.TestCase):
self._check_response_is_well_formed(two_runs)
self.assertEqual(j, two_runs)
+ def test_schema(self):
+ client = self.client
+ rest_schema = check_json(client, 'api/db_default/v4/nts/schema')
+
+ # The reported schema should be the same as the yaml one on the top.
+ with open('%s/schemas/nts.yaml' % self.instance_path) as syaml:
+ yaml_schema = yaml.load(syaml)
+ # Do some massaging to make it similar to the rest API result.
+ for m in yaml_schema['metrics']:
+ if 'unit' not in m:
+ m['unit'] = None
+ if 'unit_abbrev' not in m:
+ m['unit_abbrev'] = None
+ if 'display_name' not in m:
+ m['display_name'] = m['name']
+ if 'bigger_is_better' not in m:
+ m['bigger_is_better'] = False
+ yaml_schema['metrics'].sort(key=lambda x: x['name'])
+ yaml_schema['run_fields'].sort(key=lambda x: x['name'])
+ yaml_schema['machine_fields'].sort(key=lambda x: x['name'])
+ self.assertEqual(rest_schema, yaml_schema)
+
if __name__ == '__main__':
unittest.main(argv=[sys.argv[0], ])