sync: update metrics/pedi.py
Browse files- metrics/pedi.py +26 -0
metrics/pedi.py
CHANGED
|
@@ -397,6 +397,32 @@ class PediIndex:
|
|
| 397 |
).fetchall()
|
| 398 |
return [r[0] for r in reversed(rows)]
|
| 399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
# ββ Convenience factory ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 402 |
|
|
|
|
| 397 |
).fetchall()
|
| 398 |
return [r[0] for r in reversed(rows)]
|
| 399 |
|
| 400 |
+
def get_last_snapshot(self) -> Optional[StateSnapshot]:
|
| 401 |
+
"""Retrieve the last recorded snapshot from the database."""
|
| 402 |
+
try:
|
| 403 |
+
with sqlite3.connect(self.db_path) as conn:
|
| 404 |
+
conn.row_factory = sqlite3.Row
|
| 405 |
+
row = conn.execute(
|
| 406 |
+
"SELECT * FROM pedi_snapshots ORDER BY timestamp DESC LIMIT 1"
|
| 407 |
+
).fetchone()
|
| 408 |
+
if row:
|
| 409 |
+
needs = json.loads(row["needs_json"])
|
| 410 |
+
from datetime import datetime
|
| 411 |
+
# Handle possible timezone suffix or simple isoformat strings
|
| 412 |
+
ts_str = row["timestamp"]
|
| 413 |
+
# Replace Z with +00:00 for fromisoformat compatibility in python versions before 3.11 if needed
|
| 414 |
+
if ts_str.endswith("Z"):
|
| 415 |
+
ts_str = ts_str[:-1] + "+00:00"
|
| 416 |
+
return StateSnapshot(
|
| 417 |
+
timestamp=datetime.fromisoformat(ts_str),
|
| 418 |
+
turn_id=row["turn_id"],
|
| 419 |
+
needs=needs,
|
| 420 |
+
context_tokens_used=row["context_tokens_used"],
|
| 421 |
+
)
|
| 422 |
+
except Exception:
|
| 423 |
+
pass
|
| 424 |
+
return None
|
| 425 |
+
|
| 426 |
|
| 427 |
# ββ Convenience factory ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 428 |
|