Add middleware to log the request time 02/13602/3
authorsubhash kumar singh <subh.singh@samsung.com>
Fri, 11 Oct 2024 12:39:50 +0000 (12:39 +0000)
committersubhash kumar singh <subh.singh@samsung.com>
Thu, 24 Oct 2024 16:51:08 +0000 (16:51 +0000)
Added middleware to log the request time.

Change-Id: I556b79332bcbad276b786162502751e68b4b624d
Signed-off-by: subhash kumar singh <subh.singh@samsung.com>
.github/workflows/gerrit-verify.yaml
middleware/__init__.py [new file with mode: 0644]
middleware/loggingMiddleware.py [new file with mode: 0644]
trainingmgr/trainingmgr_main.py

index 2dc5c84..006eacc 100644 (file)
@@ -81,7 +81,7 @@ jobs:
       GERRIT_PROJECT: ${{ inputs.GERRIT_PROJECT }}
       GERRIT_REFSPEC: ${{ inputs.GERRIT_REFSPEC }}
       TOX_ENVS: '["code"]'
-      PYTHON_VERSION: '3.8'
+      PYTHON_VERSION: '3.10'
 
   vote:
     if: ${{ always() }}
diff --git a/middleware/__init__.py b/middleware/__init__.py
new file mode 100644 (file)
index 0000000..677395c
--- /dev/null
@@ -0,0 +1,17 @@
+# ==================================================================================
+#
+#       Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#          http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+# ==================================================================================
\ No newline at end of file
diff --git a/middleware/loggingMiddleware.py b/middleware/loggingMiddleware.py
new file mode 100644 (file)
index 0000000..177348a
--- /dev/null
@@ -0,0 +1,35 @@
+# ==================================================================================
+#
+#       Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#          http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+# ==================================================================================
+import time
+from werkzeug.wrappers import Request, Response
+
+class LoggingMiddleware:
+    def __init__(self, app):
+        self.app = app
+    
+    def __call__(self, environ, start_response):
+        request = Request(environ)
+
+        request_start_time = time.time()
+        response = self.app(environ, start_response)
+        request_end_time = time.time()
+        request_time_taken = request_end_time - request_start_time
+
+        print(f"=(TM)=> Request: {request.method} {request.url} - Time Taken: {request_time_taken:.4f} seconds")
+        return response
+
index 3a213c6..413de0b 100644 (file)
@@ -56,6 +56,9 @@ from trainingmgr.db.common_db_fun import get_data_extraction_in_progress_trainin
     get_feature_groups_db, get_feature_group_by_name_db, delete_feature_group_by_name, delete_trainingjob_version, change_field_value_by_version
 
 APP = Flask(__name__)
+
+from middleware.loggingMiddleware import LoggingMiddleware
+APP.wsgi_app = LoggingMiddleware(APP.wsgi_app)
 TRAININGMGR_CONFIG_OBJ = None
 PS_DB_OBJ = None
 LOGGER = None