Skip to content

Commit

Permalink
Merge pull request #8 from uptick/feat/tracebacks
Browse files Browse the repository at this point in the history
feat/tracebacks
  • Loading branch information
uptickmetachu authored Jun 19, 2024
2 parents eaa9e65 + 5dfed88 commit fec9597
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 15 additions & 7 deletions django_pev/templates/django_pev/explain.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ <h5> Slowest Query {{ slowest.duration|floatformat:4 }}s
</input>
</form>
<div>
<code>
{{ slowest.sql }}
</code>
<code><pre>{{ slowest.sql }}</pre></code>
</div>
</div>

Expand All @@ -57,17 +55,27 @@ <h5> All Queries {{explain.queries | length}}</h5>
<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">
<button class="btn btn-secondary" type="button" data-coreui-toggle="collapse" data-coreui-target="#query{{query.index}}-sql" aria-expanded="false" aria-controls="collapseExample">
Show Query
</button>
<button class="btn btn-secondary" type="button" data-coreui-toggle="collapse" data-coreui-target="#query{{query.index}}-tb" aria-expanded="false" aria-controls="collapseExample">
Show Traceback
</button>
</form>

</p>

<div class="collapse" id="query{{query.index}}">
<div class="collapse" id="query{{query.index}}-sql">
<div class="card card-body">
<code><pre>{{ query.sql }}
</pre>
</code>
</div>
</div>
<div class="collapse" id="query{{query.index}}-tb">
<div class="card card-body">
<code>
{{ query.sql }}
<code><pre>{{ query.stack_trace}}
</pre>
</code>
</div>
</div>
Expand Down
5 changes: 1 addition & 4 deletions django_pev/templates/django_pev/pev_embedded.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% extends "django_pev/base.html" %}
{% block content %}
<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
Expand Down Expand Up @@ -31,5 +29,4 @@ <h5>Explain for: {{url}} ({{duration|floatformat:4}}s)</h5>
app.component("pev2", pev2.Plan)

app.mount("#embedded_pev")
</script>
{%endblock%}
</script>
40 changes: 26 additions & 14 deletions django_pev/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
]


Expand Down Expand Up @@ -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,
)
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit fec9597

Please sign in to comment.