<s> import re <EOL> import time <EOL> import redis <EOL> from operator import itemgetter <EOL> from optparse import make_option <EOL> from django . conf import settings <EOL> from django . core . management . base import NoArgsCommand , CommandError <EOL> from django_dumpslow . utils import parse_interval <EOL> class Command ( NoArgsCommand ) : <EOL> help = "<STR_LIT>" <EOL> option_list = NoArgsCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , default = None , metavar = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , default = <NUM_LIT:20> , <EOL> help = "<STR_LIT>" ) , <EOL> ) <EOL> def handle ( self , ** options ) : <EOL> def check_option ( name , val ) : <EOL> try : <EOL> val = int ( val ) <EOL> if val < <NUM_LIT:0> : <EOL> raise ValueError ( ) <EOL> return val <EOL> except ValueError : <EOL> raise CommandError ( <EOL> '<STR_LIT>' % ( name , val ) <EOL> ) <EOL> except TypeError : <EOL> pass <EOL> limit = check_option ( '<STR_LIT>' , options [ '<STR_LIT>' ] ) <EOL> max_duration = check_option ( '<STR_LIT>' , options [ '<STR_LIT>' ] ) <EOL> after = options [ '<STR_LIT>' ] <EOL> if after : <EOL> try : <EOL> after = int ( time . time ( ) ) - parse_interval ( after ) <EOL> except ValueError : <EOL> raise CommandError ( '<STR_LIT>' % after ) <EOL> order = options [ '<STR_LIT>' ] <EOL> if order not in ( '<STR_LIT>' , '<STR_LIT:count>' , '<STR_LIT>' ) : <EOL> raise CommandError ( '<STR_LIT>' % options [ '<STR_LIT>' ] ) <EOL> client = redis . Redis ( <EOL> host = settings . REDIS_HOST , <EOL> port = settings . REDIS_PORT , <EOL> ) <EOL> data = { } <EOL> totals = { } <EOL> hits = { } <EOL> results = client . zrangebyscore ( <EOL> getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) , after , '<STR_LIT>' , <EOL> ) <EOL> for line in results : <EOL> view , duration = line . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> duration = float ( duration ) <EOL> if max_duration and duration >= max_duration : <EOL> continue <EOL> if order == '<STR_LIT>' : <EOL> try : <EOL> data [ view ] += duration <EOL> except KeyError : <EOL> data [ view ] = duration <EOL> elif order == '<STR_LIT:count>' : <EOL> try : <EOL> data [ view ] += <NUM_LIT:1> <EOL> except KeyError : <EOL> data [ view ] = <NUM_LIT:1> <EOL> elif order == '<STR_LIT>' : <EOL> try : <EOL> totals [ view ] += duration <EOL> hits [ view ] += <NUM_LIT:1> <EOL> except KeyError : <EOL> totals [ view ] = duration <EOL> hits [ view ] = <NUM_LIT:1> <EOL> data [ view ] = totals [ view ] / hits [ view ] <EOL> items = data . items ( ) <EOL> del data <EOL> items . sort ( key = itemgetter ( <NUM_LIT:1> ) , reverse = not options [ '<STR_LIT>' ] ) <EOL> if limit is not None : <EOL> items = items [ : limit ] <EOL> print "<STR_LIT>" , "<STR_LIT>" , <EOL> print { <EOL> '<STR_LIT:count>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } [ order ] . rjust ( <NUM_LIT> ) <EOL> print "<STR_LIT>" , "<STR_LIT:=>" * <NUM_LIT> <EOL> for view , value in items : <EOL> pad = <NUM_LIT> - len ( view ) <EOL> print "<STR_LIT>" , view , <EOL> if order == '<STR_LIT>' : <EOL> print ( "<STR_LIT>" % value ) . rjust ( pad ) <EOL> elif order == '<STR_LIT:count>' : <EOL> print str ( value ) . rjust ( pad ) <EOL> elif order == '<STR_LIT>' : <EOL> print ( "<STR_LIT>" % value ) . rjust ( pad ) </s>
