-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathmongodb.py
More file actions
36 lines (29 loc) · 1.18 KB
/
mongodb.py
File metadata and controls
36 lines (29 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)