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
6449a69c
Commit
6449a69c
authored
May 25, 2011
by
Harry Marr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial import
parents
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
136 additions
and
0 deletions
+136
-0
.gitignore
+1
-0
debug_toolbar_mongo/__init__.py
+0
-0
debug_toolbar_mongo/panel.py
+113
-0
debug_toolbar_mongo/templates/mongo-panel.html
+22
-0
No files found.
.gitignore
0 → 100644
View file @
6449a69c
*.pyc
debug_toolbar_mongo/__init__.py
0 → 100644
View file @
6449a69c
debug_toolbar_mongo/panel.py
0 → 100644
View file @
6449a69c
from
django.template.loader
import
render_to_string
import
pprint
import
functools
import
time
import
pymongo.collection
import
pymongo.cursor
from
debug_toolbar.panels
import
DebugPanel
class
MongoOperationHook
(
object
):
"""Track operations sent to MongoDB via PyMongo.
"""
def
__init__
(
self
):
self
.
queries
=
[]
self
.
active
=
False
def
install
(
self
):
self
.
original_methods
=
{
'insert'
:
pymongo
.
collection
.
Collection
.
insert
,
'update'
:
pymongo
.
collection
.
Collection
.
update
,
'remove'
:
pymongo
.
collection
.
Collection
.
remove
,
'refresh'
:
pymongo
.
cursor
.
Cursor
.
_refresh
,
}
# Wrap Cursor._refresh for getting queries
@functools.wraps
(
self
.
original_methods
[
'refresh'
])
def
cursor_refresh
(
cursor_self
):
# Look up __ private instance variables
def
privar
(
name
):
return
getattr
(
cursor_self
,
'_Cursor__{0}'
.
format
(
name
))
if
not
self
.
active
or
privar
(
'id'
)
is
not
None
:
# Inactive or getMore - move on
return
self
.
original_methods
[
'refresh'
](
cursor_self
)
query_details
=
{
'options'
:
privar
(
'query_options'
)(),
'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
start_time
=
time
.
time
()
result
=
self
.
original_methods
[
'refresh'
](
cursor_self
)
total_time
=
(
time
.
time
()
-
start_time
)
*
1000
self
.
queries
.
append
({
'type'
:
'query'
,
'query'
:
query_details
[
'query'
]
.
to_dict
(),
'collection'
:
query_details
[
'collection_name'
],
'time'
:
total_time
,
})
return
result
pymongo
.
cursor
.
Cursor
.
_refresh
=
cursor_refresh
def
uninstall
(
self
):
pymongo
.
cursor
.
Cursor
.
_refresh
=
self
.
original_methods
[
'refresh'
]
def
reset
(
self
):
self
.
queries
=
[]
def
start
(
self
):
self
.
active
=
True
def
stop
(
self
):
self
.
active
=
False
class
MongoDebugPanel
(
DebugPanel
):
"""Panel that shows information about MongoDB queries.
"""
name
=
'MongoDB'
has_content
=
True
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
self
.
__class__
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
self
.
op_hook
=
MongoOperationHook
()
self
.
op_hook
.
install
()
def
process_request
(
self
,
request
):
self
.
op_hook
.
reset
()
self
.
op_hook
.
start
()
def
process_response
(
self
,
request
,
response
):
self
.
op_hook
.
stop
()
def
nav_title
(
self
):
return
'MongoDB'
def
nav_subtitle
(
self
):
num_queries
=
len
(
self
.
op_hook
.
queries
)
return
'{0} queries executed'
.
format
(
num_queries
)
def
title
(
self
):
return
'MongoDB Queries'
def
url
(
self
):
return
''
def
content
(
self
):
context
=
self
.
context
.
copy
()
context
[
'queries'
]
=
self
.
op_hook
.
queries
return
render_to_string
(
'mongo-panel.html'
,
context
)
debug_toolbar_mongo/templates/mongo-panel.html
0 → 100644
View file @
6449a69c
<table>
<thead>
<tr>
<th>
Time (ms)
</th>
<th>
Type
</th>
<th>
Collection
</th>
<th>
Query
</th>
</tr>
</thead>
<tbody>
{% for query in queries %}
<tr
class=
"{% cycle 'djDebugOdd' 'djDebugEven' %}"
>
<td>
{{ query.time|floatformat:"4" }}
</td>
<td>
{{ query.type|title }}
</td>
<td>
{{ query.collection }}
</td>
<td><code>
{{ query.query|pprint }}
</code></td>
</tr>
{% endfor %}
</tbody>
</table>
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