From fb675826d89cb4f45f67867e89515b43ccecd06d Mon Sep 17 00:00:00 2001 From: azec1 Date: Fri, 31 May 2024 10:37:24 +0200 Subject: [PATCH 1/2] feat: Implement new list view for requests on start page --- silk/models.py | 38 +++++ silk/templates/silk/summary.html | 243 +++++++++++++++++++------------ 2 files changed, 185 insertions(+), 96 deletions(-) diff --git a/silk/models.py b/silk/models.py index 075c5e9a..ea78894c 100644 --- a/silk/models.py +++ b/silk/models.py @@ -19,6 +19,9 @@ OneToOneField, TextField, ) +from django.shortcuts import render +from django.db.models import Avg, Count +from .models import Request from django.utils import timezone from django.utils.safestring import mark_safe @@ -203,6 +206,41 @@ def save(self, *args, **kwargs): super().save(*args, **kwargs) Request.garbage_collect(force=False) + def summary_view(request): + num_requests = Request.objects.count() + avg_overall_time = Request.objects.aggregate(Avg('time_taken'))['time_taken__avg'] or 0 + avg_num_queries = Request.objects.aggregate(Avg('meta_num_queries'))['meta_num_queries__avg'] or 0 + avg_time_spent_on_queries = Request.objects.aggregate(Avg('meta_time_spent_queries'))['meta_time_spent_queries__avg'] or 0 + + longest_queries_by_view = Request.objects.order_by('-time_taken')[:10] + most_time_spent_in_db = Request.objects.order_by('-meta_time_spent_queries')[:10] + most_queries = Request.objects.order_by('-meta_num_queries')[:10] + + options_view_style = [{'value': 'row', 'label': 'Row'}, {'value': 'grid', 'label': 'Grid'}] + options_show = [25, 50, 100] + options_order_by = [{'value': 'start_time', 'label': 'Start Time'}, {'value': 'time_taken', 'label': 'Time Taken'}] + options_order_dir = [{'value': 'asc', 'label': 'Ascending'}, {'value': 'desc', 'label': 'Descending'}] + + context = { + 'num_requests': num_requests, + 'avg_overall_time': avg_overall_time, + 'avg_num_queries': avg_num_queries, + 'avg_time_spent_on_queries': avg_time_spent_on_queries, + 'longest_queries_by_view': longest_queries_by_view, + 'most_time_spent_in_db': most_time_spent_in_db, + 'most_queries': most_queries, + 'view_style': request.GET.get('view_style', 'row'), + 'show': request.GET.get('show', 25), + 'order_by': request.GET.get('order_by', 'start_time'), + 'order_dir': request.GET.get('order_dir', 'asc'), + 'options_view_style': options_view_style, + 'options_show': options_show, + 'options_order_by': options_order_by, + 'options_order_dir': options_order_dir, + } + + return render(request, 'silk/summary.html', context) + class Response(models.Model): id = CharField(max_length=36, default=uuid4, primary_key=True) diff --git a/silk/templates/silk/summary.html b/silk/templates/silk/summary.html index c70c46f9..1cddba04 100644 --- a/silk/templates/silk/summary.html +++ b/silk/templates/silk/summary.html @@ -1,88 +1,127 @@ {% extends 'silk/base/root_base.html' %} {% load silk_inclusion %} {% load static %} -{% block menu %} - {% root_menu request %} -{% endblock %} {% block pagetitle %}Silky - Summary{% endblock %} {% block style %} {{ block.super }} - + + +{% endblock %} + +{% block menu %} + {% root_menu request %} {% endblock %} {% block js %} {{ block.super }} - + {% endblock %} + +{% block filter %} +
+ + + + + {{ block.super }} +{% endblock %} + {% block data %}
-
-
- {% csrf_token %} -
- - - - - -
- - - -
- Using requests that executed - - - seconds ago, - -
-
- before - - , - -
-
- and after - - . - -
-
- -

Summary

{% if num_requests %}
@@ -90,59 +129,71 @@

Summary

Requests
-
{{ num_profiles }}
-
Profiles
-
-
-
{{ avg_overall_time | floatformat:0 }}ms
+
{{ avg_overall_time|floatformat:0 }}ms
Avg. Time
-
{{ avg_num_queries | floatformat:2 }}
+
{{ avg_num_queries|floatformat:2 }}
Avg. #Queries
-
{{ avg_time_spent_on_queries |floatformat:0 }}ms
+
{{ avg_time_spent_on_queries|floatformat:0 }}ms
Avg. DB Time
{% else %}

No data

{% endif %} +

Most Time Overall

{% if longest_queries_by_view %} - {% for x in longest_queries_by_view %} - - {% request_summary x %} - - {% endfor %} + {% else %}

No data

{% endif %} +

Most Time Spent in Database

{% if most_time_spent_in_db %} - {% for x in most_time_spent_in_db %} - - {% request_summary x %} - - {% endfor %} + {% else %}

No data

{% endif %} +

Most Database Queries

{% if most_queries %} - {% for x in most_queries %} - - {% request_summary x %} - - {% endfor %} + {% else %}

No data

{% endif %}
- {% endblock %} - -{# Hide filter hamburger menu #} -{% block top %}{% endblock %} -{% block filter %}{% endblock %} From 3acca2762dd169c7102135f17e79104467f0009f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 31 May 2024 08:39:52 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- silk/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/silk/models.py b/silk/models.py index ea78894c..61683b16 100644 --- a/silk/models.py +++ b/silk/models.py @@ -8,8 +8,10 @@ from django.conf import settings from django.db import models, transaction from django.db.models import ( + Avg, BooleanField, CharField, + Count, DateTimeField, FileField, FloatField, @@ -20,14 +22,14 @@ TextField, ) from django.shortcuts import render -from django.db.models import Avg, Count -from .models import Request from django.utils import timezone from django.utils.safestring import mark_safe from silk.config import SilkyConfig from silk.utils.profile_parser import parse_profile +from .models import Request + try: # New in Django 4.2 from django.core.files.storage import storages