bugfix: query doesn't work well with period=1
[releng.git] / utils / test / testapi / opnfv_testapi / resources / result_handlers.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from datetime import datetime
10 from datetime import timedelta
11
12 from bson import objectid
13
14 from opnfv_testapi.common import config
15 from opnfv_testapi.common import message
16 from opnfv_testapi.common import raises
17 from opnfv_testapi.resources import handlers
18 from opnfv_testapi.resources import result_models
19 from opnfv_testapi.tornado_swagger import swagger
20
21 CONF = config.Config()
22
23
24 class GenericResultHandler(handlers.GenericApiHandler):
25     def __init__(self, application, request, **kwargs):
26         super(GenericResultHandler, self).__init__(application,
27                                                    request,
28                                                    **kwargs)
29         self.table = self.db_results
30         self.table_cls = result_models.TestResult
31
32     def get_int(self, key, value):
33         try:
34             value = int(value)
35         except:
36             raises.BadRequest(message.must_int(key))
37         return value
38
39     def set_query(self):
40         query = dict()
41         date_range = dict()
42
43         for k in self.request.query_arguments.keys():
44             v = self.get_query_argument(k)
45             if k == 'project' or k == 'pod' or k == 'case':
46                 query[k + '_name'] = v
47             elif k == 'period':
48                 v = self.get_int(k, v)
49                 if v > 0:
50                     period = datetime.now() - timedelta(days=v)
51                     obj = {"$gte": str(period)}
52                     query['start_date'] = obj
53             elif k == 'trust_indicator':
54                 query[k + '.current'] = float(v)
55             elif k == 'from':
56                 date_range.update({'$gte': str(v)})
57             elif k == 'to':
58                 date_range.update({'$lt': str(v)})
59             elif k != 'last' and k != 'page':
60                 query[k] = v
61             if date_range:
62                 query['start_date'] = date_range
63
64             # if $lt is not provided,
65             # empty/None/null/'' start_date will also be returned
66             if 'start_date' in query and '$lt' not in query['start_date']:
67                 query['start_date'].update({'$lt': str(datetime.now())})
68
69         return query
70
71
72 class ResultsCLHandler(GenericResultHandler):
73     @swagger.operation(nickname="queryTestResults")
74     def get(self):
75         """
76             @description: Retrieve result(s) for a test project
77                           on a specific pod.
78             @notes: Retrieve result(s) for a test project on a specific pod.
79                 Available filters for this request are :
80                  - project : project name
81                  - case : case name
82                  - pod : pod name
83                  - version : platform version (Arno-R1, ...)
84                  - installer : fuel/apex/compass/joid/daisy
85                  - build_tag : Jenkins build tag name
86                  - period : x last days, incompatible with from/to
87                  - from : starting time in 2016-01-01 or 2016-01-01 00:01:23
88                  - to : ending time in 2016-01-01 or 2016-01-01 00:01:23
89                  - scenario : the test scenario (previously version)
90                  - criteria : the global criteria status passed or failed
91                  - trust_indicator : evaluate the stability of the test case
92                    to avoid running systematically long and stable test case
93
94                 GET /results/project=functest&case=vPing&version=Arno-R1 \
95                 &pod=pod_name&period=15
96             @return 200: all test results consist with query,
97                          empty list if no result is found
98             @rtype: L{TestResults}
99             @param pod: pod name
100             @type pod: L{string}
101             @in pod: query
102             @required pod: False
103             @param project: project name
104             @type project: L{string}
105             @in project: query
106             @required project: False
107             @param case: case name
108             @type case: L{string}
109             @in case: query
110             @required case: False
111             @param version: i.e. Colorado
112             @type version: L{string}
113             @in version: query
114             @required version: False
115             @param installer: fuel/apex/joid/compass
116             @type installer: L{string}
117             @in installer: query
118             @required installer: False
119             @param build_tag: i.e. v3.0
120             @type build_tag: L{string}
121             @in build_tag: query
122             @required build_tag: False
123             @param scenario: i.e. odl
124             @type scenario: L{string}
125             @in scenario: query
126             @required scenario: False
127             @param criteria: i.e. passed
128             @type criteria: L{string}
129             @in criteria: query
130             @required criteria: False
131             @param period: last days
132             @type period: L{string}
133             @in period: query
134             @required period: False
135             @param from: i.e. 2016-01-01 or 2016-01-01 00:01:23
136             @type from: L{string}
137             @in from: query
138             @required from: False
139             @param to: i.e. 2016-01-01 or 2016-01-01 00:01:23
140             @type to: L{string}
141             @in to: query
142             @required to: False
143             @param last: last records stored until now
144             @type last: L{string}
145             @in last: query
146             @required last: False
147             @param page: which page to list
148             @type page: L{int}
149             @in page: query
150             @required page: False
151             @param trust_indicator: must be float
152             @type trust_indicator: L{float}
153             @in trust_indicator: query
154             @required trust_indicator: False
155         """
156         limitations = {'sort': {'start_date': -1}}
157         last = self.get_query_argument('last', 0)
158         if last is not None:
159             last = self.get_int('last', last)
160             limitations.update({'last': last})
161
162         page = self.get_query_argument('page', None)
163         if page is not None:
164             page = self.get_int('page', page)
165             limitations.update({'page': page,
166                                 'per_page': CONF.api_results_per_page})
167
168         self._list(query=self.set_query(), **limitations)
169
170     @swagger.operation(nickname="createTestResult")
171     def post(self):
172         """
173             @description: create a test result
174             @param body: result to be created
175             @type body: L{ResultCreateRequest}
176             @in body: body
177             @rtype: L{CreateResponse}
178             @return 200: result is created.
179             @raise 404: pod/project/testcase not exist
180             @raise 400: body/pod_name/project_name/case_name not provided
181         """
182         def pod_query():
183             return {'name': self.json_args.get('pod_name')}
184
185         def project_query():
186             return {'name': self.json_args.get('project_name')}
187
188         def testcase_query():
189             return {'project_name': self.json_args.get('project_name'),
190                     'name': self.json_args.get('case_name')}
191
192         miss_fields = ['pod_name', 'project_name', 'case_name']
193         carriers = [('pods', pod_query),
194                     ('projects', project_query),
195                     ('testcases', testcase_query)]
196         self._create(miss_fields=miss_fields, carriers=carriers)
197
198
199 class ResultsGURHandler(GenericResultHandler):
200     @swagger.operation(nickname='getTestResultById')
201     def get(self, result_id):
202         """
203             @description: get a single result by result_id
204             @rtype: L{TestResult}
205             @return 200: test result exist
206             @raise 404: test result not exist
207         """
208         query = dict()
209         query["_id"] = objectid.ObjectId(result_id)
210         self._get_one(query=query)
211
212     @swagger.operation(nickname="updateTestResultById")
213     def put(self, result_id):
214         """
215             @description: update a single result by _id
216             @param body: fields to be updated
217             @type body: L{ResultUpdateRequest}
218             @in body: body
219             @rtype: L{Result}
220             @return 200: update success
221             @raise 404: result not exist
222             @raise 403: nothing to update
223         """
224         query = {'_id': objectid.ObjectId(result_id)}
225         db_keys = []
226         self._update(query=query, db_keys=db_keys)