Changeset 1772

Show
Ignore:
Timestamp:
09/24/08 15:42:35 (18 months ago)
Author:
JensDiemer
Message:

bugfix for "pagestats - queries number" -  http://trac.pylucid.net/ticket/229

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid_project/PyLucid/middlewares/pagestats.py

    r1634 r1772  
    99 
    1010    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 
    1121 
    1222    Last commit info: 
     
    2232from time import time 
    2333 
     34from django.conf import settings 
    2435from django.db import connection 
    2536 
     
    3243TAG = u"<!-- script_duration -->" 
    3344 
    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: 
     46FMT = u"render time: %(total_time)s - overall: %(overall_time)s" 
     47# used if settings.DEBUG is on: 
     48FMT_DEBUG = FMT + u" - queries: %(queries)d" 
     49 
    3950 
    4051class PageStatsMiddleware(object): 
     
    4455        """ 
    4556        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) 
    4860 
    4961    def process_response(self, request, response): 
     
    5668            return response 
    5769 
    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        } 
    6174 
    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 
    7183 
    7284        # insert the page statistic