320fcc2be4eaecdcba712ffbdc22a93ce68ec304
[releng.git] / utils / test / result_collection_api / resources / dashboard_handlers.py
1 from tornado.web import HTTPError
2
3 from common.constants import HTTP_NOT_FOUND
4 from dashboard.dashboard_utils import check_dashboard_ready_project, \
5     check_dashboard_ready_case, get_dashboard_result
6 from resources.result_handlers import GenericResultHandler
7 from resources.result_models import TestResult
8 from tornado_swagger_ui.tornado_swagger import swagger
9
10
11 class GenericDashboardHandler(GenericResultHandler):
12     def __init__(self, application, request, **kwargs):
13         super(GenericDashboardHandler, self).__init__(application,
14                                                       request,
15                                                       **kwargs)
16         self.table = self.db_results
17         self.table_cls = TestResult
18
19
20 class DashboardHandler(GenericDashboardHandler):
21     @swagger.operation(nickname='query')
22     def get(self):
23         """
24             @description: Retrieve dashboard ready result(s)
25                           for a test project
26             @notes: Retrieve dashboard ready result(s) for a test project
27                 Available filters for this request are :
28                  - project : project name
29                  - case : case name
30                  - pod : pod name
31                  - version : platform version (Arno-R1, ...)
32                  - installer (fuel, ...)
33                  - period : x (x last days)
34
35                 GET /dashboard?project=functest&case=vPing&version=Colorado \
36                 &pod=pod_name&period=15
37             @rtype: L{string}
38             @param pod: pod name
39             @type pod: L{string}
40             @in pod: query
41             @required pod: False
42             @param project: project name
43             @type project: L{string}
44             @in project: query
45             @required project: True
46             @param case: case name
47             @type case: L{string}
48             @in case: query
49             @required case: True
50             @param version: i.e. Colorado
51             @type version: L{string}
52             @in version: query
53             @required version: False
54             @param installer: fuel/apex/joid/compass
55             @type installer: L{string}
56             @in installer: query
57             @required installer: False
58             @param period: last days
59             @type period: L{string}
60             @in period: query
61             @required period: False
62             @return 200: test result exist
63             @raise 400: period is not in
64             @raise 404: project or case name missing,
65                         or project or case is not dashboard ready
66         """
67
68         project_arg = self.get_query_argument("project", None)
69         case_arg = self.get_query_argument("case", None)
70
71         # on /dashboard retrieve the list of projects and testcases
72         # ready for dashboard
73         if project_arg is None:
74             raise HTTPError(HTTP_NOT_FOUND, "Project name missing")
75
76         if not check_dashboard_ready_project(project_arg):
77             raise HTTPError(HTTP_NOT_FOUND,
78                             'Project [{}] not dashboard ready'
79                             .format(project_arg))
80
81         if case_arg is None:
82             raise HTTPError(
83                 HTTP_NOT_FOUND,
84                 'Test case missing for project [{}]'.format(project_arg))
85
86         if not check_dashboard_ready_case(project_arg, case_arg):
87             raise HTTPError(
88                 HTTP_NOT_FOUND,
89                 'Test case [{}] not dashboard ready for project [{}]'
90                 .format(case_arg, project_arg))
91
92         # special case of status for project
93         if case_arg == 'status':
94             self.finish_request(get_dashboard_result(project_arg, case_arg))
95         else:
96             def get_result(res, project, case):
97                 return get_dashboard_result(project, case, res)
98
99             self._list(self.set_query(), get_result, project_arg, case_arg)