From 2661e74a8ed7ec7188cc7d7ce1677e1237971787 Mon Sep 17 00:00:00 2001
From: William Chu
Date: Wed, 19 Jun 2024 10:52:06 +1000
Subject: [PATCH 1/3] feat: display tracebacks when explaining sql
---
django_pev/templates/django_pev/explain.html | 22 ++++++----
.../templates/django_pev/pev_embedded.html | 5 +--
django_pev/utils/__init__.py | 40 ++++++++++++-------
3 files changed, 42 insertions(+), 25 deletions(-)
diff --git a/django_pev/templates/django_pev/explain.html b/django_pev/templates/django_pev/explain.html
index d3c6001..5b334c8 100644
--- a/django_pev/templates/django_pev/explain.html
+++ b/django_pev/templates/django_pev/explain.html
@@ -35,9 +35,7 @@ Slowest Query {{ slowest.duration|floatformat:4 }}s
-
- {{ slowest.sql }}
-
+
{{ slowest.sql }}
@@ -57,17 +55,27 @@ All Queries {{explain.queries | length}}
-
-
+
+
-
- {{ query.sql }}
+ {{ query.stack_trace}}
+
diff --git a/django_pev/templates/django_pev/pev_embedded.html b/django_pev/templates/django_pev/pev_embedded.html
index 8bad913..28cd817 100644
--- a/django_pev/templates/django_pev/pev_embedded.html
+++ b/django_pev/templates/django_pev/pev_embedded.html
@@ -1,5 +1,3 @@
-{% extends "django_pev/base.html" %}
-{% block content %}
Explain for: {{url}} ({{duration|floatformat:4}}s)
app.component("pev2", pev2.Plan)
app.mount("#embedded_pev")
-
-{%endblock%}
\ No newline at end of file
+
\ No newline at end of file
diff --git a/django_pev/utils/__init__.py b/django_pev/utils/__init__.py
index ef2e892..41da52b 100644
--- a/django_pev/utils/__init__.py
+++ b/django_pev/utils/__init__.py
@@ -27,21 +27,33 @@
CursorWrapper._original_executemany = CursorWrapper._executemany # type:ignore
-@contextmanager
-def record_sql(
- self: CursorWrapper,
- sql: str,
- params: Any,
-):
+class record_sql:
"""Record the SQL query and parameters for use in the explain view"""
- thread_local_query_count.query_count = getattr(thread_local_query_count, "query_count", 0) + 1
- start_time = time.time()
- try:
- yield
- finally:
- duration = time.time() - start_time
+
+ def __init__(self, cursor_wrapper: CursorWrapper, sql: str, params: Any):
+ """Record the SQL query and parameters for use in the explain view"""
+ self.cursor_wrapper = cursor_wrapper
+ self.sql = sql
+ self.params = params
+ self.start_time = time.time()
+ self.stack_trace = ""
+
+ def __enter__(self):
+ thread_local_query_count.query_count = getattr(thread_local_query_count, "query_count", 0) + 1
+ frame_stack = traceback.extract_stack(limit=100)
+ filtered_frame_stack = [
+ x for x in frame_stack if not ("django_pev" in x.filename or "django/db" in x.filename)
+ ][-10:]
+ self.stack_trace = "".join(traceback.format_list(filtered_frame_stack))
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ duration = time.time() - self.start_time
thread_local_query_count.queries = getattr(thread_local_query_count, "queries", []) + [
- {"time": duration, "sql": self.cursor.mogrify(sql, params).decode("utf-8")}
+ {
+ "time": duration,
+ "sql": self.cursor_wrapper.cursor.mogrify(self.sql, self.params).decode("utf-8"),
+ "stack_trace": self.stack_trace,
+ }
]
@@ -164,7 +176,7 @@ def explain(
index=index,
duration=float(q["time"]),
sql=sqlparse.format(q["sql"], reindent=True, keyword_case="upper"),
- stack_trace="".join(traceback.format_stack()[-trace_limit:-2]),
+ stack_trace=q["stack_trace"],
db_alias=db_alias,
)
)
From ca2e523321b3b71ca9b1ffc4d93b5b5faf54ae32 Mon Sep 17 00:00:00 2001
From: William Chu
Date: Wed, 19 Jun 2024 10:52:39 +1000
Subject: [PATCH 2/3] chore: bump version
---
pyproject.toml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pyproject.toml b/pyproject.toml
index 3b06315..4d86ee6 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-pev"
-version = "0.1.13"
+version = "0.2.0"
description = "Context manager to upload explain plans to https://explain.dalibo.com/"
authors = ["william chu "]
readme = "README.md"
From 5dfed889c5f5cf7fc06e14ad8b3ba1edbe999c51 Mon Sep 17 00:00:00 2001
From: William Chu
Date: Wed, 19 Jun 2024 10:53:11 +1000
Subject: [PATCH 3/3] ci: add permissions
---
.github/workflows/ci.yaml | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 2623f47..9089612 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -2,17 +2,19 @@ name: CI
on:
push:
branches:
- - release/*
- - develop
- - master
- - main
+ - release/*
+ - develop
+ - master
+ - main
pull_request: {}
permissions:
id-token: write # Required for federated aws oidc
- contents: read
actions: read
- pull-requests: write
+ contents: write # to be able to publish a GitHub release
+ issues: write # to be able to comment on released issues
+ pull-requests: write # to be able to comment on released pull requests
+
jobs:
ci: