From 72d08643b336ebca6ee2db621ce78b21a5f8aa62 Mon Sep 17 00:00:00 2001
From: h4-rahman <hamidur2.rahman@live.uwe.ac.uk>
Date: Sun, 28 Apr 2024 20:30:34 +0100
Subject: [PATCH] Modified dashboard to show username and user id correctly:

---
 myproject/myapp/models.py                     | 52 +++++--------------
 myproject/myapp/payments.py                   |  2 +-
 .../myapp/templates/model_performance.html    |  6 +--
 myproject/myapp/views.py                      | 50 +++++++++++-------
 4 files changed, 46 insertions(+), 64 deletions(-)

diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py
index f6d4d5a..620f414 100644
--- a/myproject/myapp/models.py
+++ b/myproject/myapp/models.py
@@ -6,34 +6,6 @@ from enum import Enum
 from django.dispatch import receiver
 from django.db.models.signals import post_save
 
-# class UserTypes(User):
-#     USER_TYPE_CHOICES = (
-#         0, 'Basic User',
-#         1, 'Admin',
-#         2, 'ML Engineer',
-#         3, 'Accountant'
-#     )
-
-#     usertype = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES) # should we declare default=0 here?
-
-# group_names = ['Basic User', 'Admin', 'ML Engineer', 'Accountant']
-# for group_name in group_names:
-#     Group.objects.get_or_create(name=group_name)
-
-# # assign group permissions
-# content_type = ContentType.objects.get_for_model(UserTypes)
-# permission = Permission.objects.create(codename='can_view_user',
-#                                        name='Can View User',
-#                                        content_type=content_type)
-# group = Group.objects.get(name='Admin')
-# group.permissions.add(permission)
-
-
-# User = get_user_model()
-
-# user = User.objects.create_user('username', 'email', 'password')
-# # names are not necessary - reduces gdpr concerns aswell
-
 class Profile(models.Model):
     USER_TYPES = (
         (0, 'Basic User'),
@@ -61,18 +33,18 @@ class UserTokenCount(models.Model):
 
 
 class Action(Enum):
-    UPLOAD_FILE = "The user has successfully uploaded a file."
-    LOGIN = "The user has logged in to their account."
-    REGISTER = "The user has registered for a new account."
-    PAYMENT_SUCCESSFUL = "The user has successfully made a payment."
-    GENERATE_FINANCIAL_STATEMENT = "The user has generated a financial statement."
-    CHANGE_MLA = "The user has changed their maximum loss amount (MLA)."
-    RUN_ALGORITHM = "The user has run an algorithm."
-    INVALID_FILE = "The uploaded file is invalid and cannot be processed."
-    INVALID_PASSWORD = "The user has entered an invalid password."
-    USER_DOES_NOT_EXIST = "The user does not exist in the system."
-    DOWNLOAD_BREAKDOWN = "The user has downloaded a breakdown of their data."
-    UNKNOWN = "An unknown error has occurred."
+    UPLOAD_FILE = "{username} has successfully uploaded a file."
+    LOGIN = "{username} has logged in to their account."
+    REGISTER = "{username} has registered for a new account."
+    PAYMENT_SUCCESSFUL = "{username} has successfully made a payment."
+    GENERATE_FINANCIAL_STATEMENT = "{username} has generated a financial statement."
+    CHANGE_MLA = "{username} has changed their maximum loss amount (MLA)."
+    RUN_ALGORITHM = "{username} has run an algorithm."
+    INVALID_FILE = "{username} uploaded an invalid file that cannot be processed."
+    INVALID_PASSWORD = "{username} has entered an invalid password."
+    USER_DOES_NOT_EXIST = "The user {username} does not exist in the system."
+    DOWNLOAD_BREAKDOWN = "{username} has downloaded a breakdown of their data."
+    UNKNOWN = "An unknown error has occurred for user {username}."
 
 # class Logs(models.Model):
 #     """
diff --git a/myproject/myapp/payments.py b/myproject/myapp/payments.py
index 22e8b55..905ed88 100644
--- a/myproject/myapp/payments.py
+++ b/myproject/myapp/payments.py
@@ -114,6 +114,6 @@ def payment_cancelled(request):
     return render(request, 'payment_cancelled.html')
 
 def payment_success(request):
-    log_data = get_log_data(Action.PAYMENT_SUCCESSFUL, 'success')
+    log_data = get_log_data(request.user, Action.PAYMENT_SUCCESSFUL, 'success')
     create_log(request.user, log_data)
     return render(request,'payment_success.html')
diff --git a/myproject/myapp/templates/model_performance.html b/myproject/myapp/templates/model_performance.html
index 8fedd11..493a96f 100644
--- a/myproject/myapp/templates/model_performance.html
+++ b/myproject/myapp/templates/model_performance.html
@@ -7,9 +7,9 @@
   {% if metrics %}
     <div class="bg-white shadow-md rounded p-6">
       <p><strong>Request Count:</strong> {{ metrics.request_count }}</p>
-      <p><strong>Average Request Latency:</strong> {{ metrics.avg_request_latency|floatformat:2 }} ms</p>
-      <p><strong>Average Runtime Latency:</strong> {{ metrics.avg_runtime_latency|floatformat:2 }} ms</p>
-      <p><strong>Model Load Latency:</strong> {{ metrics.model_load_latency }} μs</p>
+      <p><strong>Average Request Latency:</strong> {{ metrics.avg_request_latency|floatformat:6 }} seconds</p>
+      <p><strong>Average Runtime Latency:</strong> {{ metrics.avg_runtime_latency|floatformat:6 }} seconds</p>
+      <p><strong>Model Load Latency:</strong> {{ metrics.model_load_latency|floatformat:6 }} seconds</p>
     </div>
   {% else %}
     <p>Failed to retrieve model performance metrics.</p>
diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py
index 690de16..eb925b7 100644
--- a/myproject/myapp/views.py
+++ b/myproject/myapp/views.py
@@ -38,9 +38,10 @@ import re
 
 logger = logging.getLogger(__name__)
 
-def get_log_data(action, status='success', file=None, description=None):
+def get_log_data(user, action, status='success', file=None, description=None):
     log_data = {
-        'action': action.value,
+        'username': user.username,
+        'action': action.value.format(username=user.username),
         'status': status,
         'file': file,
         'description': description,
@@ -57,10 +58,10 @@ def handling_music_file(request):
                 'action': 'File uploaded',
                 'file': request.FILES['audio_file'].name,
             }
-            log_data = get_log_data(Action.UPLOAD_FILE, 'success', file=request.FILES['audio_file'].name)
+            log_data = get_log_data(request.user ,Action.UPLOAD_FILE, 'success', file=request.FILES['audio_file'].name)
             create_log(request.user if request.user.is_authenticated else None, log_data)
             return HttpResponse('File uploaded successfully!',log_data)
-    log_data = get_log_data(Action.invalid_file, 'error')
+    log_data = get_log_data(request.user ,Action.INVALID_FILE, 'error')
     create_log(None, log_data)
     return HttpResponse('File invalid',log_data)
 
@@ -73,7 +74,7 @@ def log_fileupload(request):
         description = data.get('description')
 
         if request.user.is_authenticated:
-            log_data = get_log_data(Action.UPLOAD_FILE, status, file, description)
+            log_data = get_log_data(request.user, Action.UPLOAD_FILE, status, file, description)
             create_log(request.user, log_data)
 
         return JsonResponse({'message': 'Log created successfully'}, status=201)
@@ -82,19 +83,23 @@ def log_fileupload(request):
 
 def admin_table(request):
     # Execute the query and fetch all rows
-    query = """SELECT date, user, log FROM myapp_log ORDER BY date DESC"""
+    query = """SELECT date, log, user_id FROM myapp_log ORDER BY date DESC"""
     with connection.cursor() as cursor:
         cursor.execute(query)
         rows = cursor.fetchall()
-
+    print(rows)
     # Create a list of dictionaries from the query results
     data = []
     for row in rows:
         # Parse the JSON string into a dictionary
-        log = json.loads(row[2])
+        log = json.loads(row[1])
+
+        # Get the user object based on the user_id
+        user_id = row[2]
+
         # Create a dictionary with the date, user, and JSON fields
         date = row[0].strftime('%Y-%m-%d %H:%M:%S')
-        entry = {'date': date, 'user': row[1], 'file': log['file'], 'action': log['action'], 'status': log['status']}
+        entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status']}
         data.append(entry)
 
     # Return the data as a JSON response
@@ -104,7 +109,7 @@ def admin_table(request):
 def user_table(request):
     user_id= request.user.id
     # Only display user logs code below
-    query = """SELECT date, user, log FROM myapp_log WHERE user_id = {} ORDER BY date DESC""".format(user_id)
+    query = """SELECT date, log, user_id FROM myapp_log WHERE user_id = {} ORDER BY date DESC""".format(user_id)
     with connection.cursor() as cursor:
         cursor.execute(query)
         rows = cursor.fetchall()
@@ -113,10 +118,14 @@ def user_table(request):
     data = []
     for row in rows:
         # Parse the JSON string into a dictionary
-        log = json.loads(row[2])
+        log = json.loads(row[1])
+
+        # Get the user object based on the user_id
+        user_id = row[2]
+
         # Create a dictionary with the date, user, and JSON fields
         date = row[0].strftime('%Y-%m-%d %H:%M:%S')
-        entry = {'date': date, 'user': row[1], 'file': log['file'], 'action': log['action'], 'status': log['status']}
+        entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status']}
         data.append(entry)
 
     # Return the data as a JSON response
@@ -145,7 +154,7 @@ def index(request):
                 if response and hasattr(response, 'data') and 'predictions' in response.data:
                     context['predictions'] = response.data['predictions']
                     if request.user.is_authenticated:
-                        log_data = get_log_data(Action.RUN_ALGORITHM, 'success', file=uploaded_file.name,\
+                        log_data = get_log_data(request.user, Action.RUN_ALGORITHM, 'success', file=uploaded_file.name,\
                                                 description=response.data["predictions"])
                         create_log(request.user, log_data)
             else:
@@ -192,8 +201,7 @@ def users(request):
         context['all_user_profiles'] = all_user_profiles  # Add all_user_profiles to the context
 
         return render(request, 'user_page.html', context)
-    return redirect(   'login'
-)
+    return redirect('login')
 
 def handler404(request, *args, **kwargs):
     response = render(request, '404.html', {})
@@ -241,7 +249,7 @@ class CustomLoginView(LoginView):
         # Create log if user is authenticated
         login(self.request, form.get_user())
 
-        log_data = get_log_data(Action.LOGIN, 'success')
+        log_data = get_log_data(form.get_user(), Action.LOGIN, 'success')
         create_log(form.get_user(), log_data)
 
         return super().form_valid(form)
@@ -324,6 +332,7 @@ class InstrumentDetectionView(APIView):
     
 
 
+
 class ModelPerformanceView(UserPassesTestMixin, TemplateView):
     template_name = 'model_performance.html'
 
@@ -347,15 +356,16 @@ class ModelPerformanceView(UserPassesTestMixin, TemplateView):
             runtime_latency_count = re.search(r':tensorflow:serving:runtime_latency_count{model_name="instrument_model",API="Predict",runtime="TF1"} (\d+)', metrics_data)
             model_load_latency = re.search(r':tensorflow:cc:saved_model:load_latency{model_path="/models/instrument_model/2"} (\d+)', metrics_data)
 
-            # Calculate average latencies
-            avg_request_latency = float(request_latency_sum.group(1)) / float(request_latency_count.group(1)) if request_latency_sum and request_latency_count else None
-            avg_runtime_latency = float(runtime_latency_sum.group(1)) / float(runtime_latency_count.group(1)) if runtime_latency_sum and runtime_latency_count else None
+            # Calculate average latencies in seconds
+            avg_request_latency = float(request_latency_sum.group(1)) / float(request_latency_count.group(1)) / 1e6 if request_latency_sum and request_latency_count else None
+            avg_runtime_latency = float(runtime_latency_sum.group(1)) / float(runtime_latency_count.group(1)) / 1e6 if runtime_latency_sum and runtime_latency_count else None
+            model_load_latency_seconds = float(model_load_latency.group(1)) / 1e6 if model_load_latency else None
 
             context['metrics'] = {
                 'request_count': request_count.group(1) if request_count else None,
                 'avg_request_latency': avg_request_latency,
                 'avg_runtime_latency': avg_runtime_latency,
-                'model_load_latency': model_load_latency.group(1) if model_load_latency else None
+                'model_load_latency': model_load_latency_seconds
             }
         else:
             context['metrics'] = None
-- 
GitLab