From acb8345a7796345cfb42f0ffc09e97278cf29b7f Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Mon, 10 Apr 2017 17:27:11 +0000 Subject: [PATCH] Added some debug functionality which makes it possible to see from where (python source file and line) an SQL query comes when looking at the sql query summary available at the bottom of pages in debug mode, on INTERNAL_IPS. - Legacy-Id: 13188 --- ietf/static/ietf/css/ietf.css | 19 +++ ietf/templates/debug.html | 46 +++++- patch/README | 10 ++ patch/trace-django-queryset-origin.patch | 196 +++++++++++++++++++++++ 4 files changed, 268 insertions(+), 3 deletions(-) create mode 100644 patch/README create mode 100644 patch/trace-django-queryset-origin.patch diff --git a/ietf/static/ietf/css/ietf.css b/ietf/static/ietf/css/ietf.css index 49b0f0f99..6a2911985 100644 --- a/ietf/static/ietf/css/ietf.css +++ b/ietf/static/ietf/css/ietf.css @@ -824,3 +824,22 @@ blockquote { page-break-before: always; } } + +.modal-max { + width: 90%; +} + +#debug-query-table .code { + background-color: #eee; + font-family: 'PT Mono', monospace; + font-size: 0.8em; + word-break: break-all; +} + +#debug-query-table table { + max-width: 100%; +} + +#debug-query-table .sql { + border-top: 1px solid #555; +} diff --git a/ietf/templates/debug.html b/ietf/templates/debug.html index 07e4dd7a5..2d8ab7e39 100644 --- a/ietf/templates/debug.html +++ b/ietf/templates/debug.html @@ -13,7 +13,7 @@ onclick="$('#debug-query-table').toggleClass('hide');">Show {% endif %}

- +
@@ -28,12 +28,52 @@ {% with sql_queries|annotate_sql_queries as sql_query_info %} {% for query in sql_query_info %} - + - + diff --git a/patch/README b/patch/README new file mode 100644 index 000000000..1b355b6db --- /dev/null +++ b/patch/README @@ -0,0 +1,10 @@ + +This directory is for patches to the datatracker environment, not for patches +to the datatracker itself (those are ephemeral, in the sense that they should +not stay around as patches, they should be committed to the repository if they +are of long-term value). + +As an example, trace-django-queryset-origin.patch is a patch to django 1.10 +which adds some meta-information to the query entries provided for debug +purpose by django.template.context_processors.debug as sql_queries. + diff --git a/patch/trace-django-queryset-origin.patch b/patch/trace-django-queryset-origin.patch new file mode 100644 index 000000000..d23eb8357 --- /dev/null +++ b/patch/trace-django-queryset-origin.patch @@ -0,0 +1,196 @@ +--- ../x/env/lib/python2.7/site-packages/django/db/models/query.py 2017-04-07 15:10:29.426831000 -0700 ++++ env/lib/python2.7/site-packages/django/db/models/query.py 2017-04-07 15:18:39.938521412 -0700 +@@ -5,6 +5,8 @@ + import copy + import sys + import warnings ++import inspect ++import logging + from collections import OrderedDict, deque + + from django.conf import settings +@@ -34,6 +36,7 @@ + # Pull into this namespace for backwards compatibility. + EmptyResultSet = sql.EmptyResultSet + ++logger = logging.getLogger('django.db.backends') + + class BaseIterable(object): + def __init__(self, queryset): +@@ -51,6 +54,7 @@ + compiler = queryset.query.get_compiler(using=db) + # Execute the query. This will also fill compiler.select, klass_info, + # and annotations. ++ + results = compiler.execute_sql() + select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info, + compiler.annotation_col_map) +@@ -174,6 +178,8 @@ + self._known_related_objects = {} # {rel_field, {pk: rel_obj}} + self._iterable_class = ModelIterable + self._fields = None ++ self._origin = [] ++ self._djangodir = __file__[:(__file__.index('django')+len('django')+1)] + + def as_manager(cls): + # Address the circular dependency between `Queryset` and `Manager`. +@@ -316,6 +322,37 @@ + combined.query.combine(other.query, sql.OR) + return combined + ++ def _add_origin(self, depth=1): ++ if settings.DEBUG: ++ # get list of frame records. Each is: ++ # [ frame, filename, lineno, function, code_context, index ] ++ stack = inspect.stack() ++ # caller stack record ++ method = stack[depth][3] ++ # look for the first stack entry which is not from django ++ i = 0 ++ while i
#
{{ forloop.counter }} {{ query.sql|expand_comma|escape }} {{ query.count }} {{ query.where }}{{ query.loc }} + {{ query.loc }} + {% if query.origin %} + + {% endif %} + + {{ query.time }} {{ query.time_accum }}