22
33from pymongo import MongoClient , errors
44from bson import ObjectId
5- from datetime import datetime
5+ from datetime import datetime , timezone
66import os
77import time
88import json
@@ -46,6 +46,13 @@ def add_digital_twin(self, userRef, digital_twin_data):
4646 )
4747
4848 return digital_twin_id
49+
50+ def add_logs (self , log_data_list ):
51+ logs_collection = self .db ["logs" ]
52+
53+ log_ids = logs_collection .insert_many (log_data_list ).inserted_ids
54+
55+ return log_ids
4956
5057
5158 def append_execution (self , digital_twin_id , execution_data ):
@@ -78,18 +85,18 @@ def append_step(self, execution_id, step_data):
7885 return step_id
7986
8087 def add_output (self , step_id , output_data ):
81- steps_collection = self .db ["outputs" ]
88+ output_collection = self .db ["outputs" ]
8289 output_data ["stepRef" ] = step_id
8390
8491 # TODO: Make its own function. Taking out user_id
8592 #output_data["access_control"]["authorized_users"] = user_id
8693
87- output_id = steps_collection .insert_one (output_data ).inserted_id
94+ output_id = output_collection .insert_one (output_data ).inserted_id
8895
89- # Update digital twin with execution reference
90- self .db .digitalTwins .update_one (
96+ # Update steps with execution reference
97+ self .db .steps .update_one (
9198 {"_id" : ObjectId (step_id )}, # Specify the document to update
92- {"$set" : {"field_name " : output_id }} # Use $set to replace the value of a field
99+ {"$set" : {"output " : output_id }} # Use $set to replace the value of a field
93100 )
94101
95102 return output_id
@@ -108,15 +115,25 @@ def append_logs(self, step_id, log_data_list):
108115 {"_id" : ObjectId (step_id )},
109116 {"$push" : {"logs" : {"$each" : log_data_list }}}
110117 )
111-
112- def add_result (self , digital_twin_id , execution_id , result_data ):
118+
119+ def update_result (self , result_id , output_id ):
113120 results_collection = self .db ["results" ]
114- result_data ["digitalTwinRef" ] = digital_twin_id
115- result_data ["executionRed" ] = execution_id
116-
117- result_id = results_collection .insert_one (result_data ).inserted_id
121+ results_collection .update_one (
122+ {"_id" : ObjectId (result_id )},
123+ {"$push" : {"output" : output_id }}
124+ )
125+
126+ results_collection .update_one (
127+ {"_id" : ObjectId (result_id )},
128+ {"$set" : {"updated_at" : datetime .now (timezone .utc )}}
129+ )
118130
119- return result_id
131+ def update_end_time (self , step_id ):
132+ steps_collection = self .db ["steps" ]
133+ steps_collection .update_one (
134+ {"_id" : ObjectId (step_id )},
135+ {"$set" : {"end_timestamp" : datetime .now (timezone .utc )}}
136+ )
120137
121138 ######### Get methods
122139
@@ -260,21 +277,26 @@ def main(delay=2):
260277 newLogList = []
261278 for log in logs :
262279 newLogEntry = {
263- "timestamp " : datetime . utcnow () ,
264- "type " : "INFO" ,
280+ "stepRef " : step_id ,
281+ "timestamp " : datetime . now ( timezone . utc ) ,
265282 "logstring" : log }
266283
267284 newLogList .append (newLogEntry )
268285
269286
270287 dbManager = MongoManager (MONGO_URL , db_name )
271- _ = dbManager .append_logs (step_id , newLogList )
288+ # _ = dbManager.append_logs(step_id, newLogList)
289+ _ = dbManager .add_logs (newLogList )
272290 dbManager .close ()
273291
274292 time .sleep (0.2 )
275293
276294 # TODO: Improve this
277295 if log == "--- ODTP COMPONENT ENDING ---" :
296+ dbManager = MongoManager (MONGO_URL , db_name )
297+ dbManager .update_end_time (step_id )
298+ dbManager .close ()
299+
278300 ending_detected = True
279301
280302 #time.sleep(delay)
0 commit comments