<s> """<STR_LIT>""" <EOL> import cgi <EOL> import email . Utils <EOL> import logging <EOL> import mimetypes <EOL> import os <EOL> import re <EOL> import sys <EOL> import time <EOL> from google . appengine . api import users <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . ext . webapp import _template <EOL> from google . appengine . ext . webapp import util <EOL> from google . appengine . ext . appstats import recording <EOL> DEBUG = recording . config . DEBUG <EOL> def render ( tmplname , data ) : <EOL> """<STR_LIT>""" <EOL> here = os . path . dirname ( __file__ ) <EOL> tmpl = os . path . join ( here , '<STR_LIT>' , tmplname ) <EOL> data [ '<STR_LIT>' ] = os . environ <EOL> try : <EOL> return _template . render ( tmpl , data ) <EOL> except Exception , err : <EOL> logging . exception ( '<STR_LIT>' , tmpl ) <EOL> return '<STR_LIT>' % ( tmplname , err ) <EOL> class SummaryHandler ( webapp . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> recording . dont_record ( ) <EOL> if not self . request . path . endswith ( '<STR_LIT:/>' ) : <EOL> self . redirect ( self . request . path + '<STR_LIT:/>' ) <EOL> return <EOL> summaries = recording . load_summary_protos ( ) <EOL> allstats = { } <EOL> pathstats = { } <EOL> pivot_path_rpc = { } <EOL> pivot_rpc_path = { } <EOL> for index , summary in enumerate ( summaries ) : <EOL> path_key = recording . config . extract_key ( summary ) <EOL> if path_key not in pathstats : <EOL> pathstats [ path_key ] = [ <NUM_LIT:1> , index + <NUM_LIT:1> ] <EOL> else : <EOL> values = pathstats [ path_key ] <EOL> values [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> if len ( values ) >= <NUM_LIT:11> : <EOL> if values [ - <NUM_LIT:1> ] : <EOL> values . append ( <NUM_LIT:0> ) <EOL> else : <EOL> values . append ( index + <NUM_LIT:1> ) <EOL> if path_key not in pivot_path_rpc : <EOL> pivot_path_rpc [ path_key ] = { } <EOL> for x in summary . rpc_stats_list ( ) : <EOL> rpc_key = x . service_call_name ( ) <EOL> value = x . total_amount_of_calls ( ) <EOL> if rpc_key in allstats : <EOL> allstats [ rpc_key ] += value <EOL> else : <EOL> allstats [ rpc_key ] = value <EOL> if rpc_key not in pivot_path_rpc [ path_key ] : <EOL> pivot_path_rpc [ path_key ] [ rpc_key ] = <NUM_LIT:0> <EOL> pivot_path_rpc [ path_key ] [ rpc_key ] += value <EOL> if rpc_key not in pivot_rpc_path : <EOL> pivot_rpc_path [ rpc_key ] = { } <EOL> if path_key not in pivot_rpc_path [ rpc_key ] : <EOL> pivot_rpc_path [ rpc_key ] [ path_key ] = <NUM_LIT:0> <EOL> pivot_rpc_path [ rpc_key ] [ path_key ] += value <EOL> allstats_by_count = [ ] <EOL> for k , v in allstats . iteritems ( ) : <EOL> pivot = sorted ( pivot_rpc_path [ k ] . iteritems ( ) , <EOL> key = lambda x : ( - x [ <NUM_LIT:1> ] , x [ <NUM_LIT:0> ] ) ) <EOL> allstats_by_count . append ( ( k , v , pivot ) ) <EOL> allstats_by_count . sort ( key = lambda x : ( - x [ <NUM_LIT:1> ] , x [ <NUM_LIT:0> ] ) ) <EOL> pathstats_by_count = [ ] <EOL> for path_key , values in pathstats . iteritems ( ) : <EOL> pivot = sorted ( pivot_path_rpc [ path_key ] . iteritems ( ) , <EOL> key = lambda x : ( - x [ <NUM_LIT:1> ] , x [ <NUM_LIT:0> ] ) ) <EOL> rpc_count = sum ( x [ <NUM_LIT:1> ] for x in pivot ) <EOL> pathstats_by_count . append ( ( path_key , rpc_count , <EOL> values [ <NUM_LIT:0> ] , values [ <NUM_LIT:1> : ] , pivot ) ) <EOL> pathstats_by_count . sort ( key = lambda x : ( - x [ <NUM_LIT:1> ] , - x [ <NUM_LIT:2> ] , x [ <NUM_LIT:0> ] ) ) <EOL> data = { '<STR_LIT>' : summaries , <EOL> '<STR_LIT>' : allstats_by_count , <EOL> '<STR_LIT>' : pathstats_by_count , <EOL> } <EOL> self . response . out . write ( render ( '<STR_LIT>' , data ) ) <EOL> class DetailsHandler ( webapp . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> recording . dont_record ( ) <EOL> time_key = self . request . get ( '<STR_LIT:time>' ) <EOL> timestamp = None <EOL> record = None <EOL> if time_key : <EOL> try : <EOL> timestamp = int ( time_key ) * <NUM_LIT> <EOL> except Exception : <EOL> pass <EOL> if timestamp : <EOL> record = recording . load_full_proto ( timestamp ) <EOL> if record is None : <EOL> self . response . set_status ( <NUM_LIT> ) <EOL> self . response . out . write ( render ( '<STR_LIT>' , { } ) ) <EOL> return <EOL> rpcstats_map = { } <EOL> for rpc_stat in record . individual_stats_list ( ) : <EOL> key = rpc_stat . service_call_name ( ) <EOL> count , real , api = rpcstats_map . get ( key , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> count += <NUM_LIT:1> <EOL> real += rpc_stat . duration_milliseconds ( ) <EOL> api += rpc_stat . api_mcycles ( ) <EOL> rpcstats_map [ key ] = ( count , real , api ) <EOL> rpcstats_by_count = [ <EOL> ( name , count , real , recording . mcycles_to_msecs ( api ) ) <EOL> for name , ( count , real , api ) in rpcstats_map . iteritems ( ) ] <EOL> rpcstats_by_count . sort ( key = lambda x : - x [ <NUM_LIT:1> ] ) <EOL> real_total = <NUM_LIT:0> <EOL> api_total_mcycles = <NUM_LIT:0> <EOL> for i , rpc_stat in enumerate ( record . individual_stats_list ( ) ) : <EOL> real_total += rpc_stat . duration_milliseconds ( ) <EOL> api_total_mcycles += rpc_stat . api_mcycles ( ) <EOL> api_total = recording . mcycles_to_msecs ( api_total_mcycles ) <EOL> data = { '<STR_LIT>' : sys , <EOL> '<STR_LIT>' : record , <EOL> '<STR_LIT>' : rpcstats_by_count , <EOL> '<STR_LIT>' : real_total , <EOL> '<STR_LIT>' : api_total , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . response . out . write ( render ( '<STR_LIT>' , data ) ) <EOL> class FileHandler ( webapp . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> recording . dont_record ( ) <EOL> lineno = self . request . get ( '<STR_LIT:n>' ) <EOL> try : <EOL> lineno = int ( lineno ) <EOL> except : <EOL> lineno = <NUM_LIT:0> <EOL> filename = self . request . get ( '<STR_LIT:f>' ) or '<STR_LIT>' <EOL> orig_filename = filename <EOL> match = re . match ( '<STR_LIT>' , filename ) <EOL> if match : <EOL> index , tail = match . groups ( ) <EOL> index = int ( index ) <EOL> if index < len ( sys . path ) : <EOL> filename = sys . path [ index ] + tail <EOL> try : <EOL> fp = open ( filename ) <EOL> except IOError , err : <EOL> self . response . out . write ( '<STR_LIT>' % <EOL> cgi . escape ( str ( err ) ) ) <EOL> self . response . set_status ( <NUM_LIT> ) <EOL> else : <EOL> try : <EOL> data = { '<STR_LIT>' : fp , <EOL> '<STR_LIT:filename>' : filename , <EOL> '<STR_LIT>' : orig_filename , <EOL> '<STR_LIT>' : lineno , <EOL> } <EOL> self . response . out . write ( render ( '<STR_LIT>' , data ) ) <EOL> finally : <EOL> fp . close ( ) <EOL> class StaticHandler ( webapp . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> here = os . path . dirname ( __file__ ) <EOL> fn = self . request . path <EOL> i = fn . rfind ( '<STR_LIT:/>' ) <EOL> fn = fn [ i + <NUM_LIT:1> : ] <EOL> fn = os . path . join ( here , '<STR_LIT>' , fn ) <EOL> ctype , encoding = mimetypes . guess_type ( fn ) <EOL> assert ctype and '<STR_LIT:/>' in ctype , repr ( ctype ) <EOL> expiry = <NUM_LIT> <EOL> expiration = email . Utils . formatdate ( time . time ( ) + expiry , usegmt = True ) <EOL> fp = open ( fn , '<STR_LIT:rb>' ) <EOL> try : <EOL> self . response . out . write ( fp . read ( ) ) <EOL> finally : <EOL> fp . close ( ) <EOL> self . response . headers [ '<STR_LIT>' ] = ctype <EOL> self . response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . response . headers [ '<STR_LIT>' ] = expiration <EOL> URLMAP = [ <EOL> ( '<STR_LIT>' , DetailsHandler ) , <EOL> ( '<STR_LIT>' , FileHandler ) , <EOL> ( '<STR_LIT>' , StaticHandler ) , <EOL> ( '<STR_LIT>' , SummaryHandler ) , <EOL> ] <EOL> class AuthCheckMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , application ) : <EOL> self . _application = application <EOL> def __call__ ( self , environ , start_response ) : <EOL> if not environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) : <EOL> if not users . is_current_user_admin ( ) : <EOL> if users . get_current_user ( ) is None : <EOL> start_response ( '<STR_LIT>' , <EOL> [ ( '<STR_LIT>' , <EOL> users . create_login_url ( os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) ) ) ] ) <EOL> return [ ] <EOL> else : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return [ '<STR_LIT>' ] <EOL> return self . _application ( environ , start_response ) <EOL> app = AuthCheckMiddleware ( webapp . WSGIApplication ( URLMAP , debug = DEBUG ) ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> util . run_bare_wsgi_app ( app ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
