Skip to content

implement MongoDBSummaryModule #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions devserver/modules/mongodb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from devserver.modules import DevServerModule
from debug_toolbar_mongo import operation_tracker

class MongoDBSummaryModule(DevServerModule):
"""
Outputs a summary NoSQL queries.
"""

logger_name = 'mongodb'

def __init__(self, *args, **kwargs):
super(MongoDBSummaryModule, self).__init__(*args, **kwargs)
operation_tracker.install_tracker()

def process_init(self, request):
operation_tracker.reset()

def process_complete(self, request):
attrs = ('queries', 'inserts', 'updates', 'removes')
stats = dict(
(attr, {
'count': len(ops),
'time': sum(op['time'] for op in ops)
}) for attr, ops in (
(attr, getattr(operation_tracker, attr)) for attr in attrs
)
)

if any(stat['count'] for stat in stats.values()):
total_time = sum(stat['time'] for stat in stats.values())
self.logger.info(', '.join(
'%(count)d %(name)s (%(time).2fms)' % dict(
count=stats[attr]['count'], name=attr, time=stats[attr]['time'],
) for attr in attrs
), duration=total_time)