#NEWLINE# def post( self ) : #NEWLINE# #INDENT# ' Saves logs records to the Datastore for later viewing. ' #NEWLINE# encoded_data = self . request . body #NEWLINE# data = json . loads ( encoded_data ) #NEWLINE# service_name = data [ 'service_name' ] #NEWLINE# host = data [ 'host' ] #NEWLINE# log_lines = data [ 'logs' ] #NEWLINE# service = LoggedService . get_by_id ( service_name ) #NEWLINE# if ( service is None ) : #NEWLINE# #INDENT# service = LoggedService ( id = service_name ) #NEWLINE# service . hosts = [ host ] #NEWLINE# service . put ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( host not in service . hosts ) : #NEWLINE# #INDENT# service . hosts . append ( host ) #NEWLINE# service . put ( ) #NEWLINE# #UNINDENT# #UNINDENT# entities_to_store = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for log_line_dict in log_lines : #NEWLINE# #INDENT# the_time = int ( log_line_dict [ 'timestamp' ] ) #NEWLINE# reversed_time = ( ( ( 2 ** 34 ) - the_time ) * 1000000 ) #NEWLINE# key_name = ( ( service_name + host ) + str ( reversed_time ) ) #NEWLINE# log_line = None #NEWLINE# if ( key_name in entities_to_store ) : #NEWLINE# #INDENT# log_line = entities_to_store [ key_name ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log_line = RequestLogLine . get_by_id ( id = key_name ) #NEWLINE# #UNINDENT# if ( not log_line ) : #NEWLINE# #INDENT# log_line = RequestLogLine ( id = self ) #NEWLINE# log_line . service_name = service_name #NEWLINE# log_line . host = host #NEWLINE# entities_to_store [ key_name ] = log_line #NEWLINE# #UNINDENT# app_log_line = AppLogLine ( ) #NEWLINE# app_log_line . message = log_line_dict [ 'message' ] #NEWLINE# app_log_line . level = log_line_dict [ 'level' ] #NEWLINE# app_log_line . timestamp = datetime . datetime . fromtimestamp ( the_time ) #NEWLINE# log_line . app_logs . append ( app_log_line ) #NEWLINE# entities_to_store [ key_name ] = log_line #NEWLINE# #UNINDENT# batch_put = [ ] #NEWLINE# for key_name in entities_to_store : #NEWLINE# #INDENT# batch_put . append ( entities_to_store [ key_name ] ) #NEWLINE# #UNINDENT# ndb . put_multi ( batch_put )