From 0413214b872a5a6a29c7125fe6a3b24605d1bc87 Mon Sep 17 00:00:00 2001 From: Dan Breen Date: Fri, 11 Sep 2020 16:30:40 -0400 Subject: [PATCH 1/3] Add a stacktrace to the records to show where the searches were originated --- elastic_panel/panel.py | 8 +++++++- .../static/elastic_panel/js/elastic_panel.js | 6 ++++-- .../templates/elastic_panel/elastic_panel.html | 13 ++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/elastic_panel/panel.py b/elastic_panel/panel.py index 63070c1..4ee3454 100644 --- a/elastic_panel/panel.py +++ b/elastic_panel/panel.py @@ -2,7 +2,8 @@ from django.template.loader import render_to_string from django.utils.translation import ugettext_lazy as _ from debug_toolbar.panels import Panel -from debug_toolbar.utils import ThreadCollector +from debug_toolbar.utils import ThreadCollector, get_stack, tidy_stacktrace, render_stacktrace, \ + get_module_path, hidden_paths import hashlib import json @@ -30,6 +31,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: @@ -47,6 +51,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): @@ -91,6 +96,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) diff --git a/elastic_panel/static/elastic_panel/js/elastic_panel.js b/elastic_panel/static/elastic_panel/js/elastic_panel.js index 53cecd6..7b8380e 100644 --- a/elastic_panel/static/elastic_panel/js/elastic_panel.js +++ b/elastic_panel/static/elastic_panel/js/elastic_panel.js @@ -12,9 +12,11 @@ var uarr = String.fromCharCode(0x25b6), var showHandler = function(e) { - toggle(this.nextElementSibling) + if(this.nextElementSibling) { + toggle(this.nextElementSibling) + } - arrow = document.querySelector('a.elasticShowTemplate .toggleArrow') + arrow = this.children[0] arrow.textContent = arrow.textContent == uarr ? darr : uarr toggle(this.parentNode.nextElementSibling) diff --git a/elastic_panel/templates/elastic_panel/elastic_panel.html b/elastic_panel/templates/elastic_panel/elastic_panel.html index 5556d54..df0ced2 100644 --- a/elastic_panel/templates/elastic_panel/elastic_panel.html +++ b/elastic_panel/templates/elastic_panel/elastic_panel.html @@ -32,7 +32,18 @@ {{record.response}} - +
+ + +
{% endfor %} {% else %} From 71ae949c99a5853d0228b5eb1626cab3f4b369a2 Mon Sep 17 00:00:00 2001 From: Dan Breen Date: Mon, 21 Feb 2022 13:30:43 -0500 Subject: [PATCH 2/3] Remove unused import --- elastic_panel/panel.py | 1 - 1 file changed, 1 deletion(-) diff --git a/elastic_panel/panel.py b/elastic_panel/panel.py index 65e4ed8..796a669 100644 --- a/elastic_panel/panel.py +++ b/elastic_panel/panel.py @@ -1,6 +1,5 @@ import hashlib import json -import traceback from debug_toolbar.panels import Panel from debug_toolbar.utils import ThreadCollector, get_stack, tidy_stacktrace, render_stacktrace, \ From d8d0ef8b62798cc5d75b1d9cea0ceb42f7b24de7 Mon Sep 17 00:00:00 2001 From: benoit Date: Tue, 22 Feb 2022 14:23:47 +1300 Subject: [PATCH 3/3] Add stacktrace and fix tests using templates --- CHANGES | 3 +++ README.md | 1 - elastic_panel/panel.py | 12 +++++++++--- pytest.ini | 3 +++ tests/requirements.txt | 2 ++ tests/settings.py | 26 ++++++++++++++++++++++++++ tests/test_toolbar.py | 10 ++++++---- 7 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 pytest.ini create mode 100644 tests/settings.py diff --git a/CHANGES b/CHANGES index f34ef11..b66ca95 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/README.md b/README.md index 262788a..49114cd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/elastic_panel/panel.py b/elastic_panel/panel.py index 796a669..70dfe9c 100644 --- a/elastic_panel/panel.py +++ b/elastic_panel/panel.py @@ -2,13 +2,19 @@ import json from debug_toolbar.panels import Panel -from debug_toolbar.utils import ThreadCollector, get_stack, tidy_stacktrace, render_stacktrace, \ - get_module_path, hidden_paths +from debug_toolbar.utils import ( + ThreadCollector, + 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 -# Patching og the orginal elasticsearch log_request +# Patching og the original elasticsearch log_request old_log_request_success = Connection.log_request_success collector = ThreadCollector() diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..daed00c --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +env = + DJANGO_SETTINGS_MODULE=tests.settings diff --git a/tests/requirements.txt b/tests/requirements.txt index 7e5c538..43dd717 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -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 diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..a1307cb --- /dev/null +++ b/tests/settings.py @@ -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, + }, +] diff --git a/tests/test_toolbar.py b/tests/test_toolbar.py index 5d3b667..e63d8a8 100644 --- a/tests/test_toolbar.py +++ b/tests/test_toolbar.py @@ -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) @@ -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("/")