feat(sync): add SSE live feed, unified logs page, fix Oracle connection
- Add SSE event bus in sync_service (subscribe/unsubscribe/_emit) - Add GET /api/sync/stream SSE endpoint for real-time sync progress - Rewrite logs.html: unified runs table + live feed + summary + filters - Rewrite logs.js: SSE EventSource client, run selection, pagination - Dashboard: clickable runs navigate to /logs?run=, sync started banner - Remove "Import Comenzi" nav item, delete sync_detail.html - Add error_message column to sync_runs table with migration - Fix: export TNS_ADMIN as OS env var so oracledb finds tnsnames.ora - Fix: use get_oracle_connection() instead of direct pool.acquire() - Fix: CRM_POLITICI_PRET_ART INSERT to match actual table schema Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from fastapi import APIRouter, Request, BackgroundTasks
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.responses import HTMLResponse
|
||||
from starlette.responses import StreamingResponse
|
||||
from pydantic import BaseModel
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -16,27 +20,46 @@ class ScheduleConfig(BaseModel):
|
||||
interval_minutes: int = 5
|
||||
|
||||
|
||||
# HTML pages
|
||||
@router.get("/sync", response_class=HTMLResponse)
|
||||
async def sync_page(request: Request):
|
||||
return templates.TemplateResponse("dashboard.html", {"request": request})
|
||||
# SSE streaming endpoint
|
||||
@router.get("/api/sync/stream")
|
||||
async def sync_stream(request: Request):
|
||||
"""SSE stream for real-time sync progress."""
|
||||
q = sync_service.subscribe()
|
||||
|
||||
async def event_generator():
|
||||
try:
|
||||
while True:
|
||||
# Check if client disconnected
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
try:
|
||||
event = await asyncio.wait_for(q.get(), timeout=15.0)
|
||||
yield f"data: {json.dumps(event)}\n\n"
|
||||
if event.get("type") in ("completed", "failed"):
|
||||
break
|
||||
except asyncio.TimeoutError:
|
||||
yield f"data: {json.dumps({'type': 'keepalive'})}\n\n"
|
||||
finally:
|
||||
sync_service.unsubscribe(q)
|
||||
|
||||
@router.get("/sync/run/{run_id}", response_class=HTMLResponse)
|
||||
async def sync_detail_page(request: Request, run_id: str):
|
||||
return templates.TemplateResponse("sync_detail.html", {"request": request, "run_id": run_id})
|
||||
return StreamingResponse(
|
||||
event_generator(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}
|
||||
)
|
||||
|
||||
|
||||
# API endpoints
|
||||
@router.post("/api/sync/start")
|
||||
async def start_sync(background_tasks: BackgroundTasks):
|
||||
"""Trigger a sync run in the background."""
|
||||
status = await sync_service.get_sync_status()
|
||||
if status.get("status") == "running":
|
||||
return {"error": "Sync already running", "run_id": status.get("run_id")}
|
||||
result = await sync_service.prepare_sync()
|
||||
if result.get("error"):
|
||||
return {"error": result["error"], "run_id": result.get("run_id")}
|
||||
|
||||
background_tasks.add_task(sync_service.run_sync)
|
||||
return {"message": "Sync started"}
|
||||
run_id = result["run_id"]
|
||||
background_tasks.add_task(sync_service.run_sync, run_id=run_id)
|
||||
return {"message": "Sync started", "run_id": run_id}
|
||||
|
||||
|
||||
@router.post("/api/sync/stop")
|
||||
@@ -61,8 +84,8 @@ async def sync_history(page: int = 1, per_page: int = 20):
|
||||
|
||||
|
||||
@router.get("/logs", response_class=HTMLResponse)
|
||||
async def logs_page(request: Request):
|
||||
return templates.TemplateResponse("logs.html", {"request": request})
|
||||
async def logs_page(request: Request, run: str = None):
|
||||
return templates.TemplateResponse("logs.html", {"request": request, "selected_run": run or ""})
|
||||
|
||||
|
||||
@router.get("/api/sync/run/{run_id}")
|
||||
|
||||
Reference in New Issue
Block a user