This commit is contained in:
Your Name
2026-04-17 14:07:50 +08:00
parent dc34a4e294
commit 1921318c25
11 changed files with 2415 additions and 19 deletions

View File

@@ -7,6 +7,7 @@ from uuid import uuid4
from app.core.config import settings
from app.core.db import get_db
from app.services.debug_event_service import push_debug_event
ACTIVE_JOB_STATUSES = ("pending", "running")
@@ -222,6 +223,34 @@ def list_detect_jobs(limit: int = 20) -> list[dict]:
return [_fetch_job_summary(cur, row, event_limit=10) for row in rows]
def list_recent_detect_run_events(limit: int = 20) -> list[dict]:
safe_limit = max(1, min(int(limit or 20), 200))
with get_db() as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT job_id, node_code, event_type, level, message, payload_json, created_at
FROM detect_run_events
ORDER BY created_at DESC, id DESC
LIMIT %s
""",
(safe_limit,),
)
rows = cur.fetchall()
return [
{
"job_id": int(row[0]) if row[0] is not None else None,
"node_code": row[1] or "",
"event_type": row[2] or "",
"level": row[3] or "info",
"message": row[4] or "",
"payload": _decode_payload(row[5]),
"created_at": _format_time(row[6]),
}
for row in rows
]
def get_detect_queue_health(window_minutes: int = 15) -> dict:
window_minutes = max(5, min(int(window_minutes or 15), 120))
active_job = get_active_detect_job_summary(event_limit=10)
@@ -498,6 +527,20 @@ def append_detect_job_event(
),
)
conn.commit()
try:
push_debug_event(
service="detect-job",
event_type=str(event_type or "").strip() or "info",
level=str(level or "info").strip() or "info",
message=str(message or "").strip(),
payload={
"job_id": int(job_id),
"node_code": node_code or settings.node_code,
**(payload or {}),
},
)
except Exception:
pass
def create_detect_job_if_needed(limit: int = 1000, created_by: str = "system") -> dict | None: