NFVBENCH-40 Add pylint to tox
[nfvbench.git] / nfvbench / nfvbenchd.py
index 3ab30de..15b71c5 100644 (file)
 #    under the License.
 #
 
+import json
+import Queue
+import traceback
+import uuid
+
 from flask import Flask
 from flask import jsonify
 from flask import render_template
@@ -24,13 +29,10 @@ from flask_socketio import SocketIO
 from fluentd import FluentLogHandler
 from summarizer import NFVBenchSummarizer
 
-import json
 from log import LOG
-import Queue
-import traceback
 from utils import byteify
 from utils import RunLock
-import uuid
+
 
 # this global cannot reside in Ctx because of the @app and @socketio decorators
 app = None
@@ -137,7 +139,6 @@ def setup_flask(root_path):
     app.root_path = root_path
     socketio = SocketIO(app, async_mode='threading')
     busy_json = result_json(STATUS_ERROR, 'there is already an NFVbench request running')
-    config_is_null_json = result_json(STATUS_ERROR, 'configuration is missing')
     not_busy_json = result_json(STATUS_ERROR, 'no pending NFVbench run')
     not_found_msg = 'results not found'
     pending_msg = 'NFVbench run still pending'
@@ -145,43 +146,41 @@ def setup_flask(root_path):
     # --------- socketio requests ------------
 
     @socketio.on('start_run')
-    def socketio_start_run(config):
+    def _socketio_start_run(config):
         if not Ctx.is_busy():
             Ctx.enqueue(config, get_uuid(), from_socketio=True)
         else:
             emit('error', {'reason': 'there is already an NFVbench request running'})
 
     @socketio.on('echo')
-    def socketio_echo(config):
+    def _socketio_echo(config):
         emit('echo', config)
 
     # --------- HTTP requests ------------
 
     @app.route('/')
-    def index():
+    def _index():
         return render_template('index.html')
 
     @app.route('/echo', methods=['GET'])
-    def echo():
+    def _echo():
         config = request.json
         return jsonify(config)
 
     @app.route('/start_run', methods=['POST'])
-    def start_run():
+    def _start_run():
         config = load_json(request.json)
-        if config:
-            if Ctx.is_busy():
-                return jsonify(busy_json)
-            else:
-                request_id = get_uuid()
-                Ctx.enqueue(config, request_id)
-                return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
-        else:
-            return jsonify(config_is_null_json)
+        if not config:
+            config = {}
+        if Ctx.is_busy():
+            return jsonify(busy_json)
+        request_id = get_uuid()
+        Ctx.enqueue(config, request_id)
+        return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
 
     @app.route('/status', defaults={'request_id': None}, methods=['GET'])
     @app.route('/status/<request_id>', methods=['GET'])
-    def get_status(request_id):
+    def _get_status(request_id):
         if request_id:
             if Ctx.is_busy() and request_id == Ctx.get_current_request_id():
                 # task with request_id still pending
@@ -191,9 +190,8 @@ def setup_flask(root_path):
             if res:
                 # found result for given request_id
                 return jsonify(res)
-            else:
-                # result for given request_id not found
-                return jsonify(result_json(STATUS_NOT_FOUND, not_found_msg, request_id))
+            # result for given request_id not found
+            return jsonify(result_json(STATUS_NOT_FOUND, not_found_msg, request_id))
         else:
             if Ctx.is_busy():
                 # task still pending, return with request_id
@@ -204,8 +202,7 @@ def setup_flask(root_path):
             res = Ctx.get_result()
             if res:
                 return jsonify(res)
-            else:
-                return jsonify(not_busy_json)
+            return jsonify(not_busy_json)
 
 
 class WebSocketIoServer(object):
@@ -218,10 +215,13 @@ class WebSocketIoServer(object):
         self.nfvbench_runner = runner
         setup_flask(http_root)
         self.fluent_logger = logger
-        self.result_fluent_logger = FluentLogHandler("resultnfvbench",
-                                                     fluentd_ip=self.fluent_logger.sender.host,
-                                                     fluentd_port=self.fluent_logger.sender.port) \
-            if self.fluent_logger else None
+        self.result_fluent_logger = None
+        if self.fluent_logger:
+            self.result_fluent_logger = \
+                FluentLogHandler("resultnfvbench",
+                                 fluentd_ip=self.fluent_logger.sender.host,
+                                 fluentd_port=self.fluent_logger.sender.port)
+            self.result_fluent_logger.runlogdate = self.fluent_logger.runlogdate
 
     def run(self, host='127.0.0.1', port=7556):
 
@@ -253,6 +253,8 @@ class WebSocketIoServer(object):
             else:
                 # this might overwrite a previously unfetched result
                 Ctx.set_result(results)
+            if self.fluent_logger:
+                self.result_fluent_logger.runlogdate = self.fluent_logger.runlogdate
             summary = NFVBenchSummarizer(results['result'], self.result_fluent_logger)
             LOG.info(str(summary))
             Ctx.release()