Skip to content

Commit

Permalink
feat: adds an explain view with an embedded PEV
Browse files Browse the repository at this point in the history
  • Loading branch information
uptickmetachu committed Jan 22, 2024
1 parent 75ee5fe commit 3cac384
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 218 deletions.
90 changes: 44 additions & 46 deletions django_pev/templates/django_pev/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,52 @@

<body class="app">
<!-- Header content here -->
<header class="header">
<a class="header-brand" href="#">
Django PEV
</a>
</header>

<div class="app-body">
<!-- Sidebar content here -->
<div id="sidebar" class="sidebar sidebar-show sidebar-fixed">
<div class="sidebar-brand d-none d-md-flex">
<h3> Django PEV </h3>
</div>
<ul class="sidebar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:queries' %}">
<i class="nav-icon fa-solid fa-clipboard-question"></i> Queries
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:space' %}">
<i class="nav-icon fa-solid fa-chart-pie"></i> Space
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:indexes' %}">
<i class="nav-icon fa-solid fa-bookmark"></i> Indexes
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:live-queries' %}">
<i class="nav-icon fa-solid fa-play"></i> Live Queries
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:connections' %}">
<i class="nav-icon fa-solid fa-wifi "></i> Connections
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:maintenance' %}">
<i class="nav-icon fa-solid fa-hammer"></i> Table Maintenance
</a>
</li>
</ul>
<!-- Sidebar content here -->
<div id="sidebar" class="sidebar sidebar-show sidebar-fixed">
<div class="sidebar-brand d-none d-md-flex">
<h3> Django PEV </h3>
</div>
<ul class="sidebar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:queries' %}">
<i class="nav-icon fa-solid fa-clipboard-question"></i> Queries
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:space' %}">
<i class="nav-icon fa-solid fa-chart-pie"></i> Space
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:indexes' %}">
<i class="nav-icon fa-solid fa-bookmark"></i> Indexes
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:live-queries' %}">
<i class="nav-icon fa-solid fa-play"></i> Live Queries
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:connections' %}">
<i class="nav-icon fa-solid fa-wifi "></i> Connections
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:explain' %}">
<i class="nav-icon fa-solid fa-ambulance"></i> Explain Query
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'django_pev:maintenance' %}">
<i class="nav-icon fa-solid fa-hammer"></i> Table Maintenance
</a>
</li>
</ul>
</div>
<div class="wrapper d-flex flex-column min-vh-100 bg-light">
<div class="body flex-grow-1 px-3">
<div class="container-lg">
<div class="sidebar-open container-lg">
{% block content %}
<div class="card">
<div class="card-body">
Expand All @@ -72,7 +71,6 @@ <h3> Django PEV </h3>
</div>
<!-- Main content here -->
</div>
{% comment %} End app-body {% endcomment %}
</div>

<footer class="app-footer">
Expand Down
90 changes: 90 additions & 0 deletions django_pev/templates/django_pev/explain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{% extends "django_pev/base.html" %}
{% block content %}
{% load humanize %}
<div class="row">
<div class="card mb-4 mt-4">
<div class="card-body">
<div class="d-flex justify-content-between">
<div>
<h4 class="card-title mb-0">Explain Query</h4>
This page helps find the slowest queries of a page and explains it using an embedded version of <a href="https://explain.dalibo.com">Postgres Explain Visualizer 2 (by dalibo)</a>
</div>
</div>
<div class="d-flex justify-content-between mt-4">
<form action="/django-pev/explain" method="post">
{% csrf_token %}
<label for="id_url" class="form-label"> <b>Page url </b></label>
<input type="text" class="form-control" name="url" value="/api/v2/tasks/" required="true" id="id_url">
<div id="helpBlock" class="form-text">
The url should not include the protocol or the base url. For example (/dashboard/) .
</div>
<input class="btn btn-primary" type="submit" value="Submit" />
</form>
</div>

{% if explain %}

<div class="card p-4 mt-1">
<h5> Slowest Query {{ slowest.duration|floatformat:4 }}s
</h5>
<form action="{% url 'django_pev:explain-visualize' %}" method="post">
{% csrf_token %}
<input type="hidden" name="explainset_id" value="{{explain.id}}" />
<input type="hidden" name="query_index" value="{{slowest.index}}" />
<input class="btn btn-primary" type="submit" value="Analyze">
</input>
</form>
<div>
<code>
{{ slowest.sql }}
</code>
</div>
</div>

<div class="mt-4">
<h5> All Queries {{explain.queries | length}}</h5>
<ol>
{% for query in explain.queries %}

<li>
{{ query.duration | floatformat:4}}s
<code>{{ query.sql | truncatechars:100 }}</code>

<p class="d-inline-flex gap-1">

<form action="{% url 'django_pev:explain-visualize' %}" method="post">
{% csrf_token %}
<input type="hidden" name="explainset_id" value="{{explain.id}}" />
<input type="hidden" name="query_index" value="{{query.index}}" />
<input class="btn btn-primary" type="submit" value="Analyze" />
<button class="btn btn-secondary" type="button" data-coreui-toggle="collapse" data-coreui-target="#query{{query.index}}" aria-expanded="false" aria-controls="collapseExample">
Show Query
</button>
</form>

</p>

<div class="collapse" id="query{{query.index}}">
<div class="card card-body">
<code>
{{ query.sql }}
</code>
</div>
</div>
</li>

{% endfor %}
</ol>
</div>

{% endif %}

</div>



<div class="card-footer">
</div>
</div>
</div>
{% endblock %}
31 changes: 31 additions & 0 deletions django_pev/templates/django_pev/pev_embedded.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script src="https://unpkg.com/[email protected]/dist/vue.global.prod.js"></script>
<script src="https://unpkg.com/pev2/dist/pev2.umd.js"></script>
<link
href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="https://unpkg.com/pev2/dist/style.css" />

<div id="embedded_pev">
<pev2 :plan-source="plan" :plan-query="query" />
</div>

<script>
const { createApp } = Vue;

const plan = `{{plan |safe}}`;
const query = `{{query |safe}}`;

const app = createApp({
data() {
return {
plan: plan,
query: query,
}
},
})

app.component("pev2", pev2.Plan)

app.mount("#embedded_pev")
</script>
3 changes: 3 additions & 0 deletions django_pev/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
path("indexes", views.IndexesView.as_view(), name="indexes"),
path("live-queries", views.LiveQueriesView.as_view(), name="live-queries"),
path("queries", views.QueriesView.as_view(), name="queries"),
path("explain", views.ExplainView.as_view(), name="explain"),
path("explain-visualize", views.ExplainVisualize.as_view(), name="explain-visualize"),
path("embedded-pev", views.EmbeddedPev.as_view(), name="embedded-pev"),
]
151 changes: 0 additions & 151 deletions django_pev/utils.py

This file was deleted.

Loading

0 comments on commit 3cac384

Please sign in to comment.