<s> from functools import wraps <EOL> import time <EOL> import logging <EOL> from playhouse . gfk import Model <EOL> import peewee <EOL> from playhouse . postgres_ext import PostgresqlExtDatabase <EOL> from redash import statsd_client <EOL> metrics_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class MeteredPostgresqlExtDatabase ( PostgresqlExtDatabase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . query_count = <NUM_LIT:0> <EOL> self . query_duration = <NUM_LIT:0> <EOL> return super ( MeteredPostgresqlExtDatabase , self ) . __init__ ( * args , ** kwargs ) <EOL> def execute_sql ( self , * args , ** kwargs ) : <EOL> start_time = time . time ( ) <EOL> try : <EOL> result = super ( MeteredPostgresqlExtDatabase , self ) . execute_sql ( * args , ** kwargs ) <EOL> return result <EOL> finally : <EOL> self . query_count += <NUM_LIT:1> <EOL> duration = ( time . time ( ) - start_time ) * <NUM_LIT:1000> <EOL> self . query_duration += duration <EOL> def reset_metrics ( self ) : <EOL> self . query_count = <NUM_LIT:0> <EOL> self . query_duration = <NUM_LIT:0> <EOL> def patch_query_execute ( ) : <EOL> real_execute = peewee . Query . _execute <EOL> real_clone = peewee . Query . clone <EOL> @ wraps ( real_execute ) <EOL> def metered_execute ( self , * args , ** kwargs ) : <EOL> name = self . model_class . __name__ <EOL> action = getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) <EOL> start_time = time . time ( ) <EOL> try : <EOL> result = real_execute ( self , * args , ** kwargs ) <EOL> return result <EOL> finally : <EOL> duration = ( time . time ( ) - start_time ) * <NUM_LIT:1000> <EOL> statsd_client . timing ( '<STR_LIT>' . format ( name , action ) , duration ) <EOL> metrics_logger . debug ( "<STR_LIT>" , name , action , duration ) <EOL> @ wraps ( real_clone ) <EOL> def extended_clone ( self ) : <EOL> cloned = real_clone ( self ) <EOL> setattr ( cloned , '<STR_LIT>' , getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return cloned <EOL> peewee . Query . _execute = metered_execute <EOL> peewee . Query . clone = extended_clone <EOL> patch_query_execute ( ) <EOL> class MeteredModel ( Model ) : <EOL> @ classmethod <EOL> def select ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def update ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def insert ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def insert_many ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def insert_from ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def delete ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def raw ( cls , * args , ** kwargs ) : <EOL> return cls . _execute_and_measure ( '<STR_LIT>' , args , kwargs ) <EOL> @ classmethod <EOL> def _execute_and_measure ( cls , action , args , kwargs ) : <EOL> result = getattr ( super ( MeteredModel , cls ) , action ) ( * args , ** kwargs ) <EOL> setattr ( result , '<STR_LIT>' , action ) <EOL> return result </s>
