<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import json <EOL> import os <EOL> import time <EOL> import logging <EOL> from functools import partial <EOL> from tornado import auth , template , web <EOL> from viewfinder . backend . base import handler , counters <EOL> from viewfinder . backend . base . dotdict import DotDict <EOL> from viewfinder . backend . db import metric <EOL> from viewfinder . backend . db . db_client import RangeOperator <EOL> from viewfinder . backend . www . admin import admin <EOL> class CountersHandler ( admin . AdminHandler ) : <EOL> """<STR_LIT>""" <EOL> @ handler . authenticated ( ) <EOL> @ handler . asynchronous ( datastore = True ) <EOL> @ admin . require_permission ( level = '<STR_LIT:root>' ) <EOL> def get ( self ) : <EOL> logging . info ( '<STR_LIT>' % counters . counters ) <EOL> t_dict = self . PermissionsTemplateDict ( ) <EOL> t_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> t_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> t_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . render ( "<STR_LIT>" , ** t_dict ) <EOL> class CountersDataHandler ( admin . AdminHandler ) : <EOL> """<STR_LIT>""" <EOL> MAX_TICK_COUNT = <NUM_LIT> <EOL> """<STR_LIT>""" <EOL> @ handler . authenticated ( ) <EOL> @ handler . asynchronous ( datastore = True ) <EOL> @ admin . require_permission ( level = '<STR_LIT:root>' ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> def _OnAggregation ( aggregator ) : <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . write ( json . dumps ( aggregator , default = _SerializeAggregateMetrics ) ) <EOL> self . finish ( ) <EOL> start_time = float ( self . get_argument ( '<STR_LIT:start>' ) ) <EOL> end_time = float ( self . get_argument ( '<STR_LIT:end>' ) ) <EOL> intervals = metric . METRIC_INTERVALS <EOL> min_resolution = ( end_time - start_time ) / self . MAX_TICK_COUNT <EOL> selected_interval = next ( ( i for i in intervals if i . length > min_resolution ) , intervals [ - <NUM_LIT:1> ] ) <EOL> group_key = metric . Metric . EncodeGroupKey ( metric . DEFAULT_CLUSTER_NAME , selected_interval ) <EOL> logging . info ( '<STR_LIT>' <EOL> % ( time . ctime ( start_time ) , time . ctime ( end_time ) , selected_interval . name ) ) <EOL> metric . AggregatedMetric . CreateAggregateForTimespan ( self . _client , group_key , start_time , end_time , <EOL> counters . counters , _OnAggregation ) <EOL> def _SerializeAggregateMetrics ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , metric . AggregatedMetric ) : <EOL> return { '<STR_LIT>' : obj . start_time , <EOL> '<STR_LIT>' : obj . end_time , <EOL> '<STR_LIT>' : obj . group_key , <EOL> '<STR_LIT>' : list ( obj . machines ) , <EOL> '<STR_LIT:data>' : obj . counter_data <EOL> } <EOL> elif isinstance ( obj , metric . AggregatedMetric . AggregatedCounter ) : <EOL> logging . info ( '<STR_LIT>' % obj . description ) <EOL> return { '<STR_LIT:description>' : obj . description , <EOL> '<STR_LIT>' : obj . is_average , <EOL> '<STR_LIT>' : obj . machine_data , <EOL> '<STR_LIT>' : obj . cluster_total , <EOL> '<STR_LIT>' : obj . cluster_avg <EOL> } <EOL> else : <EOL> raise web . HTTPError ( <NUM_LIT> , "<STR_LIT>" % obj ) </s>
