修复:告警状态流转统一 — 前端处理直接结单(CLOSED),支持处理图片上传
1. handle接口状态映射:handled→CLOSED(而非CONFIRMED),ignored→FALSE 2. handle_status同步设置:handled→DONE,ignored→IGNORED 3. 新增handle_image_url字段支持处理图片存储 4. 自动迁移:启动时自动ALTER TABLE添加新列
This commit is contained in:
@@ -281,6 +281,7 @@ class AlarmEvent(Base):
|
||||
area_id = Column(BigInteger, comment="所属区域ID")
|
||||
handler = Column(String(64), comment="处理人")
|
||||
handle_remark = Column(Text, comment="处理备注")
|
||||
handle_image_url = Column(String(512), comment="处理图片URL")
|
||||
handled_at = Column(DateTime, comment="处理时间")
|
||||
created_at = Column(DateTime, default=lambda: beijing_now())
|
||||
updated_at = Column(DateTime, default=lambda: beijing_now(),
|
||||
@@ -312,6 +313,7 @@ class AlarmEvent(Base):
|
||||
"area_id": self.area_id,
|
||||
"handler": self.handler,
|
||||
"handle_remark": self.handle_remark,
|
||||
"handle_image_url": self.handle_image_url,
|
||||
"handled_at": self.handled_at.strftime('%Y-%m-%d %H:%M:%S') if self.handled_at else None,
|
||||
"created_at": self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
|
||||
"updated_at": self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None,
|
||||
@@ -535,9 +537,27 @@ def get_session():
|
||||
return _SessionLocal()
|
||||
|
||||
|
||||
def _auto_migrate(engine):
|
||||
"""自动添加缺失的列(create_all 不会 ALTER 已有表)"""
|
||||
from sqlalchemy import inspect, text
|
||||
insp = inspect(engine)
|
||||
migrations = [
|
||||
("alarm_event", "handle_image_url", "VARCHAR(512) COMMENT '处理图片URL'"),
|
||||
]
|
||||
with engine.connect() as conn:
|
||||
for table, column, col_def in migrations:
|
||||
if table in insp.get_table_names():
|
||||
existing = [c["name"] for c in insp.get_columns(table)]
|
||||
if column not in existing:
|
||||
conn.execute(text(f"ALTER TABLE {table} ADD COLUMN {column} {col_def}"))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def init_db():
|
||||
engine = get_engine()
|
||||
Base.metadata.create_all(bind=engine)
|
||||
# 自动添加新列(ALTER TABLE 不会被 create_all 处理)
|
||||
_auto_migrate(engine)
|
||||
|
||||
|
||||
class NotifyArea(Base):
|
||||
|
||||
@@ -101,6 +101,7 @@ async def _alarm_to_camel(alarm_dict: dict, camera_info_map: dict = None, camera
|
||||
"edgeNodeId": alarm_dict.get("edge_node_id"),
|
||||
"handler": alarm_dict.get("handler"),
|
||||
"handleRemark": alarm_dict.get("handle_remark"),
|
||||
"handleImageUrl": alarm_dict.get("handle_image_url"),
|
||||
"handledAt": alarm_dict.get("handled_at"),
|
||||
"createdAt": alarm_dict.get("created_at"),
|
||||
"updatedAt": alarm_dict.get("updated_at"),
|
||||
@@ -219,6 +220,7 @@ async def handle_alert(
|
||||
handleStatus: Optional[str] = Query(None, description="处理状态: HANDLING/DONE/IGNORED"),
|
||||
status: Optional[str] = Query(None, description="处理状态(兼容旧接口)"),
|
||||
remark: Optional[str] = Query(None, description="处理备注"),
|
||||
handleImageUrl: Optional[str] = Query(None, description="处理图片URL"),
|
||||
service: AlarmEventService = Depends(get_alarm_event_service),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
@@ -228,13 +230,13 @@ async def handle_alert(
|
||||
raise HTTPException(status_code=400, detail="缺少 alarmId 或 id 参数")
|
||||
handler = current_user.get("username", "admin")
|
||||
|
||||
# 兼容旧接口: status=handled → alarmStatus=CONFIRMED, status=ignored → alarmStatus=FALSE
|
||||
# 兼容旧接口: status=handled → alarmStatus=CLOSED, status=ignored → alarmStatus=FALSE
|
||||
if not alarmStatus and status:
|
||||
status_convert = {"handled": "CONFIRMED", "ignored": "FALSE", "resolved": "CLOSED"}
|
||||
status_convert = {"handled": "CLOSED", "ignored": "FALSE", "resolved": "CLOSED"}
|
||||
handle_status_convert = {"handled": "DONE", "ignored": "IGNORED", "resolved": "DONE"}
|
||||
alarmStatus = status_convert.get(status, status.upper())
|
||||
# 忽略操作设置 handle_status=IGNORED,区别于自动结单的 DONE
|
||||
if status == "ignored" and not handleStatus:
|
||||
handleStatus = "IGNORED"
|
||||
if not handleStatus:
|
||||
handleStatus = handle_status_convert.get(status, "")
|
||||
|
||||
alarm = service.handle_alarm(
|
||||
alarm_id=alarm_id,
|
||||
@@ -242,6 +244,7 @@ async def handle_alert(
|
||||
handle_status=handleStatus,
|
||||
remark=remark,
|
||||
handler=handler,
|
||||
handle_image_url=handleImageUrl,
|
||||
)
|
||||
if not alarm:
|
||||
raise HTTPException(status_code=404, detail="告警不存在")
|
||||
|
||||
@@ -427,6 +427,7 @@ class AlarmEventService:
|
||||
handle_status: Optional[str] = None,
|
||||
remark: Optional[str] = None,
|
||||
handler: Optional[str] = None,
|
||||
handle_image_url: Optional[str] = None,
|
||||
) -> Optional[AlarmEvent]:
|
||||
"""处理告警"""
|
||||
db = get_session()
|
||||
@@ -443,6 +444,8 @@ class AlarmEventService:
|
||||
alarm.handle_remark = remark
|
||||
if handler:
|
||||
alarm.handler = handler
|
||||
if handle_image_url:
|
||||
alarm.handle_image_url = handle_image_url
|
||||
|
||||
now = beijing_now()
|
||||
alarm.handled_at = now
|
||||
|
||||
Reference in New Issue
Block a user