Skip to content

Commit ff0d9c3

Browse files
authored
Merge pull request #6 from odtp-org/output-results
v0.1.0
2 parents 0b75d5b + 3580ad2 commit ff0d9c3

4 files changed

Lines changed: 72 additions & 22 deletions

File tree

.env.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ODTP ENV VARIABLES TO CONNECT
2+
ODTP_MONGO_SERVER=
3+
ODTP_S3_SERVER=
4+
ODTP_BUCKET_NAME=
5+
ODTP_ACCESS_KEY=
6+
ODTP_SECRET_KEY=
7+
8+
# ODTP ENV VARIABLES DB REFERENCES
9+
ODTP_USER_ID=
10+
ODTP_DIGITAL_TWIN=
11+
ODTP_EXCUTION=
12+
ODTP_STEP=
13+
ODTP_COMPONENT=
14+
ODTP_COMPONENT_VERSION=
15+
ODTP_RESULT=
16+
17+
# OTHER ENV VARIABLES (FALSE/TRUE)
18+
TRANSFER_INPUT_TO_OUTPUT=FALSE
19+
ODTP_SAVE_SNAPSHOT=FALSE
20+
ODTP_SAVE_IN_RESULT=TRUE

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ Client for ODTP Components. This repository is aimed to be used as a submodule i
44

55
## Changelog
66

7+
- v0.1.0
8+
- Logging in `logs` collection as individual document.
9+
- Updating `output` document
10+
- Updating `result` document
11+
712
- v0.0.1
813
- First version
914
- Inluding conditional to make snapshot optionals
10-
- Adding quiet flag to compressing
15+
- Adding quiet flag to compressing odtp-output
1116

1217
# Acknowledgments, Copyright, and Licensing
1318
## Acknowledgments and Funding

logger.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pymongo import MongoClient, errors
44
from bson import ObjectId
5-
from datetime import datetime
5+
from datetime import datetime, timezone
66
import os
77
import time
88
import 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)

s3uploader.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime
1+
from datetime import datetime, timezone
22
import os
33
import time
44
import logging
@@ -176,8 +176,8 @@ def main():
176176
"file_name": "odtp-output.zip", # The name of the file in the output
177177
"file_size": file_size_bytes, # Size of the file in bytes
178178
"file_type": "application/zip", # MIME type or file type
179-
"created_at": datetime.utcnow(), # Timestamp when the output was created
180-
"updated_at": datetime.utcnow(), # Timestamp when the output was last updated
179+
"created_at": datetime.now(timezone.utc), # Timestamp when the output was created
180+
"updated_at": datetime.now(timezone.utc), # Timestamp when the output was last updated
181181
"metadata": { # Additional metadata associated with the output
182182
"description": "Description of the snapshot",
183183
"tags": ["tag1", "tag2"],
@@ -190,6 +190,9 @@ def main():
190190
}
191191

192192
odtp_output_id = dbManager.add_output(STEP_ID, output_data)
193+
194+
if os.getenv("ODTP_SAVE_IN_RESULT") == "TRUE":
195+
dbManager.update_result(os.getenv("ODTP_RESULT"), odtp_output_id)
193196

194197
logging.info("ODTP OUTPUT UPLOADED IN {}".format(odtp_output_id))
195198

@@ -209,8 +212,8 @@ def main():
209212
"file_name": "odtp-snapshot.zip", # The name of the file in the output
210213
"file_size": file_size_bytes, # Size of the file in bytes
211214
"file_type": "application/zip", # MIME type or file type
212-
"created_at": datetime.utcnow(), # Timestamp when the output was created
213-
"updated_at": datetime.utcnow(), # Timestamp when the output was last updated
215+
"created_at": datetime.now(timezone.utc), # Timestamp when the output was created
216+
"updated_at": datetime.now(timezone.utc), # Timestamp when the output was last updated
214217
"metadata": { # Additional metadata associated with the output
215218
"description": "Description of the snapshot",
216219
"tags": ["tag1", "tag2"],

0 commit comments

Comments
 (0)