From 2989d5655838dbdf6451f40e57146e386948c331 Mon Sep 17 00:00:00 2001 From: Tim Buckley Date: Wed, 15 Jul 2015 16:41:48 -0600 Subject: [PATCH] add JSON api for getting tempest run results --- stackviz/views/tempest/api.py | 64 ++++++++++++++++++++++++++++++++++ stackviz/views/tempest/urls.py | 10 +++--- 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 stackviz/views/tempest/api.py diff --git a/stackviz/views/tempest/api.py b/stackviz/views/tempest/api.py new file mode 100644 index 0000000..76ee0ba --- /dev/null +++ b/stackviz/views/tempest/api.py @@ -0,0 +1,64 @@ +from restless.views import Endpoint + +from stackviz.parser.tempest_subunit import (get_repositories, + convert_run, + reorganize) + +#: Cached results from loaded subunit logs indexed by their run number +_cached_run = {} + +#: Cached results converted into tree form +_cached_tree = {} + + +class NoRunDataException(Exception): + pass + +class RunNotFoundException(Exception): + pass + + +def _load_run(run_id): + if run_id in _cached_run: + return _cached_run[run_id] + + repos = get_repositories() + if not repos: + raise NoRunDataException() + + try: + # assume first repo for now + run = repos[0].get_test_run(run_id) + + # strip details for now + # TODO: provide method for getting details on demand + # (preferably for individual tests to avoid bloat) + converted_run = convert_run(run, strip_details=True) + _cached_run[run_id] = converted_run + + return converted_run + except KeyError: + raise RunNotFoundException() + + +def _load_tree(run_id): + if run_id in _cached_tree: + return _cached_tree[run_id] + + run = _load_run(run_id) + tree = reorganize(run) + + _cached_tree[run_id] = tree + return tree + + +class TempestRunEndpoint(Endpoint): + def get(self, request, run_id): + return _load_tree(run_id) + + +# TODO: run details +# class TempestRunDetailsEndpoint(Endpoint): +# def get(self, request, run_id, test_path): +# ... + diff --git a/stackviz/views/tempest/urls.py b/stackviz/views/tempest/urls.py index e5cc6b3..8507b36 100644 --- a/stackviz/views/tempest/urls.py +++ b/stackviz/views/tempest/urls.py @@ -1,13 +1,11 @@ from django.conf.urls import patterns, include, url -from django.contrib import admin -from stackviz.views.tempest.latest_results import LatestResultsView +from .latest_results import LatestResultsView +from .api import TempestRunEndpoint urlpatterns = patterns('', - # Examples: - # url(r'^$', 'stackviz.views.home', name='home'), - # url(r'^blog/', include('blog.urls')), - url(r'^latest_results$', LatestResultsView.as_view()), + + url(r'^api/(\d+)/$', TempestRunEndpoint.as_view()) )