Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref #365 Added Clear DB page #368

Merged
merged 4 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions silk/templates/silk/clear_db.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{% extends 'silk/base/root_base.html' %}
{% load static %}
{% load silk_inclusion %}

{% block menu %}
{% if silk_request %}
{% request_menu request silk_request %}
{% else %}
{% root_menu request %}
{% endif %}
{% endblock %}

{% block style %}
{{ block.super }}
<style>
.container {
padding: 0 1em;
}

h2 {
margin-bottom: 10px;
}

.cleardb-form .cleardb-form-wrapper{
margin-bottom: 20px;
}

.cleardb-form label {
display: block;
margin-bottom: 8px;
}

.cleardb-form .btn {
background: #333344;
color: #fff;
padding: 10px 20px;
border-radius: 2px;
cursor: pointer;
box-shadow: none;
font-size: 16px;
line-height: 20px;
border: 0;
min-width: 150px;
text-align: center;
}
.cleardb-form label :last-child {
margin-bottom: 0;
}
.msg {
margin-top: 20px;
color: #bac54b;
}
</style>

{% endblock %}

{% block js %}
{{ block.super }}
<script>

$(document).ready(function () {
initFilters();
initFilterButton();
});
</script>
{% endblock %}


{% block data %}
<div class="container">
<h2>Silk Clear DB</h2>
<form class="cleardb-form" action="." method="post">
{% csrf_token %}
<div class="cleardb-form-wrapper">
<label>
<input type="checkbox" name="clear_requests" disabled="disabled"/>
Requests
</label>
<label>
<input type="checkbox" name="clear_profiling" disabled="disabled"/>
Profiling
</label>
<label>
<input type="checkbox" name="clear_all"/>
All
</label>
</div>
<button class="btn">Clear</button>
</form>
<div class="msg">{{ msg }}</div>
</div>
{% endblock %}


{% block filter %}{% endblock %}
{% block filters %}{% endblock %}
7 changes: 7 additions & 0 deletions silk/templates/silk/inclusion/root_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@
</div>
</a>
</div>
<div class="menu-item selectable-menu-item {% navactive request 'silk:cleardb' %}">
<a href="{% url "silk:cleardb" %}">
<div class="menu-item-outer">
<div class="menu-item-inner">Clear DB</div>
</div>
</a>
</div>

2 changes: 2 additions & 0 deletions silk/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf.urls import url

from silk.views.clear_db import ClearDBView
from silk.views.profile_detail import ProfilingDetailView
from silk.views.profile_download import ProfileDownloadView
from silk.views.profile_dot import ProfileDotView
Expand Down Expand Up @@ -82,6 +83,7 @@
name='profile_sql_detail'
),
url(r'^profiling/$', ProfilingView.as_view(), name='profiling'),
url(r'^cleardb/$', ClearDBView.as_view(), name='cleardb'),
url(
r'^request/(?P<request_id>[a-zA-Z0-9\-]+)/cprofile/$',
CProfileView.as_view(),
Expand Down
33 changes: 33 additions & 0 deletions silk/views/clear_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.db import connection
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.generic import View

from silk.auth import login_possibly_required, permissions_possibly_required
from silk.models import Request, Response, SQLQuery, Profile


class ClearDBView(View):

def _truncate_tables(self, models):
raw_query = 'TRUNCATE TABLE {0};'
truncate_query = [raw_query.format(m._meta.db_table) for m in models]
truncate_query = ' '.join(truncate_query)
query = 'SET FOREIGN_KEY_CHECKS = 0; {0} SET FOREIGN_KEY_CHECKS = 1;'.format(truncate_query)
cursor = connection.cursor()
cursor.execute(query)

@method_decorator(login_possibly_required)
@method_decorator(permissions_possibly_required)
def get(self, request, *_, **kwargs):
return render(request, 'silk/clear_db.html')

@method_decorator(login_possibly_required)
@method_decorator(permissions_possibly_required)
def post(self, request, *_, **kwargs):
context = {}
if 'clear_all' in request.POST:
self._truncate_tables([Response, SQLQuery, Profile, Request])
tables = ['Response', 'SQLQuery', 'Profile', 'Request']
context['msg'] = 'Cleared data for following silk tables: {0}'.format(', '.join(tables))
return render(request, 'silk/clear_db.html', context=context)