2026-06-15 15:53:57 +08:00
|
|
|
"""Routes for triggering evaluations and polling background task status."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-06-23 10:35:00 +08:00
|
|
|
import logging
|
|
|
|
|
|
2026-06-15 15:53:57 +08:00
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
|
|
|
|
|
|
from webapp.models import (
|
|
|
|
|
TaskStatus,
|
|
|
|
|
TriggerEvaluationRequest,
|
|
|
|
|
TriggerEvaluationResponse,
|
|
|
|
|
)
|
|
|
|
|
from webapp.services import scenario_scanner
|
|
|
|
|
from webapp.services.task_manager import task_manager
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/evaluations", tags=["evaluations"])
|
2026-06-23 10:35:00 +08:00
|
|
|
logger = logging.getLogger("webapp.api.evaluations")
|
2026-06-15 15:53:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", response_model=TriggerEvaluationResponse)
|
|
|
|
|
def trigger_evaluation(request: TriggerEvaluationRequest) -> TriggerEvaluationResponse:
|
|
|
|
|
"""Validate the scenario path and queue a background evaluation task."""
|
2026-06-23 10:35:00 +08:00
|
|
|
logger.info("[trigger] scenario=%s", request.scenario_path)
|
2026-06-15 15:53:57 +08:00
|
|
|
resolved = scenario_scanner.resolve_scenario_path(request.scenario_path)
|
|
|
|
|
if resolved is None:
|
2026-06-23 10:35:00 +08:00
|
|
|
logger.warning("[trigger] invalid scenario path: %s", request.scenario_path)
|
2026-06-15 15:53:57 +08:00
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=400,
|
|
|
|
|
detail=f"无效或不允许的场景路径: {request.scenario_path}",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
task_id = task_manager.submit(request.scenario_path)
|
2026-06-23 10:35:00 +08:00
|
|
|
logger.info("[trigger] queued task_id=%s scenario=%s", task_id, request.scenario_path)
|
2026-06-15 15:53:57 +08:00
|
|
|
return TriggerEvaluationResponse(task_id=task_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{task_id}", response_model=TaskStatus)
|
|
|
|
|
def get_task_status(task_id: str) -> TaskStatus:
|
|
|
|
|
"""Return the current status and logs for one evaluation task."""
|
|
|
|
|
status = task_manager.get(task_id)
|
|
|
|
|
if status is None:
|
2026-06-23 10:35:00 +08:00
|
|
|
logger.warning("[task_status] not found task_id=%s", task_id)
|
2026-06-15 15:53:57 +08:00
|
|
|
raise HTTPException(status_code=404, detail=f"未找到任务: {task_id}")
|
2026-06-23 10:35:00 +08:00
|
|
|
logger.debug("[task_status] task_id=%s status=%s", task_id, status.status)
|
2026-06-15 15:53:57 +08:00
|
|
|
return status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("", response_model=dict)
|
|
|
|
|
def list_tasks() -> dict[str, list]:
|
|
|
|
|
"""Return all known evaluation tasks for this server session."""
|
2026-06-23 10:35:00 +08:00
|
|
|
tasks = task_manager.list_tasks()
|
|
|
|
|
logger.info("[list_tasks] count=%d", len(tasks))
|
|
|
|
|
return {"tasks": [task.model_dump() for task in tasks]}
|