pagestats.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » pylucid_project » middlewares » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Content Management Systems » PyLucid 
PyLucid » PyLucid_standalone » pylucid_project » middlewares » pagestats.py
 # coding: utf-8

"""
    PyLucid page statistics
    ~~~~~~~~~~~~~~~~~~~~~~~

    A small page statistic middleware.
    -replace the >TAG< with some stats. But only in HTML pages.

    Based on http://code.djangoproject.com/wiki/PageStatsMiddleware
    
    database queries
    ~~~~~~~~~~~~~~~~
    We display only the the database queries count if debug is on. Otherwise
    the database cursor doesn't count the sql statements and we always get 0 ;)
    
    Some SQL print stuff from: 
        http://www.djangosnippets.org/snippets/817/
    
    TODO:
    ~~~~~
    Put settings for debug_sql_queries() into settings.py:
        http://trac.pylucid.net/ticket/230

    Last commit info:
    ~~~~~~~~~~~~~~~~~
    $LastChangedDate$
    $Rev$
    $Author$

    :copyleft: 2007-2009 by the PyLucid team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""

import time
import inspect

from django.conf import settings
from django.db import connection
from django.template.loader import render_to_string

# http://code.google.com/p/django-tools/
from django_tools.template.filters import human_duration

from pylucid_project.middlewares.utils import replace_content,cut_filename


# Save the start time of the current running python instance
start_overall = time.time()

TAG = u"<!-- script_duration -->"

# used if settings.DEBUG is off:
FMT = u"render time: %(total_time)s - overall: %(overall_time)s"
# used if settings.DEBUG is on:
FMT_DEBUG = FMT + u" - queries: %(query_count)d"

STACK_LIMIT = 5


class SqlLoggingList(list):
    """ Append some infomation on every query in debug mode. """
    def _pformat_sql(self, query):
        sql = query["sql"]
        sql = sql.replace('`', '')
        sql = sql.replace(' FROM ', '`FROM ')
        sql = sql.replace(' WHERE ', '`WHERE ')
        sql = sql.replace(' ORDER BY ', '`ORDER BY ')
        return sql.split('`')

    def append(self, query):
        query["pformat"] = self._pformat_sql(query)

        stack_list = inspect.stack()[1:]
        for no, stack_line in enumerate(stack_list):
            filename = stack_line[1]
            if "pylucid" in filename or "pylucid_project" in filename:
                break

        stack_list = stack_list[no:no + STACK_LIMIT] # limit the displayed stack info

        stack_info = []
        for stack_line in reversed(stack_list):
            stack_info.append({
                "filename": cut_filename(stack_line[1]),
                "lineno": stack_line[2],
                "func_name": stack_line[3],
                "code": stack_line[4]
            })

        query["stack_info"] = stack_info

        list.append(self, query)





class PageStatsMiddleware(object):
    def process_request(self, request):
        """
        save start time and database connections count.
        """
        self.start_time = time.time()
        if settings.DEBUG:
            # get number of db queries before we do anything
            self.old_queries = len(connection.queries)
            if settings.SQL_DEBUG:
                connection.queries = SqlLoggingList(connection.queries)

    def process_response(self, request, response):
        """
        calculate the statistic and replace it into the html page.
        """
        # Put only the statistic into HTML pages
        if not "html" in response._headers["content-type"][1]:
            # No HTML Page -> do nothing
            return response

        try:
            start_time = self.start_time
        except AttributeError:
            # FIXME: process_request() was not called?!?
            return response

        context = {
            'total_time' : human_duration(time.time() - start_time),
            'overall_time' : human_duration(time.time() - start_overall),
        }

        if settings.DEBUG:
            # compute the db time for the queries just run, this information is
            # only available if the debug cursor used
            context["query_count"] = len(connection.queries) - self.old_queries
            stat_info = FMT_DEBUG % context
        else:
            # Used the template without queries
            stat_info = FMT % context

        # insert the page statistic
        response = replace_content(response, TAG, stat_info)

        if settings.DEBUG and settings.SQL_DEBUG:
            # Insert all SQL queries into html page
            context["queries"] = connection.queries
            sql_info = render_to_string("pylucid/sql_debug.html", context)
            response = replace_content(response, "</body>", sql_info)

        return response

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.