Commit c92cff03 by Harry Marr

Pull out query, skip, limit. Detect cmds (e.g.count)

parent 2abd5c99
...@@ -9,7 +9,7 @@ import pymongo.cursor ...@@ -9,7 +9,7 @@ import pymongo.cursor
from debug_toolbar.panels import DebugPanel from debug_toolbar.panels import DebugPanel
class MongoOperationHook(object): class MongoOperationTracker(object):
"""Track operations sent to MongoDB via PyMongo. """Track operations sent to MongoDB via PyMongo.
""" """
...@@ -36,26 +36,43 @@ class MongoOperationHook(object): ...@@ -36,26 +36,43 @@ class MongoOperationHook(object):
# Inactive or getMore - move on # Inactive or getMore - move on
return self.original_methods['refresh'](cursor_self) return self.original_methods['refresh'](cursor_self)
query_details = { # NOTE: See pymongo/cursor.py+557 [_refresh()] and
'options': privar('query_options')(), # pymongo/message.py for where information is stored
'collection_name': privar('collection').full_name,
'num_to_skip': privar('skip'),
'num_to_return': privar('limit'),
'query': privar('query_spec')(),
'field_selector': privar('fields'),
}
# Time the actual query # Time the actual query
start_time = time.time() start_time = time.time()
result = self.original_methods['refresh'](cursor_self) result = self.original_methods['refresh'](cursor_self)
total_time = (time.time() - start_time) * 1000 total_time = (time.time() - start_time) * 1000
self.queries.append({ query_son = privar('query_spec')().to_dict()
'type': 'query',
'query': query_details['query'].to_dict(), query_data = {
'collection': query_details['collection_name'],
'time': total_time, 'time': total_time,
}) 'operation': 'query',
}
# Collection in format <db_name>.<collection_name>
collection_name = privar('collection')
query_data['collection'] = collection_name.full_name.split('.')[1]
if query_data['collection'] == '$cmd':
query_data['operation'] = 'command'
# Handle count as a special case
if 'count' in query_son:
# Information is in a different format to a standar query
query_data['collection'] = query_son['count']
query_data['operation'] = 'count'
query_data['skip'] = query_son.get('skip')
query_data['limit'] = query_son.get('limit')
query_data['query'] = query_son['query']
del query_son['count']
else:
# Normal Query
query_data['skip'] = privar('skip')
query_data['limit'] = privar('limit')
query_data['query'] = query_son['$query']
self.queries.append(query_data)
return result return result
...@@ -82,22 +99,22 @@ class MongoDebugPanel(DebugPanel): ...@@ -82,22 +99,22 @@ class MongoDebugPanel(DebugPanel):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs) super(self.__class__, self).__init__(*args, **kwargs)
self.op_hook = MongoOperationHook() self.op_tracker = MongoOperationTracker()
self.op_hook.install() self.op_tracker.install()
def process_request(self, request): def process_request(self, request):
self.op_hook.reset() self.op_tracker.reset()
self.op_hook.start() self.op_tracker.start()
def process_response(self, request, response): def process_response(self, request, response):
self.op_hook.stop() self.op_tracker.stop()
def nav_title(self): def nav_title(self):
return 'MongoDB' return 'MongoDB'
def nav_subtitle(self): def nav_subtitle(self):
num_queries = len(self.op_hook.queries) num_queries = len(self.op_tracker.queries)
return '{0} queries executed'.format(num_queries) return '{0} operations executed'.format(num_queries)
def title(self): def title(self):
return 'MongoDB Queries' return 'MongoDB Queries'
...@@ -107,7 +124,7 @@ class MongoDebugPanel(DebugPanel): ...@@ -107,7 +124,7 @@ class MongoDebugPanel(DebugPanel):
def content(self): def content(self):
context = self.context.copy() context = self.context.copy()
context['queries'] = self.op_hook.queries context['queries'] = self.op_tracker.queries
return render_to_string('mongo-panel.html', context) return render_to_string('mongo-panel.html', context)
...@@ -3,18 +3,22 @@ ...@@ -3,18 +3,22 @@
<thead> <thead>
<tr> <tr>
<th>Time (ms)</th> <th>Time (ms)</th>
<th>Type</th> <th>Operation</th>
<th>Collection</th> <th>Collection</th>
<th>Query</th> <th>Query</th>
<th>Skip</th>
<th>Limit</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for query in queries %} {% for query in queries %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}"> <tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ query.time|floatformat:"4" }}</td> <td>{{ query.time|floatformat:"4" }}</td>
<td>{{ query.type|title }}</td> <td>{{ query.operation|title }}</td>
<td>{{ query.collection }}</td> <td>{{ query.collection }}</td>
<td><code>{{ query.query|pprint }}</code></td> <td><code>{{ query.query|pprint }}</code></td>
<td>{% if query.skip %}{{ query.skip }}{% endif %}</td>
<td>{% if query.limit %}{{ query.limit }}{% endif %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment