<s> import time <EOL> import re <EOL> import logging <EOL> from error import DiamondException <EOL> class Metric ( object ) : <EOL> _METRIC_TYPES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , path , value , raw_value = None , timestamp = None , precision = <NUM_LIT:0> , <EOL> metric_type = '<STR_LIT>' , ttl = None , host = "<STR_LIT>" , dimensions = None ) : <EOL> """<STR_LIT>""" <EOL> if ( None in [ path , value ] or metric_type not in self . _METRIC_TYPES ) : <EOL> raise DiamondException ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % ( path , value , metric_type ) ) <EOL> if timestamp is None : <EOL> timestamp = int ( time . time ( ) ) <EOL> else : <EOL> if not isinstance ( timestamp , int ) : <EOL> try : <EOL> timestamp = int ( timestamp ) <EOL> except ValueError , e : <EOL> raise DiamondException ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % ( path , e ) ) <EOL> if not isinstance ( value , ( int , float ) ) : <EOL> try : <EOL> if precision == <NUM_LIT:0> : <EOL> value = round ( float ( value ) ) <EOL> else : <EOL> value = float ( value ) <EOL> except ValueError , e : <EOL> raise DiamondException ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( path , e ) ) <EOL> if dimensions is not None : <EOL> if not isinstance ( dimensions , dict ) : <EOL> raise DiamondException ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % ( path , dimensions ) ) <EOL> else : <EOL> dimensions = dict ( <EOL> ( k , str ( v ) ) for k , v in dimensions . iteritems ( ) <EOL> if v is not None and isinstance ( v , ( int , float , str , unicode ) ) and k is not None and isinstance ( k , str ) <EOL> ) <EOL> self . dimensions = dimensions <EOL> self . path = path <EOL> self . value = value <EOL> self . raw_value = raw_value <EOL> self . timestamp = timestamp <EOL> self . precision = precision <EOL> self . metric_type = metric_type <EOL> self . ttl = ttl <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( self . precision , ( int , long ) ) : <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> log . warn ( '<STR_LIT>' , self . path ) <EOL> self . precision = <NUM_LIT:0> <EOL> fstring = "<STR_LIT>" % self . precision <EOL> return fstring % ( self . path , self . value , self . timestamp ) <EOL> @ classmethod <EOL> def parse ( cls , string ) : <EOL> """<STR_LIT>""" <EOL> match = re . match ( r'<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' , <EOL> string ) <EOL> try : <EOL> groups = match . groupdict ( ) <EOL> return Metric ( groups [ '<STR_LIT:name>' ] , <EOL> groups [ '<STR_LIT:value>' ] , <EOL> float ( groups [ '<STR_LIT>' ] ) ) <EOL> except : <EOL> raise DiamondException ( <EOL> "<STR_LIT>" % string ) <EOL> def getPathPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . path . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> def getCollectorPath ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . path . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> ] <EOL> def getMetricPath ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . path . split ( '<STR_LIT:.>' ) [ <NUM_LIT:2> : ] <EOL> return '<STR_LIT:.>' . join ( path ) </s>
