<s> """<STR_LIT>""" <EOL> from grr . gui import api_call_handler_base <EOL> from grr . gui import api_value_renderers <EOL> from grr . lib import aff4 <EOL> from grr . lib import rdfvalue <EOL> from grr . lib import timeseries <EOL> from grr . lib import utils <EOL> from grr . lib . aff4_objects import stats_store as stats_store_lib <EOL> from grr . lib . rdfvalues import structs as rdf_structs <EOL> from grr . proto import api_pb2 <EOL> CATEGORY = "<STR_LIT>" <EOL> class ApiListStatsStoreMetricsMetadataArgs ( rdf_structs . RDFProtoStruct ) : <EOL> protobuf = api_pb2 . ApiListStatsStoreMetricsMetadataArgs <EOL> class ApiListStatsStoreMetricsMetadataHandler ( <EOL> api_call_handler_base . ApiCallHandler ) : <EOL> """<STR_LIT>""" <EOL> category = CATEGORY <EOL> args_type = ApiListStatsStoreMetricsMetadataArgs <EOL> def Render ( self , args , token = None ) : <EOL> stats_store = aff4 . FACTORY . Create ( None , aff4_type = "<STR_LIT>" , <EOL> mode = "<STR_LIT:w>" , token = token ) <EOL> process_ids = [ pid for pid in stats_store . ListUsedProcessIds ( ) <EOL> if pid . startswith ( args . component . name . lower ( ) ) ] <EOL> if not process_ids : <EOL> return { } <EOL> else : <EOL> metadata = stats_store . ReadMetadata ( process_id = process_ids [ <NUM_LIT:0> ] ) <EOL> return api_value_renderers . RenderValue ( metadata ) <EOL> class ApiGetStatsStoreMetricArgs ( rdf_structs . RDFProtoStruct ) : <EOL> protobuf = api_pb2 . ApiGetStatsStoreMetricArgs <EOL> class ApiGetStatsStoreMetricHandler ( api_call_handler_base . ApiCallHandler ) : <EOL> """<STR_LIT>""" <EOL> category = CATEGORY <EOL> args_type = ApiGetStatsStoreMetricArgs <EOL> def Render ( self , args , token ) : <EOL> stats_store = aff4 . FACTORY . Create ( <EOL> stats_store_lib . StatsStore . DATA_STORE_ROOT , <EOL> aff4_type = "<STR_LIT>" , mode = "<STR_LIT>" , token = token ) <EOL> process_ids = stats_store . ListUsedProcessIds ( ) <EOL> filtered_ids = [ pid for pid in process_ids <EOL> if pid . startswith ( args . component . name . lower ( ) ) ] <EOL> start_time = args . start <EOL> end_time = args . end <EOL> if not end_time : <EOL> end_time = rdfvalue . RDFDatetime ( ) . Now ( ) <EOL> if not start_time : <EOL> start_time = end_time - rdfvalue . Duration ( "<STR_LIT>" ) <EOL> base_start_time = start_time <EOL> start_time = start_time - rdfvalue . Duration ( "<STR_LIT>" ) <EOL> if end_time <= start_time : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> result = dict ( <EOL> start = base_start_time . AsMicroSecondsFromEpoch ( ) , <EOL> end = end_time . AsMicroSecondsFromEpoch ( ) , <EOL> metric_name = args . metric_name , <EOL> timeseries = [ ] ) <EOL> data = stats_store . MultiReadStats ( <EOL> process_ids = filtered_ids , <EOL> metric_name = utils . SmartStr ( args . metric_name ) , <EOL> timestamp = ( start_time , end_time ) ) <EOL> if not data : <EOL> return result <EOL> pid = data . keys ( ) [ <NUM_LIT:0> ] <EOL> metadata = stats_store . ReadMetadata ( process_id = pid ) <EOL> metric_metadata = metadata [ args . metric_name ] <EOL> query = stats_store_lib . StatsStoreDataQuery ( data ) <EOL> query . In ( args . component . name . lower ( ) + "<STR_LIT>" ) . In ( args . metric_name ) <EOL> if metric_metadata . fields_defs : <EOL> query . InAll ( ) <EOL> requested_duration = end_time - start_time <EOL> if requested_duration >= rdfvalue . Duration ( "<STR_LIT>" ) : <EOL> sampling_duration = rdfvalue . Duration ( "<STR_LIT>" ) <EOL> elif requested_duration >= rdfvalue . Duration ( "<STR_LIT>" ) : <EOL> sampling_duration = rdfvalue . Duration ( "<STR_LIT>" ) <EOL> else : <EOL> sampling_duration = rdfvalue . Duration ( "<STR_LIT>" ) <EOL> if metric_metadata . metric_type == metric_metadata . MetricType . COUNTER : <EOL> query . TakeValue ( ) . MakeIncreasing ( ) . Normalize ( <EOL> sampling_duration , <EOL> start_time , <EOL> end_time , <EOL> mode = timeseries . NORMALIZE_MODE_COUNTER ) <EOL> elif metric_metadata . metric_type == metric_metadata . MetricType . EVENT : <EOL> if args . distribution_handling_mode == "<STR_LIT>" : <EOL> query . TakeDistributionSum ( ) <EOL> elif args . distribution_handling_mode == "<STR_LIT>" : <EOL> query . TakeDistributionCount ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % args . distribution_handling_mode ) <EOL> query . MakeIncreasing ( ) <EOL> query . Normalize ( sampling_duration , <EOL> start_time , <EOL> end_time , <EOL> mode = timeseries . NORMALIZE_MODE_COUNTER ) <EOL> elif metric_metadata . metric_type == metric_metadata . MetricType . GAUGE : <EOL> query . TakeValue ( ) . Normalize ( sampling_duration , start_time , end_time ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if args . aggregation_mode == "<STR_LIT>" : <EOL> query . AggregateViaSum ( ) <EOL> elif args . aggregation_mode == "<STR_LIT>" : <EOL> query . AggregateViaMean ( ) <EOL> elif args . aggregation_mode == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> args . aggregation ) <EOL> if ( args . rate and <EOL> metric_metadata . metric_type != metric_metadata . MetricType . GAUGE ) : <EOL> query . Rate ( ) <EOL> query . InTimeRange ( base_start_time , end_time ) <EOL> ts = [ ] <EOL> for value , timestamp in query . ts . data : <EOL> if value is not None : <EOL> ts . append ( ( timestamp / <NUM_LIT> , value ) ) <EOL> result [ "<STR_LIT>" ] = ts <EOL> return result </s>
