Spaces:
Sleeping
Sleeping
Create telemetry.py
Browse files- telemetry.py +33 -0
telemetry.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
class Telemetry:
|
| 6 |
+
def __init__(self, agent_id):
|
| 7 |
+
self.agent_id = agent_id
|
| 8 |
+
self.log_file = f"telemetry_{agent_id}.json"
|
| 9 |
+
self._init_log()
|
| 10 |
+
|
| 11 |
+
def _init_log(self):
|
| 12 |
+
if not os.path.exists(self.log_file):
|
| 13 |
+
with open(self.log_file, "w") as f:
|
| 14 |
+
json.dump({"events": []}, f)
|
| 15 |
+
|
| 16 |
+
def log(self, event_type, status, details=None):
|
| 17 |
+
if details is None:
|
| 18 |
+
details = {}
|
| 19 |
+
|
| 20 |
+
entry = {
|
| 21 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 22 |
+
"event_type": event_type,
|
| 23 |
+
"status": status,
|
| 24 |
+
"details": details,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
with open(self.log_file, "r") as f:
|
| 28 |
+
data = json.load(f)
|
| 29 |
+
data["events"].append(entry)
|
| 30 |
+
with open(self.log_file, "w") as f:
|
| 31 |
+
json.dump(data, f, indent=2)
|
| 32 |
+
|
| 33 |
+
print(f"[Telemetry] {event_type} - {status} | {details}")
|