Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-debug-toolbar-mongo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
django-debug-toolbar-mongo
Commits
b2efe44f
Commit
b2efe44f
authored
May 26, 2011
by
Harry Marr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added insert, update and remove support
parent
8d4c1265
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
179 additions
and
2 deletions
+179
-2
debug_toolbar_mongo/operation_tracker.py
+78
-0
debug_toolbar_mongo/panel.py
+6
-1
debug_toolbar_mongo/templates/mongo-panel.html
+91
-0
debug_toolbar_mongo/templatetags/mongo_debug_tags.py
+0
-1
example/views.py
+4
-0
No files found.
debug_toolbar_mongo/operation_tracker.py
View file @
b2efe44f
...
...
@@ -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
=
[]
...
...
debug_toolbar_mongo/panel.py
View file @
b2efe44f
...
...
@@ -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
)
debug_toolbar_mongo/templates/mongo-panel.html
View file @
b2efe44f
...
...
@@ -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 %}
debug_toolbar_mongo/templatetags/mongo_debug_tags.py
View file @
b2efe44f
...
...
@@ -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
...
...
example/views.py
View file @
b2efe44f
...
...
@@ -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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment