<s> try : <EOL> from django . core . context_processors import csrf <EOL> except ImportError : <EOL> from django . template . context_processors import csrf <EOL> from django . db . models import Avg , Count , Sum , Max <EOL> from django . shortcuts import render_to_response <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . generic import View <EOL> from silk import models <EOL> from silk . auth import login_possibly_required , permissions_possibly_required <EOL> from silk . request_filters import BaseFilter , filters_from_request <EOL> class SummaryView ( View ) : <EOL> filters_key = '<STR_LIT>' <EOL> def _avg_num_queries ( self , filters ) : <EOL> queries__aggregate = models . Request . objects . filter ( * filters ) . annotate ( num_queries = Count ( '<STR_LIT>' ) ) . aggregate ( num = Avg ( '<STR_LIT>' ) ) <EOL> return queries__aggregate [ '<STR_LIT>' ] <EOL> def _avg_time_spent_on_queries ( self , filters ) : <EOL> taken__aggregate = models . Request . objects . filter ( * filters ) . annotate ( time_spent = Sum ( '<STR_LIT>' ) ) . aggregate ( num = Avg ( '<STR_LIT>' ) ) <EOL> return taken__aggregate [ '<STR_LIT>' ] <EOL> def _avg_overall_time ( self , filters ) : <EOL> taken__aggregate = models . Request . objects . filter ( * filters ) . annotate ( time_spent = Sum ( '<STR_LIT>' ) ) . aggregate ( num = Avg ( '<STR_LIT>' ) ) <EOL> return taken__aggregate [ '<STR_LIT>' ] <EOL> def _longest_query_by_view ( self , filters ) : <EOL> values_list = models . Request . objects . filter ( * filters ) . values_list ( "<STR_LIT>" ) . annotate ( max = Max ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:5> ] <EOL> requests = [ ] <EOL> for view_name , _ in values_list : <EOL> request = models . Request . objects . filter ( view_name = view_name , * filters ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> requests . append ( request ) <EOL> return requests <EOL> def _time_spent_in_db_by_view ( self , filters ) : <EOL> values_list = models . Request . objects . filter ( * filters ) . values_list ( '<STR_LIT>' ) . annotate ( t = Sum ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:5> ] <EOL> requests = [ ] <EOL> for view , _ in values_list : <EOL> r = models . Request . objects . filter ( view_name = view , * filters ) . annotate ( t = Sum ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> requests . append ( r ) <EOL> return requests <EOL> def _num_queries_by_view ( self , filters ) : <EOL> queryset = models . Request . objects . filter ( * filters ) . values_list ( '<STR_LIT>' ) . annotate ( t = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:5> ] <EOL> views = [ r [ <NUM_LIT:0> ] for r in queryset [ : <NUM_LIT:6> ] ] <EOL> requests = [ ] <EOL> for view in views : <EOL> try : <EOL> r = models . Request . objects . filter ( view_name = view , * filters ) . annotate ( t = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> requests . append ( r ) <EOL> except IndexError : <EOL> pass <EOL> return requests <EOL> def _create_context ( self , request ) : <EOL> raw_filters = request . session . get ( self . filters_key , { } ) <EOL> filters = [ BaseFilter . from_dict ( filter_d ) for _ , filter_d in raw_filters . items ( ) ] <EOL> avg_overall_time = self . _avg_num_queries ( filters ) <EOL> c = { <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : models . Request . objects . filter ( * filters ) . count ( ) , <EOL> '<STR_LIT>' : models . Profile . objects . filter ( * filters ) . count ( ) , <EOL> '<STR_LIT>' : avg_overall_time , <EOL> '<STR_LIT>' : self . _avg_time_spent_on_queries ( filters ) , <EOL> '<STR_LIT>' : self . _avg_overall_time ( filters ) , <EOL> '<STR_LIT>' : self . _longest_query_by_view ( filters ) , <EOL> '<STR_LIT>' : self . _time_spent_in_db_by_view ( filters ) , <EOL> '<STR_LIT>' : self . _num_queries_by_view ( filters ) , <EOL> '<STR_LIT>' : raw_filters <EOL> } <EOL> c . update ( csrf ( request ) ) <EOL> return c <EOL> @ method_decorator ( login_possibly_required ) <EOL> @ method_decorator ( permissions_possibly_required ) <EOL> def get ( self , request ) : <EOL> c = self . _create_context ( request ) <EOL> return render_to_response ( '<STR_LIT>' , c ) <EOL> @ method_decorator ( login_possibly_required ) <EOL> @ method_decorator ( permissions_possibly_required ) <EOL> def post ( self , request ) : <EOL> filters = filters_from_request ( request ) <EOL> request . session [ self . filters_key ] = { ident : f . as_dict ( ) for ident , f in filters . items ( ) } <EOL> return render_to_response ( '<STR_LIT>' , self . _create_context ( request ) ) </s>
