<s> """<STR_LIT>""" <EOL> from diamond . collector import str_to_bool <EOL> from jolokia import JolokiaCollector , MBean <EOL> import math <EOL> import string <EOL> import re <EOL> class CassandraJolokiaCollector ( JolokiaCollector ) : <EOL> def get_default_config_help ( self ) : <EOL> config_help = super ( CassandraJolokiaCollector , <EOL> self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> config = super ( CassandraJolokiaCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:False>' <EOL> } ) <EOL> return config <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CassandraJolokiaCollector , self ) . __init__ ( * args , ** kwargs ) <EOL> self . offsets = self . create_offsets ( <NUM_LIT> ) <EOL> self . update_config ( self . config ) <EOL> def update_config ( self , config ) : <EOL> if '<STR_LIT>' in config : <EOL> self . percentiles = map ( int , config [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in config : <EOL> self . histogram_regex = re . compile ( config [ '<STR_LIT>' ] ) <EOL> def collect_bean ( self , prefix , obj ) : <EOL> for k , v in obj . iteritems ( ) : <EOL> if type ( v ) in [ int , float , long ] : <EOL> self . parse_dimension_bean ( prefix , k , v ) <EOL> elif isinstance ( v , dict ) and str_to_bool ( self . config [ '<STR_LIT>' ] ) : <EOL> self . collect_bean ( "<STR_LIT>" % ( prefix , k ) , v ) <EOL> elif isinstance ( v , list ) and str_to_bool ( self . config [ '<STR_LIT>' ] ) : <EOL> self . interpret_bean_with_list ( "<STR_LIT>" % ( prefix , k ) , v ) <EOL> def patch_dimensions ( self , bean , dims ) : <EOL> metric_name = dims . pop ( "<STR_LIT:name>" , None ) <EOL> metric_type = dims . pop ( "<STR_LIT:type>" , None ) <EOL> scope_type = dims . pop ( "<STR_LIT>" , None ) <EOL> if scope_type : <EOL> dims [ "<STR_LIT:type>" ] = scope_type <EOL> return metric_name , metric_type , dims <EOL> def patch_metric_name ( self , bean , metric_name_list ) : <EOL> if self . config . get ( '<STR_LIT>' , None ) : <EOL> metric_name_list = [ self . config [ '<STR_LIT>' ] ] + metric_name_list <EOL> if bean . bean_key . lower ( ) != '<STR_LIT:value>' : <EOL> metric_name_list . append ( bean . bean_key . lower ( ) ) <EOL> return metric_name_list <EOL> def interpret_bean_with_list ( self , prefix , values ) : <EOL> if not self . histogram_regex . match ( prefix ) : <EOL> return <EOL> buckets = values <EOL> offsets = self . offsets <EOL> for percentile in self . percentiles : <EOL> value = self . compute_percentile ( offsets , buckets , percentile ) <EOL> cleaned_key = self . clean_up ( "<STR_LIT>" % ( prefix , percentile ) ) <EOL> self . publish ( cleaned_key , value ) <EOL> def compute_percentile ( self , offsets , buckets , percentile_int ) : <EOL> non_zero_points_sum = sum ( buckets ) <EOL> if non_zero_points_sum is <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> middle_point_index = math . floor ( <EOL> non_zero_points_sum * ( percentile_int / float ( <NUM_LIT:100> ) ) ) <EOL> points_seen = <NUM_LIT:0> <EOL> for index , bucket in enumerate ( buckets ) : <EOL> points_seen += bucket <EOL> if points_seen >= middle_point_index : <EOL> return round ( ( offsets [ index ] - offsets [ index - <NUM_LIT:1> ] ) / <NUM_LIT:2> ) <EOL> def create_offsets ( self , bucket_count ) : <EOL> last_num = <NUM_LIT:1> <EOL> offsets = [ last_num ] <EOL> for index in range ( bucket_count ) : <EOL> next_num = round ( last_num * <NUM_LIT> ) <EOL> if next_num == last_num : <EOL> next_num += <NUM_LIT:1> <EOL> offsets . append ( next_num ) <EOL> last_num = next_num <EOL> return offsets </s>
