|
|
@@ -1,6 +1,6 @@
|
|
|
import os.path
|
|
|
|
|
|
-from fastapi import APIRouter, File, Body, HTTPException, status
|
|
|
+from fastapi import APIRouter, File, Body, HTTPException, status, Query
|
|
|
from fastapi.responses import FileResponse, JSONResponse
|
|
|
from ..core.config import settings
|
|
|
import datetime
|
|
|
@@ -188,7 +188,6 @@ async def update_scoring_config(new_config: dict = Body(...)):
|
|
|
json.dump(new_config, f, indent=2, ensure_ascii=False)
|
|
|
logger.info("配置备份结束")
|
|
|
|
|
|
-
|
|
|
return JSONResponse(
|
|
|
status_code=status.HTTP_200_OK,
|
|
|
content={"message": "配置已成功更新"}
|
|
|
@@ -198,4 +197,74 @@ async def update_scoring_config(new_config: dict = Body(...)):
|
|
|
raise HTTPException(
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
detail=f"保存新配置文件时发生错误: {e}"
|
|
|
- )
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+@router.get("/severity_names", summary="获取缺陷严重程度列表")
|
|
|
+async def get_severity_names(defect_label: str = Query(..., description="缺陷标签,如 wear, damaged, stain")):
|
|
|
+ """
|
|
|
+ 根据缺陷标签返回对应的严重程度列表。
|
|
|
+ """
|
|
|
+ if not settings.SCORE_CONFIG_PATH.exists():
|
|
|
+ raise HTTPException(status_code=404, detail="评分配置文件未找到")
|
|
|
+
|
|
|
+ try:
|
|
|
+ with open(settings.SCORE_CONFIG_PATH, 'r', encoding='utf-8') as f:
|
|
|
+ config = json.load(f)
|
|
|
+
|
|
|
+ severity_config = config.get("severity_level", {})
|
|
|
+
|
|
|
+ # 1. 定义映射关系:从 缺陷label 映射到 severity_level 的 key
|
|
|
+ # 逻辑依据:参考 score_service 中将 label 转换为 rule key 的逻辑,再简化为 severity key
|
|
|
+ # rule key -> severity key:
|
|
|
+ # wear_area -> wear
|
|
|
+ # loss_area -> loss
|
|
|
+ # scratch_length -> scratch
|
|
|
+ # pit_area -> pit
|
|
|
+ # stain_area -> stain
|
|
|
+
|
|
|
+ severity_key = None
|
|
|
+
|
|
|
+ # 磨损类
|
|
|
+ if defect_label in ['wear', 'wear_and_impact', 'wear_and_stain']:
|
|
|
+ severity_key = "wear"
|
|
|
+
|
|
|
+ elif defect_label in ['damaged']:
|
|
|
+ severity_key = "loss"
|
|
|
+ elif defect_label in ['impact']:
|
|
|
+ # 这是一个歧义点,取决于它是边角还是面。
|
|
|
+ # 既然是获取列表,通常 pit 和 loss 的等级名可能是一样的(轻微/一般/严重)。
|
|
|
+ # 这里优先映射为 pit (常见于面),或者你可以根据实际 json 结构调整
|
|
|
+ severity_key = "pit"
|
|
|
+ elif defect_label in ['scratch', 'scuff']:
|
|
|
+ severity_key = "scratch"
|
|
|
+ elif defect_label in ['pit', 'protrudent']:
|
|
|
+ severity_key = "pit"
|
|
|
+ elif defect_label in ['stain']:
|
|
|
+ severity_key = "stain"
|
|
|
+
|
|
|
+ if not severity_key:
|
|
|
+ # 如果没匹配到,尝试直接用 label 查找 (容错)
|
|
|
+ if defect_label in severity_config:
|
|
|
+ severity_key = defect_label
|
|
|
+ else:
|
|
|
+ # 默认返回 wear 或抛出异常,这里选择返回空列表表示未找到
|
|
|
+ return JSONResponse(content={"data": {"type": defect_label, "names": []}})
|
|
|
+
|
|
|
+ # 2. 获取对应的配置列表
|
|
|
+ levels = severity_config.get(severity_key, [])
|
|
|
+
|
|
|
+ # 3. 提取 name 列表
|
|
|
+ # 假设结构是 [{"name": "轻微", "value": 0.5}, ...]
|
|
|
+ names = [item.get("name") for item in levels]
|
|
|
+
|
|
|
+ return JSONResponse(content={
|
|
|
+ "data": {
|
|
|
+ "defect_label": severity_key,
|
|
|
+ "names": names
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"获取严重程度列表失败: {e}")
|
|
|
+ raise HTTPException(status_code=500, detail=f"服务器内部错误: {e}")
|