Add HTML5 operations SSE updates
This commit is contained in:
@@ -1653,6 +1653,24 @@ async def html5_operation_summary() -> Response:
|
||||
)
|
||||
|
||||
|
||||
@app.get("/html5/operations/events")
|
||||
async def html5_operations_events(once: bool = False) -> StreamingResponse:
|
||||
def stream_operations():
|
||||
while True:
|
||||
jobs = _html5_operation_jobs()
|
||||
yield _html5_sse_event("operations-summary", render_html5_operation_summary(jobs))
|
||||
yield _html5_sse_event("operations-jobs", render_html5_operation_rows(jobs))
|
||||
if once:
|
||||
break
|
||||
time.sleep(3)
|
||||
|
||||
return StreamingResponse(
|
||||
stream_operations(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@app.get("/html5/projects/{project_id}/editor")
|
||||
async def html5_project_editor(project_id: str, q: str = "") -> Response:
|
||||
try:
|
||||
@@ -1683,7 +1701,7 @@ async def html5_project_events(project_id: str, once: bool = False) -> Streaming
|
||||
fragment = render_html5_status(project_id, snapshot)
|
||||
except HTTPException as error:
|
||||
fragment = f'<span>project: {project_id}</span><span>error: {error.detail}</span>'
|
||||
yield f"event: status\ndata: {fragment}\n\n"
|
||||
yield _html5_sse_event("status", fragment)
|
||||
if once:
|
||||
break
|
||||
time.sleep(5)
|
||||
@@ -8357,6 +8375,11 @@ def _html5_operation_jobs() -> list[OperationJob]:
|
||||
return sorted(_operations.jobs.values(), key=lambda job: job.updated_at, reverse=True)[:50]
|
||||
|
||||
|
||||
def _html5_sse_event(event: str, fragment: str) -> str:
|
||||
data = "\n".join(f"data: {line}" for line in fragment.splitlines())
|
||||
return f"event: {event}\n{data}\n\n"
|
||||
|
||||
|
||||
def _project_summaries() -> list[ProjectSummaryResponse]:
|
||||
project_ids = set(_project_setup.keys())
|
||||
stored_snapshots = _storage.list_snapshot_refs()
|
||||
|
||||
Reference in New Issue
Block a user