Source

pyramid_perfstat / pyramid_perfstat / __init__.py

tuck 548a9e4 







tuck 2ba8f55 
tuck 548a9e4 











tuck 2a792f3 
tuck 548a9e4 







tuck d27ec99 
tuck 2ba8f55 
tuck 2a792f3 
tuck 548a9e4 
tuck d27ec99 












tuck 548a9e4 


tuck 50e4483 

tuck 548a9e4 




tuck 87bb27e 
tuck 50e4483 
tuck 87bb27e 
tuck 7e735cb 
tuck 548a9e4 
tuck 87bb27e 
tuck 548a9e4 






tuck 3de4f74 
tuck d27ec99 
tuck 3de4f74 




tuck d27ec99 
tuck 3de4f74 

tuck 548a9e4 
tuck 3de4f74 
tuck 548a9e4 









tuck d27ec99 
tuck 2a792f3 
tuck 38cb8df 
tuck 548a9e4 
tuck d27ec99 
tuck 548a9e4 
tuck e8bb9cb 

#!/usr/bin/python
# -*- coding: utf-8 -*- 
#-----------------------------------------------------------------------------
# Name:       __init__.py
# Purpose:     
#
# Author:      Stéphane Bard
#
# Created:      31/01/2012
# Copyright:   -
# Licence:     BSD License
# New field:   ----------
#-----------------------------------------------------------------------------


import logging
import sys
from pprint import pformat
from textwrap import dedent

from pyramid.tweens import EXCVIEW
from pyramid.events import NewRequest
from pyramid.settings import aslist
from pyramid.settings import asbool
from pyramid.util import DottedNameResolver
from pyramid.httpexceptions import WSGIHTTPException

import time
import datetime

from pyramid_perfstat.utils import STATIC_PATH, ROUTE_PREFIX
from pyramid_perfstat.perf_serializer import PerfDbManager
from pyramid_perfstat.perf_serializer import new_perfstat_request_subscriber


def to_int(*segment_names):
    def predicate(info, request):
        match = info['match']
        for segment_name in segment_names:
            try:
                match[segment_name] = int(match[segment_name])
            except (TypeError, ValueError):
                pass
        return True
    return predicate


def perf_tween_factory(handler, registry):
    """
    """
    PerfDbManager(None).init_db()
    
    def perf_tween(request, getLogger=logging.getLogger):
        """
        """
        t0=time.time()
        result = handler(request)

        perf_manager = PerfDbManager(request)
        perf_manager.insert_data(time.time()-t0)
        
        return result
    
    return perf_tween

def include_perf_routes(config):
    """
    add some routes
    """
    config.add_route('pyramid_perfstat.reporting', '/stat')
    config.add_route('pyramid_perfstat.reset','/reset')

    config.add_route('pyramid_perfstat.reporting.session_detail', '/stat/{id_session}',
                     custom_predicates=(to_int('id_session'),))
    
    config.add_route('pyramid_perfstat.reporting.view_detail', '/stat/{id_session}/{id_view}',
                     custom_predicates=(to_int('id_session', 'id_view'),))

    config.add_route('pyramid_perfstat.reporting.url_detail', '/stat/{id_session}/{id_view}/{id_route}',
                     custom_predicates=(to_int('id_session', 'id_view', 'id_route'),))
    
    
def includeme(config):
    """
    Set up am implicit :term:`tween` to log performance information of each
    Pyramid url application. 

    This tween configured to be placed 'top' view tween.  It
    will log all excution time and log it in sqlite db. It should
    provite a url to allow log performance display.
    such as mean time, number of request urls etc ...
    """
    config.add_static_view(ROUTE_PREFIX+'/static', STATIC_PATH)
    config.add_subscriber(new_perfstat_request_subscriber,NewRequest)
    
    config.add_tween('pyramid_perfstat.perf_tween_factory', under=EXCVIEW)
    config.include(include_perf_routes, route_prefix=ROUTE_PREFIX)

    # scan views
    config.scan('pyramid_perfstat.views')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.