mirror of
https://github.com/azaion/detections.git
synced 2026-04-22 23:06:32 +00:00
[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:
@@ -10,6 +10,7 @@ Run with: pytest e2e/tests/test_streaming_video_upload.py -s -v
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -37,21 +38,23 @@ def _chunked_reader(path: str, chunk_size: int = 64 * 1024):
|
||||
|
||||
|
||||
def _start_sse_listener(
|
||||
http_client, media_id: str, auth_headers: dict
|
||||
http_client, channel_id: str, auth_headers: dict
|
||||
) -> tuple[list[dict], list[BaseException], threading.Event]:
|
||||
events: list[dict] = []
|
||||
errors: list[BaseException] = []
|
||||
first_event = threading.Event()
|
||||
connected = threading.Event()
|
||||
|
||||
def _listen():
|
||||
try:
|
||||
with http_client.get(
|
||||
f"/detect/{media_id}",
|
||||
f"/detect/events/{channel_id}",
|
||||
stream=True,
|
||||
timeout=_TIMEOUT + 2,
|
||||
headers=auth_headers,
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
connected.set()
|
||||
for event in sseclient.SSEClient(resp).events():
|
||||
if not event.data or not str(event.data).strip():
|
||||
continue
|
||||
@@ -62,9 +65,12 @@ def _start_sse_listener(
|
||||
except BaseException as exc:
|
||||
errors.append(exc)
|
||||
finally:
|
||||
connected.set()
|
||||
first_event.set()
|
||||
|
||||
threading.Thread(target=_listen, daemon=True).start()
|
||||
th = threading.Thread(target=_listen, daemon=True)
|
||||
th.start()
|
||||
connected.wait(timeout=3)
|
||||
return events, errors, first_event
|
||||
|
||||
|
||||
@@ -74,6 +80,8 @@ def test_streaming_video_detections_appear_during_upload(
|
||||
):
|
||||
# Arrange
|
||||
video_path = _fixture_path("video_test01.mp4")
|
||||
channel_id = str(uuid.uuid4())
|
||||
events, errors, first_event = _start_sse_listener(http_client, channel_id, auth_headers)
|
||||
|
||||
# Act
|
||||
r = http_client.post(
|
||||
@@ -81,14 +89,13 @@ def test_streaming_video_detections_appear_during_upload(
|
||||
data=_chunked_reader(video_path),
|
||||
headers={
|
||||
**auth_headers,
|
||||
"X-Channel-Id": channel_id,
|
||||
"X-Filename": "video_test01.mp4",
|
||||
"Content-Type": "application/octet-stream",
|
||||
},
|
||||
timeout=8,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
media_id = r.json()["mediaId"]
|
||||
events, errors, first_event = _start_sse_listener(http_client, media_id, auth_headers)
|
||||
assert r.status_code == 202
|
||||
first_event.wait(timeout=_TIMEOUT)
|
||||
|
||||
# Assert
|
||||
@@ -103,6 +110,8 @@ def test_streaming_video_detections_appear_during_upload(
|
||||
def test_non_faststart_video_still_works(warm_engine, http_client, auth_headers):
|
||||
# Arrange
|
||||
video_path = _fixture_path("video_test01.mp4")
|
||||
channel_id = str(uuid.uuid4())
|
||||
events, errors, first_event = _start_sse_listener(http_client, channel_id, auth_headers)
|
||||
|
||||
# Act
|
||||
r = http_client.post(
|
||||
@@ -110,14 +119,13 @@ def test_non_faststart_video_still_works(warm_engine, http_client, auth_headers)
|
||||
data=_chunked_reader(video_path),
|
||||
headers={
|
||||
**auth_headers,
|
||||
"X-Channel-Id": channel_id,
|
||||
"X-Filename": "video_test01_plain.mp4",
|
||||
"Content-Type": "application/octet-stream",
|
||||
},
|
||||
timeout=8,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
media_id = r.json()["mediaId"]
|
||||
events, errors, first_event = _start_sse_listener(http_client, media_id, auth_headers)
|
||||
assert r.status_code == 202
|
||||
first_event.wait(timeout=_TIMEOUT)
|
||||
|
||||
# Assert
|
||||
|
||||
Reference in New Issue
Block a user