<s> from cqlshlib . displaying import MAGENTA <EOL> from datetime import datetime <EOL> import time <EOL> from cassandra . query import QueryTrace , TraceUnavailable <EOL> def print_trace_session ( shell , session , session_id ) : <EOL> """<STR_LIT>""" <EOL> trace = QueryTrace ( session_id , session ) <EOL> try : <EOL> trace . populate ( ) <EOL> except TraceUnavailable : <EOL> shell . printerr ( "<STR_LIT>" % session_id ) <EOL> else : <EOL> print_trace ( shell , trace ) <EOL> def print_trace ( shell , trace ) : <EOL> """<STR_LIT>""" <EOL> rows = make_trace_rows ( trace ) <EOL> if not rows : <EOL> shell . printerr ( "<STR_LIT>" % ( trace . trace_id , ) ) <EOL> return <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:source>' , '<STR_LIT>' ] <EOL> formatted_names = map ( shell . myformat_colname , names ) <EOL> formatted_values = [ map ( shell . myformat_value , row ) for row in rows ] <EOL> shell . writeresult ( '<STR_LIT>' ) <EOL> shell . writeresult ( '<STR_LIT>' , color = MAGENTA , newline = False ) <EOL> shell . writeresult ( trace . trace_id ) <EOL> shell . writeresult ( '<STR_LIT>' ) <EOL> shell . print_formatted_result ( formatted_names , formatted_values ) <EOL> shell . writeresult ( '<STR_LIT>' ) <EOL> def make_trace_rows ( trace ) : <EOL> if not trace . events : <EOL> return [ ] <EOL> rows = [ [ trace . request_type , str ( datetime_from_utc_to_local ( trace . started_at ) ) , trace . coordinator , <NUM_LIT:0> ] ] <EOL> for event in trace . events : <EOL> rows . append ( [ "<STR_LIT>" % ( event . description , event . thread_name ) , <EOL> str ( datetime_from_utc_to_local ( event . datetime ) ) , <EOL> event . source , <EOL> event . source_elapsed . microseconds if event . source_elapsed else "<STR_LIT>" ] ) <EOL> if trace . duration : <EOL> finished_at = ( datetime_from_utc_to_local ( trace . started_at ) + trace . duration ) <EOL> else : <EOL> finished_at = trace . duration = "<STR_LIT>" <EOL> rows . append ( [ '<STR_LIT>' , str ( finished_at ) , trace . coordinator , trace . duration . microseconds ] ) <EOL> return rows <EOL> def datetime_from_utc_to_local ( utc_datetime ) : <EOL> now_timestamp = time . time ( ) <EOL> offset = datetime . fromtimestamp ( now_timestamp ) - datetime . utcfromtimestamp ( now_timestamp ) <EOL> return utc_datetime + offset </s>
