feature: /api/v1/runs/{run_id}/export 按照需求导出测试结果

This commit is contained in:
baozaotumao2025
2026-07-18 23:49:41 +08:00
parent c57ec67fe3
commit e980f1a349
8 changed files with 245 additions and 106 deletions
+38 -1
View File
@@ -1,7 +1,7 @@
import json
import logging
from hashlib import sha256
from typing import Annotated
from typing import Annotated, Literal
from fastapi import APIRouter, BackgroundTasks, Depends, Header, Path, Query, Response, status
@@ -27,6 +27,7 @@ from app.schemas.api import (
)
from app.services.capability_service import CapabilityProbeService
from app.services.dataset_service import DatasetGateway, TestPlanService
from app.services.export_service import matches_result, render_markdown
from app.services.test_execution_service import TestExecutionService
router = APIRouter()
@@ -519,3 +520,39 @@ def get_run_results(
)
for row in rows
]
@router.get(
"/runs/{run_id}/export",
summary="导出测试运行详细报告",
description="将终态 Run 的逐条结果导出为 Markdown;默认导出全部,可按执行状态和评价结果筛选。",
responses={
200: {"content": {"text/markdown": {}}, "description": "Markdown 详细报告附件。"},
404: {"model": ErrorResponse},
409: {"model": ErrorResponse},
},
)
def export_run(
run_id: int = Path(description="要导出的测试运行 ID。", gt=0),
execution_status: Literal["completed", "error"] | None = Query(default=None),
verdict: Literal["pass", "fail", "needs_human_review", "judge_format_error", "none"]
| None = Query(default=None),
db: Session = Depends(get_database),
settings: Settings = Depends(get_settings),
) -> Response:
run = TestRunRepository(db).get(run_id)
if not run:
raise NotFoundError("测试运行不存在")
if run.status not in TERMINAL_STATUSES:
raise ConflictError("只能导出已结束的测试运行")
results = [
item.model_dump()
for item in get_run_results(run_id, db, settings)
if matches_result(item.model_dump(), execution_status, verdict)
]
content = render_markdown(_run_detail(run).model_dump(mode="json"), results)
return Response(
content=content,
media_type="text/markdown",
headers={"Content-Disposition": f'attachment; filename="run_{run_id}_results.md"'},
)