<s> """<STR_LIT>""" <EOL> import logging <EOL> import threading <EOL> from django . core . handlers . wsgi import WSGIRequest <EOL> from perftools . middleware import Base <EOL> from perftools . patcher import Patcher <EOL> class State ( threading . local ) : <EOL> def __init__ ( self ) : <EOL> self . count = <NUM_LIT:0> <EOL> self . queries = [ ] <EOL> class CursorWrapper ( object ) : <EOL> def __init__ ( self , cursor , connection , state , queries = False ) : <EOL> self . cursor = cursor <EOL> self . connection = connection <EOL> self . _state = state <EOL> self . _queries = queries <EOL> def _incr ( self , sql , params ) : <EOL> if self . _queries : <EOL> self . _state . queries . append ( ( sql , params ) ) <EOL> self . _state . count += <NUM_LIT:1> <EOL> def execute ( self , sql , params = ( ) ) : <EOL> try : <EOL> return self . cursor . execute ( sql , params ) <EOL> finally : <EOL> self . _incr ( sql , params ) <EOL> def executemany ( self , sql , paramlist ) : <EOL> try : <EOL> return self . cursor . executemany ( sql , paramlist ) <EOL> finally : <EOL> self . _incr ( sql , paramlist ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in self . __dict__ : <EOL> return self . __dict__ [ attr ] <EOL> else : <EOL> return getattr ( self . cursor , attr ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . cursor ) <EOL> def get_cursor_wrapper ( state , queries = False ) : <EOL> def cursor ( func , self , * args , ** kwargs ) : <EOL> result = func ( self , * args , ** kwargs ) <EOL> return CursorWrapper ( result , self , state , queries = queries ) <EOL> return cursor <EOL> class QueryCountLoggingMiddleware ( Base ) : <EOL> def __init__ ( self , application , threshold = <NUM_LIT:1> , stacks = False , queries = False , logger = None , ** kwargs ) : <EOL> self . application = application <EOL> self . threshold = threshold <EOL> self . stacks = stacks <EOL> self . logger = logger or logging . getLogger ( __name__ ) <EOL> self . queries = queries <EOL> super ( QueryCountLoggingMiddleware , self ) . __init__ ( application , ** kwargs ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> if not self . should_run ( environ ) : <EOL> return self . application ( environ , start_response ) <EOL> state = State ( ) <EOL> cursor = get_cursor_wrapper ( state , queries = self . queries ) <EOL> with Patcher ( '<STR_LIT>' , cursor ) : <EOL> try : <EOL> return list ( self . application ( environ , start_response ) ) <EOL> finally : <EOL> if state . count > self . threshold : <EOL> self . log_request ( WSGIRequest ( environ ) , state ) <EOL> def log_request ( self , request , state ) : <EOL> url = request . build_absolute_uri ( ) <EOL> self . logger . warning ( '<STR_LIT>' , url , extra = { <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : self . stacks , <EOL> '<STR_LIT:url>' : url , <EOL> '<STR_LIT:data>' : { <EOL> '<STR_LIT>' : self . threshold , <EOL> '<STR_LIT>' : state . count , <EOL> '<STR_LIT>' : [ ( k , repr ( v ) ) for k , v in state . queries ] , <EOL> } <EOL> } ) </s>
