<s> import datetime <EOL> import json <EOL> from django . conf import settings <EOL> from django . shortcuts import render , get_object_or_404 <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . timezone import now <EOL> from rest_framework . generics import CreateAPIView <EOL> from rest_framework . authentication import SessionAuthentication <EOL> from salmon . core import authentication , permissions <EOL> from . import forms , models , serializers <EOL> class CreateMetricView ( CreateAPIView ) : <EOL> """<STR_LIT>""" <EOL> authentication_classes = ( authentication . SettingsAuthentication , <EOL> SessionAuthentication ) <EOL> permission_classes = ( permissions . SalmonPermission , ) <EOL> model = models . Metric <EOL> serializer_class = serializers . MetricSerializer <EOL> def get_serializer ( self , instance = None , data = None , files = None , <EOL> many = False , partial = False ) : <EOL> if isinstance ( data , list ) : <EOL> many = True <EOL> return super ( CreateMetricView , self ) . get_serializer ( instance , data , <EOL> files , many , <EOL> partial ) <EOL> def dashboard ( request ) : <EOL> """<STR_LIT>""" <EOL> sources = ( models . Source . objects . all ( ) . prefetch_related ( '<STR_LIT>' ) <EOL> . order_by ( '<STR_LIT:name>' ) ) <EOL> metrics = SortedDict ( [ ( src , src . metric_set . all ( ) ) for src in sources ] ) <EOL> no_source_metrics = models . Metric . objects . filter ( source__isnull = True ) <EOL> if no_source_metrics : <EOL> metrics [ '<STR_LIT>' ] = no_source_metrics <EOL> if request . META . get ( '<STR_LIT>' , False ) : <EOL> parent_template = '<STR_LIT>' <EOL> else : <EOL> parent_template = '<STR_LIT>' <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : metrics , <EOL> '<STR_LIT>' : parent_template <EOL> } ) <EOL> def history ( request , name ) : <EOL> source = get_object_or_404 ( models . Source , name = name ) <EOL> from_date = now ( ) - datetime . timedelta ( hours = <NUM_LIT:12> ) <EOL> to_date = now ( ) <EOL> initial = { <EOL> "<STR_LIT>" : from_date , <EOL> "<STR_LIT>" : to_date <EOL> } <EOL> if "<STR_LIT>" in request . GET or "<STR_LIT>" in request . GET : <EOL> data = request . GET . copy ( ) <EOL> if not request . GET . get ( "<STR_LIT>" ) : <EOL> data . setlist ( "<STR_LIT>" , [ from_date ] ) <EOL> if not request . GET . get ( "<STR_LIT>" ) : <EOL> data . setlist ( "<STR_LIT>" , [ to_date ] ) <EOL> form = forms . FilterHistory ( data , initial = initial ) <EOL> if form . is_valid ( ) : <EOL> from_date = form . cleaned_data [ "<STR_LIT>" ] or from_date <EOL> to_date = form . cleaned_data [ "<STR_LIT>" ] or to_date <EOL> else : <EOL> form = forms . FilterHistory ( initial = initial ) <EOL> graphs = [ ] <EOL> for metric in source . metric_set . all ( ) . order_by ( '<STR_LIT:name>' ) : <EOL> history = metric . load_archive ( <EOL> from_date = from_date , <EOL> to_date = to_date ) <EOL> js_data = map ( lambda x : ( x [ <NUM_LIT:0> ] * <NUM_LIT:1000> , x [ <NUM_LIT:1> ] ) , history ) <EOL> graphs . append ( { <EOL> '<STR_LIT:name>' : metric . name , <EOL> '<STR_LIT:data>' : json . dumps ( js_data ) , <EOL> '<STR_LIT:type>' : '<STR_LIT:float>' , <EOL> } ) <EOL> if request . META . get ( '<STR_LIT>' , False ) : <EOL> parent_template = '<STR_LIT>' <EOL> else : <EOL> parent_template = '<STR_LIT>' <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:source>' : source , <EOL> '<STR_LIT>' : graphs , <EOL> '<STR_LIT>' : parent_template , <EOL> '<STR_LIT>' : settings . REFRESH_INTERVAL_HISTORY , <EOL> } ) </s>
