Skip to content

Commit

Permalink
Merge branch 'master' into fix_ddt_420
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy authored Aug 14, 2023
2 parents 71e4f0a + d8d0ef8 commit e6deb64
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
3.1.0
* Add a stacktrace to the records to show where the searches were originated #13

3.0.2
* Just CI auto deploy fixes

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ Django Elasticsearch Toolbar
============================

A Django Debug Toolbar panel for Elasticsearch
[![Build Status](https://travis-ci.org/Benoss/django-elasticsearch-debug-toolbar.svg?branch=master)](https://travis-ci.org/Benoss/django-elasticsearch-debug-toolbar)
[![PyPI version](https://badge.fury.io/py/django-elasticsearch-debug-toolbar.svg)](https://badge.fury.io/py/django-elasticsearch-debug-toolbar)

About
Expand Down
16 changes: 14 additions & 2 deletions elastic_panel/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import traceback
import threading


from debug_toolbar.panels import Panel
from debug_toolbar.utils import (
get_module_path,
get_stack,
hidden_paths,
render_stacktrace,
tidy_stacktrace,
)
from django.templatetags.static import static
from django.utils.translation import gettext_lazy as _
from elasticsearch.connection.base import Connection
Expand All @@ -24,7 +32,7 @@ def clear_collection(self):
self.data.collection = []


# Patching og the orginal elasticsearch log_request
# Patching of the original elasticsearch log_request
old_log_request_success = Connection.log_request_success
collector = ThreadCollector()

Expand All @@ -46,6 +54,9 @@ def _pretty_json(data):
return data


hidden_paths.append(get_module_path(__name__))


class ElasticQueryInfo:
def __init__(self, method, full_url, path, body, status_code, response, duration):
if not body:
Expand All @@ -54,7 +65,6 @@ def __init__(self, method, full_url, path, body, status_code, response, duration
self.body = _pretty_json(body)
if isinstance(self.body, bytes):
self.body = self.body.decode("ascii", "ignore")
self.traceback = "".join(traceback.format_stack()[:-2])
self.method = method
self.full_url = full_url
self.path = path
Expand All @@ -64,6 +74,7 @@ def __init__(self, method, full_url, path, body, status_code, response, duration
self.hash = hashlib.md5(
self.full_url.encode("ascii", "ignore") + self.body.encode("ascii", "ignore")
).hexdigest()
self.stacktrace = tidy_stacktrace(reversed(get_stack()))


class ElasticDebugPanel(Panel):
Expand Down Expand Up @@ -114,6 +125,7 @@ def generate_stats(self, request, response):
if record.hash in hashs:
self.nb_duplicates += 1
hashs.add(record.hash)
record.stacktrace = render_stacktrace(record.stacktrace)

self.nb_queries = len(records)

Expand Down
6 changes: 4 additions & 2 deletions elastic_panel/static/elastic_panel/js/elastic_panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ var uarr = String.fromCharCode(0x25b6),

var showHandler = function(e) {

toggle(this.nextElementSibling)
if(this.nextElementSibling) {
toggle(this.nextElementSibling)
}

var arrow = document.querySelector('a.elasticShowTemplate .toggleArrow')
var arrow = this.children[0];
arrow.textContent = arrow.textContent == uarr ? darr : uarr

toggle(this.parentNode.nextElementSibling)
Expand Down
8 changes: 4 additions & 4 deletions elastic_panel/templates/elastic_panel/elastic_panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
<dd>
<div class="djTemplateShowContextDiv">
<a class="elasticShowTemplate">
<span class="toggleArrow"></span> Show Traceback
<span class="toggleArrow"></span> Show Stacktrace
</a>
<a class="selectText" style="display: none;"> -&gt; (Select All Json for ctrl + c)</a>
</div>
<div class="djTemplateHideContextDiv" style="display: none;">
<code class="json_code" contenteditable="true" >{{record.traceback}}</code>
<pre class="djdt-stack">
{{record.stacktrace}}
</pre>
</div>
</dd>
</dt>
{% endfor %}
</dl>
{% else %}
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
env =
DJANGO_SETTINGS_MODULE=tests.settings
2 changes: 2 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ django-debug-toolbar>=3.0
elasticsearch<8.0.0

pytest
pytest-env

isort
flake8
flake8-print # Forbid print statement in code use logging. instead
Expand Down
26 changes: 26 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

INSTALLED_APPS = ["debug_toolbar", "elastic_panel"]

DEBUG_TOOLBAR_PANELS = [
"elastic_panel.panel.ElasticDebugPanel",
]
SECRET_KEY = "test"
DEBUG = True

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
},
}

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR + "/tests/templates"],
"APP_DIRS": True,
},
]
10 changes: 6 additions & 4 deletions tests/test_toolbar.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import unittest

from django.conf import settings
import django

django.setup()

settings.configure()

from debug_toolbar.toolbar import DebugToolbar # noqa: E402
from django.http import HttpResponse # noqa: E402
from django.test import RequestFactory # noqa: E402
from django.test import TestCase
from elasticsearch.connection import Connection # noqa: E402

from elastic_panel import panel # noqa: E402


class ImportTest(unittest.TestCase):
class ImportTest(TestCase):
def test_input(self):
panel.ElasticQueryInfo("GET", "asdasd", "asdasd", "{}", 200, "adssad", 1)
panel.ElasticQueryInfo("GET", "asdasd", "asdasd", "", 200, "adssad", 1)
Expand All @@ -21,7 +23,7 @@ def test_input(self):
panel.ElasticQueryInfo("GET", "asdasd", "asdasd", b"{'asddsa': 'asddasds'}", 200, "adssad", 1)


class PanelTests(unittest.TestCase):
class PanelTests(TestCase):
def setUp(self):
self.get_response = lambda request: HttpResponse()
self.request = RequestFactory().get("/")
Expand Down

0 comments on commit e6deb64

Please sign in to comment.