Commit b2efe44f by Harry Marr

Added insert, update and remove support

parent 8d4c1265
......@@ -11,6 +11,9 @@ class MongoOperationTracker(object):
def __init__(self):
self.queries = []
self.inserts = []
self.updates = []
self.removes = []
self.active = False
def install(self):
......@@ -22,6 +25,79 @@ class MongoOperationTracker(object):
}
# Wrap Cursor._refresh for getting queries
@functools.wraps(self.original_methods['insert'])
def insert(collection_self, doc_or_docs, safe=False, **kwargs):
start_time = time.time()
result = self.original_methods['insert'](
collection_self,
doc_or_docs,
safe=safe,
**kwargs
)
total_time = (time.time() - start_time) * 1000
if self.active:
self.inserts.append({
'document': doc_or_docs,
'safe': safe,
'time': total_time,
})
return result
pymongo.collection.Collection.insert = insert
# Wrap Cursor._refresh for getting queries
@functools.wraps(self.original_methods['update'])
def update(collection_self, spec, document, upsert=False, safe=False,
multi=False, **kwargs):
start_time = time.time()
result = self.original_methods['update'](
collection_self,
spec,
document,
upsert=False,
safe=False,
multi=False,
**kwargs
)
total_time = (time.time() - start_time) * 1000
if self.active:
self.updates.append({
'document': document,
'upsert': upsert,
'multi': multi,
'spec': spec,
'safe': safe,
'time': total_time,
})
return result
pymongo.collection.Collection.update = update
# Wrap Cursor._refresh for getting queries
@functools.wraps(self.original_methods['remove'])
def remove(collection_self, spec_or_id, safe=False, **kwargs):
start_time = time.time()
result = self.original_methods['insert'](
collection_self,
spec_or_id,
safe=safe,
**kwargs
)
total_time = (time.time() - start_time) * 1000
if self.active:
self.removes.append({
'spec_or_id': spec_or_id,
'safe': safe,
'time': total_time,
})
return result
pymongo.collection.Collection.remove = remove
# Wrap Cursor._refresh for getting queries
@functools.wraps(self.original_methods['refresh'])
def cursor_refresh(cursor_self):
# Look up __ private instance variables
......@@ -83,6 +159,8 @@ class MongoOperationTracker(object):
def uninstall(self):
pymongo.cursor.Cursor._refresh = self.original_methods['refresh']
pymongo.cursor.Collection.insert = self.original_methods['insert']
pymongo.cursor.Collection.update = self.original_methods['update']
def reset(self):
self.queries = []
......
......@@ -28,7 +28,9 @@ class MongoDebugPanel(DebugPanel):
def nav_subtitle(self):
num_queries = len(self.op_tracker.queries)
total_time = sum(q['time'] for q in self.op_tracker.queries)
attrs = ['queries', 'inserts', 'updates', 'removes']
total_time = sum(sum(o['time'] for o in getattr(self.op_tracker, a))
for a in attrs)
return '{0} operations in {1:.2f}ms'.format(num_queries, total_time)
def title(self):
......@@ -40,6 +42,9 @@ class MongoDebugPanel(DebugPanel):
def content(self):
context = self.context.copy()
context['queries'] = self.op_tracker.queries
context['inserts'] = self.op_tracker.inserts
context['updates'] = self.op_tracker.updates
context['removes'] = self.op_tracker.removes
return render_to_string('mongo-panel.html', context)
......@@ -7,6 +7,8 @@ pre.mongo-highlight, pre.mongo-highlight span {
}
</style>
<h4>Queries</h4>
{% if queries %}
<table>
<thead>
<tr>
......@@ -37,4 +39,93 @@ pre.mongo-highlight, pre.mongo-highlight span {
{% endfor %}
</tbody>
</table>
{% else %}
<p>No queries recorded</p>
{% endif %}
<h4>Inserts</h4>
{% if inserts %}
<table>
<thead>
<tr>
<th>Time (ms)</th>
<th>Document</th>
<th>Safe</th>
</tr>
</thead>
<tbody>
{% for insert in inserts %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ insert.time|floatformat:"4" }}</td>
<td>
<pre class="mongo-highlight">{{ insert.document|format_dict:120|highlight_json|safe }}</pre>
</td>
<td>{{ insert.safe }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No inserts recorded</p>
{% endif %}
<h4>Removes</h4>
{% if removes %}
<table>
<thead>
<tr>
<th>Time (ms)</th>
<th>Query / Id</th>
<th>Safe</th>
</tr>
</thead>
<tbody>
{% for remove in removes %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ remove.time|floatformat:"4" }}</td>
<td>
<pre class="mongo-highlight">{{ remove.spec_or_id|format_dict:120|highlight_json|safe }}</pre>
</td>
<td>{{ remove.safe }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No removes recorded</p>
{% endif %}
<h4>Updates</h4>
{% if updates %}
<table>
<thead>
<tr>
<th>Time (ms)</th>
<th>Query</th>
<th>Update</th>
<th>Safe</th>
<th>Multi</th>
<th>Upsert</th>
</tr>
</thead>
<tbody>
{% for update in updates %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ update.time|floatformat:"4" }}</td>
<td>
<pre class="mongo-highlight">{{ update.spec|format_dict:120|highlight_json|safe }}</pre>
</td>
<td>
<pre class="mongo-highlight">{{ update.document|format_dict:120|highlight_json|safe }}</pre>
</td>
<td>{{ update.safe }}</td>
<td>{{ update.multi }}</td>
<td>{{ update.upsert }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No updates recorded</p>
{% endif %}
......@@ -6,7 +6,6 @@ register = template.Library()
@register.filter
def format_dict(value, width=60):
print width
return pprint.pformat(value, width=int(width))
@register.filter
......
......@@ -27,5 +27,9 @@ def index(request):
}
]
}))
db.test.insert({'name': 'test'})
db.test.insert({'name': 'test2'}, safe=True)
db.test.update({'name': 'test2'}, {'age': 1}, upsert=True)
db.test.remove({'name': 'test1'})
return render_to_response('index.html')
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