PMFlow/backend/app/api/model_capability_api/routes_match_cost.py

132 lines
4.4 KiB
Python

from __future__ import annotations
from .shared import *
from .helpers import _build_service
@router.post("/validate-match")
async def validate_model_requirement_match(
request: ValidateMatchRequest,
current_user: User = Depends(get_current_user),
tenant_id: int = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
"""需求-模型匹配校验"""
_ = current_user
service = _build_service(db)
try:
data = await service.validate_model_requirement_match(
truth_node_id=request.truth_node_id,
model_version_id=request.model_version_id,
tenant_id=tenant_id,
project_id=request.project_id,
)
return success_response(data=data)
except ValueError as exc:
return JSONResponse(
status_code=404,
content=error_response(ErrorCode.NOT_FOUND, str(exc)),
)
except Exception as exc:
return JSONResponse(
status_code=500,
content=error_response(ErrorCode.INTERNAL_ERROR, f"匹配校验失败: {exc}"),
)
@router.get("/capability-gaps")
async def list_capability_gaps(
status: Optional[str] = Query(default=None),
severity: Optional[str] = Query(default=None),
limit: int = Query(default=100, ge=1, le=500),
current_user: User = Depends(get_current_user),
tenant_id: int = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
"""列出能力差距预警"""
_ = current_user
service = _build_service(db)
try:
data = await service.list_capability_gaps(
tenant_id=tenant_id,
status=status,
severity=severity,
limit=limit,
)
return success_response(data=data)
except Exception as exc:
return JSONResponse(
status_code=500,
content=error_response(ErrorCode.INTERNAL_ERROR, f"查询能力差距失败: {exc}"),
)
@router.get("/cost-tracking/{truth_node_id}")
async def get_cost_tracking(
truth_node_id: int,
period: str = Query(default="weekly", pattern="^(daily|weekly|monthly|project)$"),
current_user: User = Depends(get_current_user),
tenant_id: int = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
"""查询成本跟踪与 ROI"""
_ = current_user
service = _build_service(db)
try:
data = await service.get_cost_tracking(
truth_node_id=truth_node_id,
period=period,
tenant_id=tenant_id,
)
return success_response(data=data)
except Exception as exc:
return JSONResponse(
status_code=500,
content=error_response(ErrorCode.INTERNAL_ERROR, f"查询成本跟踪失败: {exc}"),
)
@router.get("/cost-optimization/{truth_node_id}")
async def get_cost_optimization(
truth_node_id: int,
period: str = Query(default="weekly", pattern="^(daily|weekly|monthly|project)$"),
current_user: User = Depends(get_current_user),
tenant_id: int = Depends(get_tenant_id),
db: AsyncSession = Depends(get_db),
):
"""查询成本优化建议(必要时自动触发一次计算)"""
_ = current_user
service = _build_service(db)
try:
tracking = await service.get_cost_tracking(
truth_node_id=truth_node_id,
period=period,
tenant_id=tenant_id,
)
optimization = tracking.get("optimization_suggestions") or {}
if not optimization:
computed = await service.track_cost_and_roi(
truth_node_id=truth_node_id,
period=period,
tenant_id=tenant_id,
)
optimization = computed.get("optimization_suggestions", {})
tracking = await service.get_cost_tracking(
truth_node_id=truth_node_id,
period=period,
tenant_id=tenant_id,
)
return success_response(
data={
"truth_node_id": truth_node_id,
"period": period,
"optimization_suggestions": optimization,
"roi_score": tracking.get("roi_score"),
"actual_cost_usd": tracking.get("actual_cost_usd", 0.0),
}
)
except Exception as exc:
return JSONResponse(
status_code=500,
content=error_response(ErrorCode.INTERNAL_ERROR, f"查询成本优化失败: {exc}"),
)