modify: aibox_sophon_adapter告警转发

This commit is contained in:
2026-08-05 11:51:29 +08:00
parent 3663a3de83
commit 39b5ddd585
3 changed files with 86 additions and 20 deletions
+60 -20
View File
@@ -14,6 +14,7 @@ import hashlib
import logging
import aiofiles
import httpx
from fastapi import APIRouter, UploadFile, File, Form, Request, Body, HTTPException
from fastapi.responses import FileResponse
@@ -186,26 +187,33 @@ async def alarm_report(
# 获取完整报文(含 extra 字段,排除 None 值以生成整洁 JSON)
full_data = body.model_dump(exclude_none=True)
async with Client(session=zenoh_session) as client:
try:
result = await client.call(
worker_type="doubao",
subject="doubao.analyze",
payload={
"image_base64": full_data.get("ImageData", ""),
"prompt": full_data.get("Summary", ""),
},
timeout=30.0,
retries=1
)
analysis = result.get("result", "")
error = result.get("error", "")
if error:
logger.warning(f"Doubao 分析失败: {error}")
else:
logger.info(f"Doubao 分析结果: {analysis}")
except Exception as e:
logger.error(f"Doubao 分析调用失败: {type(e).__name__}: {e}")
# 豆包 AI 分析(可通过配置开关关闭)
from .global_var import doubao_analysis_enabled
if doubao_analysis_enabled:
async with Client(session=zenoh_session) as client:
try:
result = await client.call(
worker_type="doubao",
subject="doubao.analyze",
payload={
"image_base64": full_data.get("ImageData", ""),
"prompt": f"判断是否存在{full_data.get("Summary", "")},直接返回True或False,不要输出其他内容",
},
timeout=30.0,
retries=1
)
analysis = result.get("result", "")
error = result.get("error", "")
if error:
logger.warning(f"Doubao 分析失败: {error}")
if analysis == "False":
return AlarmReportResponse(
Result=AlarmReportResult(Code=0, Desc="Doubao 分析判定为 False,忽略告警")
)
except Exception as e:
logger.error(f"Doubao 分析调用失败: {type(e).__name__}: {e}")
else:
logger.info("[AlarmReport] 豆包分析已关闭 (doubao_analysis.enabled=false)")
board_id = full_data.get("BoardId", "")
board_ip = full_data.get("BoardIp", "")
@@ -265,6 +273,38 @@ async def alarm_report(
except Exception as e:
logger.error(f"[AlarmReport] 数据库写入失败: {e}")
# 告警转发到外部平台
from .global_var import alarm_forward_enabled, alarm_forward_url, alarm_forward_timeout
if alarm_forward_enabled and alarm_forward_url:
try:
async with httpx.AsyncClient(timeout=alarm_forward_timeout) as http_client:
forward_resp = await http_client.post(
alarm_forward_url,
json=full_data,
headers={"Content-Type": "application/json"},
)
if forward_resp.is_success:
logger.info(
f"[AlarmReport] 转发成功: AlarmId={alarm_id}, "
f"URL={alarm_forward_url}, Status={forward_resp.status_code}"
)
else:
logger.warning(
f"[AlarmReport] 转发失败: AlarmId={alarm_id}, "
f"URL={alarm_forward_url}, Status={forward_resp.status_code}, "
f"Body={forward_resp.text[:500]}"
)
except httpx.TimeoutException:
logger.error(
f"[AlarmReport] 转发超时: AlarmId={alarm_id}, "
f"URL={alarm_forward_url}, Timeout={alarm_forward_timeout}s"
)
except Exception as e:
logger.error(
f"[AlarmReport] 转发异常: AlarmId={alarm_id}, "
f"URL={alarm_forward_url}, Error={type(e).__name__}: {e}"
)
return AlarmReportResponse(
Result=AlarmReportResult(Code=0, Desc="")
)