Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added max_queries option for SQLAlchemy

  • Loading branch information...
commit bcb653aea65682cc0f4b5b24c326ad57ae79cee9 1 parent 131169d
Charlie Clark authored
View
1  demo/demo.py
@@ -112,6 +112,7 @@ def __getitem__(self, name):
settings['mako.module_directory'] = '__main__:mako_modules'
settings['debugtoolbar.hosts'] = ['127.0.0.1']
settings['debugtoolbar.intercept_redirects'] = True
+ settings['debugtoolbar.sqla_max_queries'] = '2'
# session factory
session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')
# configuration setup
View
25 pyramid_debugtoolbar/panels/sqla.py
@@ -4,6 +4,7 @@
import threading
import time
import weakref
+from collections import deque
from pyramid.threadlocal import get_current_request
@@ -15,6 +16,7 @@
from pyramid_debugtoolbar.utils import STATIC_PATH
from pyramid_debugtoolbar.utils import ROOT_ROUTE_NAME
+
lock = threading.Lock()
try:
@@ -27,14 +29,19 @@ def _before_cursor_execute(conn, cursor, stmt, params, context, execmany):
@event.listens_for(Engine, "after_cursor_execute")
def _after_cursor_execute(conn, cursor, stmt, params, context, execmany):
+ global qmax
stop_timer = time.time()
request = get_current_request()
+ qmax = request.registry.settings.get(
+ 'debugtoolbar.sqla_max_queries')
+ qmax = int(qmax) if qmax else None
+
if request is not None:
with lock:
engines = getattr(request.registry, 'pdtb_sqla_engines', {})
engines[id(conn.engine)] = weakref.ref(conn.engine)
setattr(request.registry, 'pdtb_sqla_engines', engines)
- queries = getattr(request, 'pdtb_sqla_queries', [])
+ queries = getattr(request, 'pdtb_sqla_queries', deque(maxlen=qmax))
queries.append({
'engine_id': id(conn.engine),
'duration': stop_timer - conn.pdtb_start_timer,
@@ -64,13 +71,21 @@ class SQLADebugPanel(DebugPanel):
def queries(self):
return getattr(self.request, 'pdtb_sqla_queries', [])
+ @property
+ def max_queries(self):
+ return self.request.registry.settings.get(
+ 'debugtoolbar.sqla_max_queries')
+
def nav_title(self):
return _('SQLAlchemy')
def nav_subtitle(self):
- if self.queries:
- count = len(self.queries)
- return "%d %s" % (count, "query" if count == 1 else "queries")
+ count = len(self.queries)
+ if count:
+ plural_or_not = "query" if count == 1 else "queries"
+ max_or_not = "(%s max)" % self.max_queries \
+ if self.max_queries else ""
+ return "%d %s %s" % (count, plural_or_not, max_or_not)
def title(self):
return _('SQLAlchemy queries')
@@ -118,4 +133,4 @@ def content(self):
return self.render(
'pyramid_debugtoolbar.panels:templates/sqlalchemy.dbtmako',
- vars, self.request)
+ vars, self.request)
View
1  pyramid_debugtoolbar/panels/tests/__init__.py
@@ -0,0 +1 @@
+# nothing to see
View
29 pyramid_debugtoolbar/panels/tests/test_sqla.py
@@ -0,0 +1,29 @@
+import unittest
+
+from pyramid import testing
+
+class TestSQLAPanel(unittest.TestCase):
+ def setUp(self):
+ self.config = testing.setUp()
+
+ def tearDown(self):
+ testing.tearDown()
+
+ def _makeOne(self):
+ from pyramid_debugtoolbar.panels.sqla import SQLADebugPanel
+ request = testing.DummyRequest()
+ return SQLADebugPanel(request)
+
+ def test_max_queries(self):
+ panel = self._makeOne()
+ self.assertEqual(panel.max_queries, None)
+ self.config.registry.settings['debugtoolbar.sqla_max_queries'] = '10'
+ self.assertEqual(panel.max_queries, '10')
+
+ def test_nav_subtitle(self):
+ panel = self._makeOne()
+ self.assertEqual(panel.nav_subtitle(), None)
+ panel.request.pdtb_sqla_queries = [1]
+ self.assertEqual(panel.nav_subtitle(), '1 query ')
+ self.config.registry.settings['debugtoolbar.sqla_max_queries'] = '10'
+ self.assertEqual(panel.nav_subtitle(), '1 query (10 max)')
Please sign in to comment.
Something went wrong with that request. Please try again.