Changeset 1772 for trunk/pylucid_project/PyLucid/middlewares/pagestats.py
- Timestamp:
- 09/24/08 15:42:35 (18 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/pylucid_project/PyLucid/middlewares/pagestats.py
r1634 r1772 9 9 10 10 Based on http://code.djangoproject.com/wiki/PageStatsMiddleware 11 12 database queries 13 ~~~~~~~~~~~~~~~~ 14 We display only the the database queries count if debug is on. Otherwise 15 the database cursor doesn't count the sql statements and we always get 0 ;) 16 17 TODO: 18 ~~~~~ 19 Put settings for debug_sql_queries() into settings.py: 20 http://trac.pylucid.net/ticket/230 11 21 12 22 Last commit info: … … 22 32 from time import time 23 33 34 from django.conf import settings 24 35 from django.db import connection 25 36 … … 32 43 TAG = u"<!-- script_duration -->" 33 44 34 FMT = ( 35 u'render time: %(total_time)s -' 36 ' overall: %(overall_time)s -' 37 ' queries: %(queries)d' 38 ) 45 # used if settings.DEBUG is off: 46 FMT = u"render time: %(total_time)s - overall: %(overall_time)s" 47 # used if settings.DEBUG is on: 48 FMT_DEBUG = FMT + u" - queries: %(queries)d" 49 39 50 40 51 class PageStatsMiddleware(object): … … 44 55 """ 45 56 self.start_time = time() 46 # get number of db queries before we do anything 47 self.old_queries = len(connection.queries) 57 if settings.DEBUG: 58 # get number of db queries before we do anything 59 self.old_queries = len(connection.queries) 48 60 49 61 def process_response(self, request, response): … … 56 68 return response 57 69 58 # compute the db time for the queries just run 59 # FIXME: In my shared webhosting environment the queries is always = 0 60 queries = len(connection.queries) - self.old_queries 70 context = { 71 'total_time' : human_duration(time() - self.start_time), 72 'overall_time' : human_duration(time() - start_overall), 73 } 61 74 62 total_time = human_duration(time() - self.start_time) 63 overall_time = human_duration(time() - start_overall) 64 65 # replace the comment if found 66 stat_info = FMT % { 67 'total_time' : total_time, 68 'overall_time' : overall_time, 69 'queries' : queries, 70 } 75 if settings.DEBUG: 76 # compute the db time for the queries just run, this information is 77 # only available if the debug cursor used 78 context["queries"] = len(connection.queries) - self.old_queries 79 stat_info = FMT_DEBUG % context 80 else: 81 # Used the template without queries 82 stat_info = FMT % context 71 83 72 84 # insert the page statistic