| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- """报告生成 API。"""
- from __future__ import annotations
- import uuid
- from typing import Any, Dict, List, Optional
- from fastapi import APIRouter, BackgroundTasks, HTTPException
- from pydantic import BaseModel, Field
- from app.report import generator, ollama_client
- router = APIRouter(prefix="/report", tags=["report"])
- JOBS: Dict[str, Dict[str, Any]] = {}
- class ReportRequest(BaseModel):
- windCode: str = Field(..., min_length=3, max_length=32)
- startMonth: str = Field(..., pattern=r"^\d{4}-\d{2}$")
- endMonth: str = Field(..., pattern=r"^\d{4}-\d{2}$")
- title: Optional[str] = ""
- sections: List[str] = ["overview", "health", "fault", "vibration"]
- @router.post("/generate")
- async def generate(request: ReportRequest, background_tasks: BackgroundTasks):
- generator.ensure_dirs()
- job_id = uuid.uuid4().hex[:12]
- JOBS[job_id] = {
- "job_id": job_id,
- "status": "queued",
- "progress": 0,
- "message": "任务已创建",
- "wind_code": request.windCode,
- "period": f"{request.startMonth} 至 {request.endMonth}",
- "created_at": __import__("datetime").datetime.now().isoformat(timespec="seconds"),
- }
- background_tasks.add_task(generator.run_job, job_id, request.dict(), JOBS)
- return {"job_id": job_id, "status": "queued"}
- @router.get("/status/{job_id}")
- async def status(job_id: str):
- job = JOBS.get(job_id)
- if not job:
- raise HTTPException(status_code=404, detail="任务不存在或已过期")
- return job
- @router.get("/list")
- async def list_reports(windCode: Optional[str] = None):
- return {"reports": generator.list_reports(windCode)}
- @router.get("/health")
- async def health():
- return {"ollama": ollama_client.health_check(), "model": ollama_client.OLLAMA_MODEL}
|