File size: 1,333 Bytes
790d9ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | """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
|