room-visualizer / qa /store.py
github-actions[bot]
Automated Deploy: 909dd51229176b0c49faf7e3156f93b4eebe8cd7
c97431f
Raw History Blame Contribute Delete
1.33 kB
"""QA run persistence helpers: read/write run JSON, SSE formatting, utilities."""
import json
from datetime import datetime, timezone
from pathlib import Path
from config.paths import QA_RUN_DIR
from errors import bad_request, not_found
def qa_utc_now() -> datetime:
return datetime.now(timezone.utc)
def qa_iso_now() -> str:
return qa_utc_now().isoformat().replace("+00:00", "Z")
def qa_run_root(run_id: str) -> Path:
if Path(run_id).name != run_id or not run_id:
bad_request("Invalid QA run id.")
return QA_RUN_DIR / run_id
def qa_run_file(run_id: str) -> Path:
return qa_run_root(run_id) / "run.json"
def read_qa_run(run_id: str) -> dict:
path = qa_run_file(run_id)
if not path.exists():
not_found("QA run not found.")
return json.loads(path.read_text())
def write_qa_run(run: dict):
run["updatedAt"] = qa_iso_now()
root = qa_run_root(run["id"])
root.mkdir(parents=True, exist_ok=True)
qa_run_file(run["id"]).write_text(json.dumps(run, indent=2))
def qa_sse_event(event_name: str, payload: dict) -> str:
data = json.dumps(payload, separators=(",", ":"))
return f"event: {event_name}\ndata: {data}\n\n"
def qa_int(value, default: int = 0) -> int:
try:
return int(value)
except (TypeError, ValueError):
return default