From 5e776d37fe72c0d7adec38cc4acdc8566196b1c0 Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 14:35:17 -0300 Subject: [PATCH 1/8] applying patch from issue #27. --- debug_toolbar/panels/sql.py | 17 ++++++++++++++++- .../templates/debug_toolbar/panels/sql.html | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index e1e9bdf9c..47e0119e5 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -148,13 +148,28 @@ def nav_title(self): def nav_subtitle(self): self._queries = connection.queries[self._offset:] + + self._duplicate_sql_time = 0 + self.seen = {} + self.duplicate = 0 + for q in self._queries: + sql = q["sql"] + c = self.seen.get(sql, 0) + if c: + self.duplicate += 1 + self._duplicate_sql_time += q['duration'] + q["seen"] = c + self.seen[sql] = c + 1 + self._sql_time = sum([q['duration'] for q in self._queries]) num_queries = len(self._queries) # TODO l10n: use ngettext return "%d %s in %.2fms" % ( num_queries, (num_queries == 1) and 'query' or 'queries', - self._sql_time + self._sql_time, + self.duplicate, + self._duplicate_sql_time, ) def title(self): diff --git a/debug_toolbar/templates/debug_toolbar/panels/sql.html b/debug_toolbar/templates/debug_toolbar/panels/sql.html index 9ed87ca38..76ea4e19a 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/sql.html +++ b/debug_toolbar/templates/debug_toolbar/panels/sql.html @@ -2,6 +2,7 @@ + @@ -11,6 +12,7 @@ {% for query in queries %} +
{% trans "Seen times" %} {% trans "Time" %} (ms) {% trans "Action" %} {% trans 'Stacktrace' %}
{{ query.seen }} {{ query.duration|floatformat:"2" }} {% if query.params %} From 99515b6f8ebef632e15dafbd14463ddd8078c5e2 Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 14:41:34 -0300 Subject: [PATCH 2/8] better subtitle for SQL panel. --- debug_toolbar/panels/sql.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 47e0119e5..5d1f0810c 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -149,8 +149,6 @@ def nav_title(self): def nav_subtitle(self): self._queries = connection.queries[self._offset:] - self._duplicate_sql_time = 0 - self.seen = {} self.duplicate = 0 for q in self._queries: sql = q["sql"] @@ -164,7 +162,7 @@ def nav_subtitle(self): self._sql_time = sum([q['duration'] for q in self._queries]) num_queries = len(self._queries) # TODO l10n: use ngettext - return "%d %s in %.2fms" % ( + return "%d %s in %.2fms (%d duplicate in %.2fms)" % ( num_queries, (num_queries == 1) and 'query' or 'queries', self._sql_time, From 96eaac68b383b3a22f86c7b1679e3bab276c9c13 Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 14:47:01 -0300 Subject: [PATCH 3/8] missing some code from patch. --- debug_toolbar/panels/sql.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 5d1f0810c..4b704d23a 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -148,7 +148,9 @@ def nav_title(self): def nav_subtitle(self): self._queries = connection.queries[self._offset:] - + + self._duplicate_sql_time = 0 + self.seen = {} self.duplicate = 0 for q in self._queries: sql = q["sql"] From 6f69aa2dae0db719b171e519244fe7feda2349c9 Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 15:17:57 -0300 Subject: [PATCH 4/8] adding setting to enable/disable sql counting. --- README.rst | 3 ++ debug_toolbar/panels/sql.py | 47 +++++++++++++------ .../templates/debug_toolbar/panels/sql.html | 4 +- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.rst b/README.rst index d90180e7b..4deaaab36 100644 --- a/README.rst +++ b/README.rst @@ -122,6 +122,9 @@ The debug toolbar has two settings that can be set in `settings.py`: * `TAG`: If set, this will be the tag to which debug_toolbar will attach the debug toolbar. Defaults to 'body'. + * `SQL_COUNT_DUPLICATES`: If set to True (the default) then the sql panel will + count the duplicate queries. + Example configuration:: def custom_show_toolbar(request): diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 4b704d23a..de7af8996 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -28,6 +28,9 @@ SQL_WARNING_THRESHOLD = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) \ .get('SQL_WARNING_THRESHOLD', 500) +SQL_COUNT_DUPLICATES = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}). \ + .get('SQL_COUNT_DUPLICATES', True) + def tidy_stacktrace(strace): """ Clean up stacktrace and remove all entries that: @@ -149,28 +152,43 @@ def nav_title(self): def nav_subtitle(self): self._queries = connection.queries[self._offset:] - self._duplicate_sql_time = 0 - self.seen = {} - self.duplicate = 0 - for q in self._queries: - sql = q["sql"] - c = self.seen.get(sql, 0) - if c: - self.duplicate += 1 - self._duplicate_sql_time += q['duration'] - q["seen"] = c - self.seen[sql] = c + 1 + if SQL_COUNT_DUPLICATES: + self._duplicate_sql_time = 0 + self.seen = {} + self.duplicate = 0 + for q in self._queries: + sql = q["sql"] + c = self.seen.get(sql, (0,[])) + if c[0]: + self.duplicate += 1 + self._duplicate_sql_time += q['duration'] + else: + q["seen"] = 0 + c[0] += 1 + c[1].append(q) + self.seen[sql] = c + # After calculating the queries count + # apply the same counter to all equal + # queries + for c in self.seen.values(): + for q in c[1]: + q["seen"] = c[0] self._sql_time = sum([q['duration'] for q in self._queries]) num_queries = len(self._queries) # TODO l10n: use ngettext - return "%d %s in %.2fms (%d duplicate in %.2fms)" % ( + subtitle = "%d %s in %.2fms" % ( num_queries, (num_queries == 1) and 'query' or 'queries', self._sql_time, - self.duplicate, - self._duplicate_sql_time, ) + if SQL_COUNT_DUPLICATES: + subtitle = "%s (%d duplicate in %.2fms)" % ( + subtitle, + self.duplicate, + self.duplicate_sql_time, + ) + return subtitle def title(self): return _('SQL Queries') @@ -194,6 +212,7 @@ def content(self): 'queries': self._queries, 'sql_time': self._sql_time, 'is_mysql': settings.DATABASE_ENGINE == 'mysql', + 'count_duplicates': SQL_COUNT_DUPLICATES, }) return render_to_string('debug_toolbar/panels/sql.html', context) diff --git a/debug_toolbar/templates/debug_toolbar/panels/sql.html b/debug_toolbar/templates/debug_toolbar/panels/sql.html index 76ea4e19a..54f466ba1 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/sql.html +++ b/debug_toolbar/templates/debug_toolbar/panels/sql.html @@ -2,7 +2,7 @@ - + {% if count_duplicates %}{% endif %} @@ -12,7 +12,7 @@ {% for query in queries %} - + {% if count_duplicates %}{% endif %}
{% trans "Seen times" %}{% trans "Seen times" %}{% trans "Time" %} (ms) {% trans "Action" %} {% trans 'Stacktrace' %}
{{ query.seen }}{{ query.seen }}{{ query.duration|floatformat:"2" }} {% if query.params %} From 3105e866fd176a2e5f2b8fd409ee1d8a9b152c2e Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 15:20:15 -0300 Subject: [PATCH 5/8] little syntax error. --- debug_toolbar/panels/sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index de7af8996..11b103bf1 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -28,7 +28,7 @@ SQL_WARNING_THRESHOLD = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) \ .get('SQL_WARNING_THRESHOLD', 500) -SQL_COUNT_DUPLICATES = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}). \ +SQL_COUNT_DUPLICATES = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) \ .get('SQL_COUNT_DUPLICATES', True) def tidy_stacktrace(strace): From 619fc88025e03cfd7955dbf6617f86971f85515a Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 15:25:04 -0300 Subject: [PATCH 6/8] fixing an error. --- debug_toolbar/panels/sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 11b103bf1..e513b9d6c 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -158,7 +158,7 @@ def nav_subtitle(self): self.duplicate = 0 for q in self._queries: sql = q["sql"] - c = self.seen.get(sql, (0,[])) + c = self.seen.get(sql, [0,[]]) if c[0]: self.duplicate += 1 self._duplicate_sql_time += q['duration'] From a49c1f2a78c468dd90ad14a7011878e06a17cbfb Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Sun, 30 May 2010 15:29:55 -0300 Subject: [PATCH 7/8] fixing a typo. --- debug_toolbar/panels/sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index e513b9d6c..8835e5d85 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -186,7 +186,7 @@ def nav_subtitle(self): subtitle = "%s (%d duplicate in %.2fms)" % ( subtitle, self.duplicate, - self.duplicate_sql_time, + self._duplicate_sql_time, ) return subtitle From 70fb5dc29d41a47dbd0433a9879aeecfdffe2260 Mon Sep 17 00:00:00 2001 From: Vinicius Mendes Date: Mon, 31 May 2010 09:39:03 -0300 Subject: [PATCH 8/8] the duplicate count should appear only if there's duplicate queries. --- debug_toolbar/panels/sql.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 8835e5d85..377c1c20b 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -153,15 +153,15 @@ def nav_subtitle(self): self._queries = connection.queries[self._offset:] if SQL_COUNT_DUPLICATES: - self._duplicate_sql_time = 0 + duplicate_sql_time = 0 self.seen = {} - self.duplicate = 0 + duplicate = 0 for q in self._queries: sql = q["sql"] c = self.seen.get(sql, [0,[]]) if c[0]: - self.duplicate += 1 - self._duplicate_sql_time += q['duration'] + duplicate += 1 + duplicate_sql_time += q['duration'] else: q["seen"] = 0 c[0] += 1 @@ -182,11 +182,11 @@ def nav_subtitle(self): (num_queries == 1) and 'query' or 'queries', self._sql_time, ) - if SQL_COUNT_DUPLICATES: + if SQL_COUNT_DUPLICATES and duplicate: subtitle = "%s (%d duplicate in %.2fms)" % ( subtitle, - self.duplicate, - self._duplicate_sql_time, + duplicate, + duplicate_sql_time, ) return subtitle