phase 10
This commit is contained in:
@@ -3,6 +3,9 @@ Event emission helpers for detection pipeline stages.
|
||||
|
||||
Single place that knows how to build event payloads.
|
||||
Stages call these instead of constructing dicts or dataclasses directly.
|
||||
|
||||
Run context (run_id, parent_job_id) is set once at pipeline start via
|
||||
set_run_context() and automatically injected into all events.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -13,9 +16,33 @@ from datetime import datetime, timezone
|
||||
from detect.events import push_detect_event
|
||||
from detect.models import PipelineStats
|
||||
|
||||
# Module-level run context — set once per pipeline invocation
|
||||
_run_context: dict = {}
|
||||
|
||||
|
||||
def set_run_context(run_id: str = "", parent_job_id: str = "", run_type: str = "initial"):
|
||||
"""Set the run context for all subsequent events in this pipeline invocation."""
|
||||
global _run_context
|
||||
_run_context = {
|
||||
"run_id": run_id,
|
||||
"parent_job_id": parent_job_id,
|
||||
"run_type": run_type,
|
||||
}
|
||||
|
||||
|
||||
def clear_run_context():
|
||||
global _run_context
|
||||
_run_context = {}
|
||||
|
||||
|
||||
def _inject_context(payload: dict) -> dict:
|
||||
"""Add run context fields to an event payload."""
|
||||
if _run_context:
|
||||
payload.update(_run_context)
|
||||
return payload
|
||||
|
||||
|
||||
def log(job_id: str | None, stage: str, level: str, msg: str) -> None:
|
||||
"""Emit a log event."""
|
||||
if not job_id:
|
||||
return
|
||||
payload = {
|
||||
@@ -24,15 +51,17 @@ def log(job_id: str | None, stage: str, level: str, msg: str) -> None:
|
||||
"msg": msg,
|
||||
"ts": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
_inject_context(payload)
|
||||
push_detect_event(job_id, "log", payload)
|
||||
|
||||
|
||||
def stats(job_id: str | None, **kwargs) -> None:
|
||||
"""Emit a stats_update event. Pass only the fields that changed."""
|
||||
if not job_id:
|
||||
return
|
||||
s = PipelineStats(**kwargs)
|
||||
push_detect_event(job_id, "stats_update", dataclasses.asdict(s))
|
||||
payload = dataclasses.asdict(s)
|
||||
_inject_context(payload)
|
||||
push_detect_event(job_id, "stats_update", payload)
|
||||
|
||||
|
||||
def frame_update(
|
||||
@@ -42,7 +71,6 @@ def frame_update(
|
||||
jpeg_b64: str,
|
||||
boxes: list[dict],
|
||||
) -> None:
|
||||
"""Emit a frame_update event with the image and bounding boxes."""
|
||||
if not job_id:
|
||||
return
|
||||
payload = {
|
||||
@@ -51,14 +79,15 @@ def frame_update(
|
||||
"jpeg_b64": jpeg_b64,
|
||||
"boxes": boxes,
|
||||
}
|
||||
_inject_context(payload)
|
||||
push_detect_event(job_id, "frame_update", payload)
|
||||
|
||||
|
||||
def graph_update(job_id: str | None, nodes: list[dict]) -> None:
|
||||
"""Emit a graph_update event with node states."""
|
||||
if not job_id:
|
||||
return
|
||||
payload = {"nodes": nodes}
|
||||
_inject_context(payload)
|
||||
push_detect_event(job_id, "graph_update", payload)
|
||||
|
||||
|
||||
@@ -72,7 +101,6 @@ def detection(
|
||||
content_type: str = "",
|
||||
frame_ref: int | None = None,
|
||||
) -> None:
|
||||
"""Emit a brand detection event."""
|
||||
if not job_id:
|
||||
return
|
||||
payload = {
|
||||
@@ -84,12 +112,13 @@ def detection(
|
||||
"content_type": content_type,
|
||||
"frame_ref": frame_ref,
|
||||
}
|
||||
_inject_context(payload)
|
||||
push_detect_event(job_id, "detection", payload)
|
||||
|
||||
|
||||
def job_complete(job_id: str | None, report: dict) -> None:
|
||||
"""Emit a job_complete event with the final report."""
|
||||
if not job_id:
|
||||
return
|
||||
payload = {"job_id": job_id, "report": report}
|
||||
_inject_context(payload)
|
||||
push_detect_event(job_id, "job_complete", payload)
|
||||
|
||||
Reference in New Issue
Block a user