<s> from st2common . util import isotime <EOL> from st2common . models . api . base import BaseAPI <EOL> from st2common . models . db . trace import TraceDB , TraceComponentDB <EOL> TraceComponentAPISchema = { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : isotime . ISO8601_UTC_REGEX <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT:type>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : False <EOL> } <EOL> class TraceAPI ( BaseAPI ) : <EOL> model = TraceDB <EOL> schema = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:default>' : None <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : TraceComponentAPISchema <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : TraceComponentAPISchema <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : TraceComponentAPISchema <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : isotime . ISO8601_UTC_REGEX <EOL> } , <EOL> } , <EOL> '<STR_LIT>' : False <EOL> } <EOL> @ classmethod <EOL> def to_component_model ( cls , component ) : <EOL> values = { <EOL> '<STR_LIT>' : component [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : component [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : component . get ( '<STR_LIT>' , { } ) <EOL> } <EOL> updated_at = component . get ( '<STR_LIT>' , None ) <EOL> if updated_at : <EOL> values [ '<STR_LIT>' ] = isotime . parse ( updated_at ) <EOL> return TraceComponentDB ( ** values ) <EOL> @ classmethod <EOL> def to_model ( cls , instance ) : <EOL> values = { <EOL> '<STR_LIT>' : instance . trace_tag <EOL> } <EOL> action_executions = getattr ( instance , '<STR_LIT>' , [ ] ) <EOL> action_executions = [ TraceAPI . to_component_model ( component = action_execution ) <EOL> for action_execution in action_executions ] <EOL> values [ '<STR_LIT>' ] = action_executions <EOL> rules = getattr ( instance , '<STR_LIT>' , [ ] ) <EOL> rules = [ TraceAPI . to_component_model ( component = rule ) for rule in rules ] <EOL> values [ '<STR_LIT>' ] = rules <EOL> trigger_instances = getattr ( instance , '<STR_LIT>' , [ ] ) <EOL> trigger_instances = [ TraceAPI . to_component_model ( component = trigger_instance ) <EOL> for trigger_instance in trigger_instances ] <EOL> values [ '<STR_LIT>' ] = trigger_instances <EOL> start_timestamp = getattr ( instance , '<STR_LIT>' , None ) <EOL> if start_timestamp : <EOL> values [ '<STR_LIT>' ] = isotime . parse ( start_timestamp ) <EOL> return cls . model ( ** values ) <EOL> @ classmethod <EOL> def from_component_model ( cls , component_model ) : <EOL> return { '<STR_LIT>' : component_model . object_id , <EOL> '<STR_LIT>' : component_model . ref , <EOL> '<STR_LIT>' : isotime . format ( component_model . updated_at , offset = False ) , <EOL> '<STR_LIT>' : component_model . caused_by } <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> instance = cls . _from_model ( model , mask_secrets = mask_secrets ) <EOL> instance [ '<STR_LIT>' ] = isotime . format ( model . start_timestamp , offset = False ) <EOL> if model . action_executions : <EOL> instance [ '<STR_LIT>' ] = [ cls . from_component_model ( action_execution ) <EOL> for action_execution in model . action_executions ] <EOL> if model . rules : <EOL> instance [ '<STR_LIT>' ] = [ cls . from_component_model ( rule ) for rule in model . rules ] <EOL> if model . trigger_instances : <EOL> instance [ '<STR_LIT>' ] = [ cls . from_component_model ( trigger_instance ) <EOL> for trigger_instance in model . trigger_instances ] <EOL> return cls ( ** instance ) <EOL> class TraceContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id_ = None , trace_tag = None ) : <EOL> self . id_ = id_ <EOL> self . trace_tag = trace_tag <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . id_ , self . trace_tag ) <EOL> def __json__ ( self ) : <EOL> return vars ( self ) </s>
