From 4562aa6bdcecb4fbcd80ff4749d1f11a49825d37 Mon Sep 17 00:00:00 2001 From: kx_guo Date: Tue, 8 Sep 2026 18:27:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=A3=8E=E9=99=A9=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E6=8E=A5=E5=8F=A3=E5=B9=B6=E8=A1=A5=E9=BD=90=E6=A1=88?= =?UTF-8?q?=E4=BE=8B=E7=BB=93=E6=9E=9C=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/router.py | 2 ++ app/api/v1/monitoring.py | 28 ++++++++++++++++++++++++++++ app/schemas/risk_prediction.py | 1 + app/services/rag.py | 4 +++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 app/api/v1/monitoring.py diff --git a/app/api/router.py b/app/api/router.py index 9e2c9d8..05b141a 100644 --- a/app/api/router.py +++ b/app/api/router.py @@ -7,6 +7,7 @@ from app.api.v1.conversations import router as conversations_router from app.api.v1.dashboard import router as dashboard_router from app.api.v1.health import router as health_router from app.api.v1.model_config import router as model_config_router +from app.api.v1.monitoring import router as monitoring_router from app.api.v1.ocr import router as ocr_router from app.api.v1.reports import router as reports_router from app.api.v1.risk_items import router as risk_items_router @@ -17,6 +18,7 @@ from app.api.v1.suppliers import router as suppliers_router api_router = APIRouter(prefix="/api/v1") api_router.include_router(health_router) api_router.include_router(model_config_router) +api_router.include_router(monitoring_router) api_router.include_router(ocr_router) api_router.include_router(auth_router) api_router.include_router(conversations_router) diff --git a/app/api/v1/monitoring.py b/app/api/v1/monitoring.py new file mode 100644 index 0000000..e47151b --- /dev/null +++ b/app/api/v1/monitoring.py @@ -0,0 +1,28 @@ +from typing import Annotated + +from fastapi import APIRouter, Depends + +from app.api.dependencies import DatabaseSession, require_permission +from app.models.entities import OperationAudit, UserAccount +from app.schemas.common import ApiResponse + +router = APIRouter(prefix="/monitoring", tags=["monitoring"]) + + +@router.post("/risk") +async def monitor_risk( + payload: dict, + session: DatabaseSession, + current_user: Annotated[UserAccount, Depends(require_permission("risk.assess", operate=True))], +) -> ApiResponse[dict]: + session.add( + OperationAudit( + actor_user_id=current_user.id, + actor_emp_no=current_user.emp_no, + action="risk.monitor.add", + resource_type="risk_assessment", + resource_id=str(payload.get("subject_id") or ""), + audit_metadata=payload, + ) + ) + return ApiResponse(data={"monitored": True, "subject_id": payload.get("subject_id")}) diff --git a/app/schemas/risk_prediction.py b/app/schemas/risk_prediction.py index 4cf102f..6926af1 100644 --- a/app/schemas/risk_prediction.py +++ b/app/schemas/risk_prediction.py @@ -79,6 +79,7 @@ class SimilarCase(ApiSchema): description: str | None = None root_cause: str | None = None defect_level: str | None = None + result: str | None = None problem: str | None = None solution: str | None = None similarity: float | None = None diff --git a/app/services/rag.py b/app/services/rag.py index 5e50a1b..29c70cc 100644 --- a/app/services/rag.py +++ b/app/services/rag.py @@ -123,6 +123,7 @@ class AnomalyCaseRAG: AnomalyCase.solution, AnomalyCase.root_cause, AnomalyCase.defect_level, + AnomalyCase.result, ) vector_ready = await self._vector_column_ready() has_vectors = await self._has_stored_vectors() if vector_ready else False @@ -135,7 +136,7 @@ class AnomalyCaseRAG: """ SELECT id, date, product_name, product_category, material_type, supplier, factory, stage_new, anomaly_type, description, solution, root_cause, - defect_level, 1 - (embedding_vector <=> CAST(:embedding AS halfvec)) AS vector_score + defect_level, result, 1 - (embedding_vector <=> CAST(:embedding AS halfvec)) AS vector_score FROM anomaly_cases WHERE embedding_vector IS NOT NULL ORDER BY embedding_vector <=> CAST(:embedding AS halfvec) @@ -306,6 +307,7 @@ class AnomalyCaseRAG: description=row.get("description"), root_cause=row.get("root_cause"), defect_level=row.get("defect_level"), + result=row.get("result"), problem=(row.get("anomaly_type") or row.get("description") or "历史异常未填写"), solution=(row.get("solution") or row.get("root_cause") or "请查看原始案例记录"), )