summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/opnfv_testapi/resources/dashboard_handlers.py
blob: 303e8d1642b0bcbd0ab6ee58cd60087770fe02a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
##############################################################################
# Copyright (c) 2015 Orange
# guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from tornado.web import HTTPError

from opnfv_testapi.common.constants import HTTP_NOT_FOUND
from opnfv_testapi.dashboard.dashboard_utils import \
    check_dashboard_ready_project, check_dashboard_ready_case, \
    get_dashboard_result, get_dashboard_projects
from opnfv_testapi.resources.result_handlers import GenericResultHandler
from opnfv_testapi.resources.result_models import TestResult
from opnfv_testapi.tornado_swagger import swagger


class GenericDashboardHandler(GenericResultHandler):
    def __init__(self, application, request, **kwargs):
        super(GenericDashboardHandler, self).__init__(application,
                                                      request,
                                                      **kwargs)
        self.table = self.db_results
        self.table_cls = TestResult


class DashboardHandler(GenericDashboardHandler):
    @swagger.operation(nickname='query')
    def get(self):
        """
            @description: Retrieve dashboard ready result(s)
                          for a test project
            @notes: Retrieve dashboard ready result(s) for a test project
                Available filters for this request are :
                 - project : project name
                 - case : case name
                 - pod : pod name
                 - version : platform version (Arno-R1, ...)
                 - installer (fuel, ...)
                 - period : x (x last days)

                GET /dashboard?project=functest&case=vPing&version=Colorado \
                &pod=pod_name&period=15
            @rtype: L{string}
            @param pod: pod name
            @type pod: L{string}
            @in pod: query
            @required pod: False
            @param project: project name
            @type project: L{string}
            @in project: query
            @required project: True
            @param case: case name
            @type case: L{string}
            @in case: query
            @required case: True
            @param version: i.e. Colorado
            @type version: L{string}
            @in version: query
            @required version: False
            @param installer: fuel/apex/joid/compass
            @type installer: L{string}
            @in installer: query
            @required installer: False
            @param period: last days
            @type period: L{string}
            @in period: query
            @required period: False
            @return 200: test result exist
            @raise 400: period is not in
            @raise 404: project or case name missing,
                        or project or case is not dashboard ready
        """

        project_arg = self.get_query_argument("project", None)
        case_arg = self.get_query_argument("case", None)

        # on /dashboard retrieve the list of projects and testcases
        # ready for dashboard
        if project_arg is None:
            raise HTTPError(HTTP_NOT_FOUND, "Project name missing")

        if not check_dashboard_ready_project(project_arg):
            raise HTTPError(HTTP_NOT_FOUND,
                            'Project [{}] not dashboard ready'
                            .format(project_arg))

        if case_arg is None:
            raise HTTPError(
                HTTP_NOT_FOUND,
                'Test case missing for project [{}]'.format(project_arg))

        if not check_dashboard_ready_case(project_arg, case_arg):
            raise HTTPError(
                HTTP_NOT_FOUND,
                'Test case [{}] not dashboard ready for project [{}]'
                .format(case_arg, project_arg))

        # special case of status for project
        if case_arg == 'status':
            self.finish_request(get_dashboard_result(project_arg, case_arg))
        else:
            def get_result(res, project, case):
                return get_dashboard_result(project, case, res)

            self._list(self.set_query(), get_result, project_arg, case_arg)


class DashboardProjectsHandler(GenericDashboardHandler):
    @swagger.operation(nickname='list')
    def get(self):
        """
            @description: Retrieve dashboard ready project(s)
            @rtype: L{list}
            @return 200: return all dashboard ready project(s)
        """
        self.finish_request(get_dashboard_projects())