fix(alert-rules): dynamic-thresholds 迭代 Column 当行取值致 500

GET /api/cma/alert-rules/dynamic-thresholds 在 {c.name: getattr(c, c.name) for c in
DynamicThresholdCache.__table__.columns} 里把 Column 对象当 ORM 行取属性,触发
AttributeError: 'Comparator' object has no attribute 'id'。改为列名取自表元数据、
值取自 query 结果行;entity_id 过滤与 limit 50/kpi_id 分支不变。

(cherry picked from commit 4130726be411a74a3318d9dc6bbc518a6c60d0ff)
This commit is contained in:
fullstack
2026-10-18 16:31:52 +08:00
committed by Hermes CI Fix
parent a9dcbf22bc
commit 60b6ef3bfe
+4 -2
View File
@@ -420,8 +420,10 @@ def get_dynamic_thresholds(kpi_id: Optional[int] = None, db: Session = Depends(g
query = db.query(DynamicThresholdCache).filter(DynamicThresholdCache.entity_id == entity_id)
if kpi_id:
query = query.filter(DynamicThresholdCache.kpi_id == kpi_id)
cache = query.order_by(DynamicThresholdCache.id.desc()).limit(50).all()
return {"data": [{c.name: getattr(c, c.name) for c in DynamicThresholdCache.__table__.columns} for c in cache]}
rows = query.order_by(DynamicThresholdCache.id.desc()).limit(50).all()
# 列名取自表元数据,值取自 ORM 行(原实现把 Column 对象当行取值,AttributeError: 'Comparator' object has no attribute 'id')
columns = [col.name for col in DynamicThresholdCache.__table__.columns]
return {"data": [{name: getattr(row, name) for name in columns} for row in rows]}
@router.post("/calculate-dynamic")