[AZ-180] Refactor detection event handling and improve SSE support

- Updated the detection image endpoint to require a channel ID for event streaming.
- Introduced a new endpoint for streaming detection events, allowing clients to receive real-time updates.
- Enhanced the internal buffering mechanism for detection events to manage multiple channels.
- Refactored the inference module to support the new event handling structure.

Made-with: Cursor
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-04-03 02:42:05 +03:00
parent 2c35e59a77
commit 8baa96978b
26 changed files with 819 additions and 413 deletions
+82 -9
View File
@@ -1,6 +1,10 @@
import json
import threading
import time
import uuid
import pytest
import sseclient
_DETECT_TIMEOUT = 60
@@ -39,11 +43,44 @@ class TestHealthEngineStep02LazyInit:
f"engine already initialized (aiAvailability={before['aiAvailability']}); "
"lazy-init test must run before any test that triggers warm_engine"
)
cid = str(uuid.uuid4())
headers = {**auth_headers, "X-Channel-Id": cid}
done = threading.Event()
connected = threading.Event()
def _listen():
try:
with http_client.get(
f"/detect/events/{cid}",
stream=True,
timeout=_DETECT_TIMEOUT + 2,
headers=auth_headers,
) as resp:
resp.raise_for_status()
connected.set()
for ev in sseclient.SSEClient(resp).events():
if not ev.data or not str(ev.data).strip():
continue
data = json.loads(ev.data)
if data.get("mediaStatus") in ("AIProcessed", "Error"):
break
except Exception:
pass
finally:
connected.set()
done.set()
th = threading.Thread(target=_listen, daemon=True)
th.start()
connected.wait(timeout=5)
files = {"file": ("lazy.jpg", image_small, "image/jpeg")}
r = http_client.post("/detect/image", files=files, headers=auth_headers, timeout=_DETECT_TIMEOUT)
r.raise_for_status()
body = r.json()
assert isinstance(body, list)
r = http_client.post("/detect/image", files=files, headers=headers, timeout=_DETECT_TIMEOUT)
assert r.status_code == 202
done.wait(timeout=_DETECT_TIMEOUT)
th.join(timeout=2)
after = _get_health(http_client)
_assert_active_ai(after)
@@ -61,13 +98,49 @@ class TestHealthEngineStep03Warmed:
assert data.get("errorMessage") is None
def test_ft_p_15_onnx_cpu_detect(self, http_client, image_small, auth_headers):
cid = str(uuid.uuid4())
headers = {**auth_headers, "X-Channel-Id": cid}
all_detections = []
done = threading.Event()
connected = threading.Event()
def _listen():
try:
with http_client.get(
f"/detect/events/{cid}",
stream=True,
timeout=_DETECT_TIMEOUT + 2,
headers=auth_headers,
) as resp:
resp.raise_for_status()
connected.set()
for ev in sseclient.SSEClient(resp).events():
if not ev.data or not str(ev.data).strip():
continue
data = json.loads(ev.data)
if data.get("mediaStatus") == "AIProcessing":
all_detections.extend(data.get("annotations", []))
if data.get("mediaStatus") in ("AIProcessed", "Error"):
break
except Exception:
pass
finally:
connected.set()
done.set()
th = threading.Thread(target=_listen, daemon=True)
th.start()
connected.wait(timeout=5)
files = {"file": ("onnx.jpg", image_small, "image/jpeg")}
r = http_client.post("/detect/image", files=files, headers=auth_headers, timeout=_DETECT_TIMEOUT)
r = http_client.post("/detect/image", files=files, headers=headers, timeout=_DETECT_TIMEOUT)
r.raise_for_status()
body = r.json()
assert isinstance(body, list)
if body:
d = body[0]
assert r.status_code == 202
done.wait(timeout=_DETECT_TIMEOUT)
th.join(timeout=2)
if all_detections:
d = all_detections[0]
for k in (
"centerX",
"centerY",