update testapi server in testresults.opnfv.org
[releng.git] / utils / test / result_collection_api / opnfv_testapi / resources / result_handlers.py
index a9aa17b..400b84a 100644 (file)
@@ -1,3 +1,11 @@
+##############################################################################
+# 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 datetime import datetime, timedelta
 
 from bson.objectid import ObjectId
@@ -6,7 +14,7 @@ from tornado.web import HTTPError
 from opnfv_testapi.common.constants import HTTP_BAD_REQUEST, HTTP_NOT_FOUND
 from opnfv_testapi.resources.handlers import GenericApiHandler
 from opnfv_testapi.resources.result_models import TestResult
-from opnfv_testapi.tornado_swagger_ui.tornado_swagger import swagger
+from opnfv_testapi.tornado_swagger import swagger
 
 
 class GenericResultHandler(GenericApiHandler):
@@ -17,6 +25,13 @@ class GenericResultHandler(GenericApiHandler):
         self.table = self.db_results
         self.table_cls = TestResult
 
+    def get_int(self, key, value):
+        try:
+            value = int(value)
+        except:
+            raise HTTPError(HTTP_BAD_REQUEST, '{} must be int'.format(key))
+        return value
+
     def set_query(self):
         query = dict()
         for k in self.request.query_arguments.keys():
@@ -24,17 +39,14 @@ class GenericResultHandler(GenericApiHandler):
             if k == 'project' or k == 'pod' or k == 'case':
                 query[k + '_name'] = v
             elif k == 'period':
-                try:
-                    v = int(v)
-                except:
-                    raise HTTPError(HTTP_BAD_REQUEST, 'period must be int')
+                v = self.get_int(k, v)
                 if v > 0:
                     period = datetime.now() - timedelta(days=v)
                     obj = {"$gte": str(period)}
                     query['start_date'] = obj
             elif k == 'trust_indicator':
-                query[k] = float(v)
-            else:
+                query[k + '.current'] = float(v)
+            elif k != 'last':
                 query[k] = v
         return query
 
@@ -71,11 +83,11 @@ class ResultsCLHandler(GenericResultHandler):
             @param project: project name
             @type project: L{string}
             @in project: query
-            @required project: True
+            @required project: False
             @param case: case name
             @type case: L{string}
             @in case: query
-            @required case: True
+            @required case: False
             @param version: i.e. Colorado
             @type version: L{string}
             @in version: query
@@ -100,12 +112,20 @@ class ResultsCLHandler(GenericResultHandler):
             @type period: L{string}
             @in period: query
             @required period: False
-            @param trust_indicator: must be int/long/float
-            @type trust_indicator: L{string}
+            @param last: last records stored until now
+            @type last: L{string}
+            @in last: query
+            @required last: False
+            @param trust_indicator: must be float
+            @type trust_indicator: L{float}
             @in trust_indicator: query
             @required trust_indicator: False
         """
-        self._list(self.set_query())
+        last = self.get_query_argument('last', 0)
+        if last is not None:
+            last = self.get_int('last', last)
+
+        self._list(self.set_query(), sort=[('start_date', -1)], last=last)
 
     @swagger.operation(nickname="create")
     def post(self):
@@ -114,7 +134,7 @@ class ResultsCLHandler(GenericResultHandler):
             @param body: result to be created
             @type body: L{ResultCreateRequest}
             @in body: body
-            @rtype: L{TestResult}
+            @rtype: L{CreateResponse}
             @return 200: result is created.
             @raise 404: pod/project/testcase not exist
             @raise 400: body/pod_name/project_name/case_name not provided
@@ -160,3 +180,19 @@ class ResultsGURHandler(GenericResultHandler):
         query = dict()
         query["_id"] = ObjectId(result_id)
         self._get_one(query)
+
+    @swagger.operation(nickname="update")
+    def put(self, result_id):
+        """
+            @description: update a single result by _id
+            @param body: fields to be updated
+            @type body: L{ResultUpdateRequest}
+            @in body: body
+            @rtype: L{Result}
+            @return 200: update success
+            @raise 404: result not exist
+            @raise 403: nothing to update
+        """
+        query = {'_id': ObjectId(result_id)}
+        db_keys = []
+        self._update(query, db_keys)