From 2eeebd4be280373569ce19cf4a187765d2b794f9 Mon Sep 17 00:00:00 2001 From: Michael Cetrulo Date: Thu, 27 Dec 2012 17:43:34 -0800 Subject: [PATCH] implement MongoDBSummaryModule a module similar to SQLSummaryModule but to output stats for queries to MongoDB. depends on django-debug-toolbar-mongo --- devserver/modules/mongodb.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 devserver/modules/mongodb.py diff --git a/devserver/modules/mongodb.py b/devserver/modules/mongodb.py new file mode 100644 index 0000000..7b72fad --- /dev/null +++ b/devserver/modules/mongodb.py @@ -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) +