添加--添加获取未处理告警消息数量接口

This commit is contained in:
haotian 2025-05-30 14:17:13 +08:00
parent 15e088ffad
commit 4f4a484b6d
2 changed files with 26 additions and 2 deletions

View File

@ -6,7 +6,7 @@ from app.crud.event import event
from app.schemas.event import EventList, EventDetail, EventUpdate, EventQuery, BackStageEvent, BackStageEventDto, BackStageEventDetail, EditTemperatureDto, OcrAlertMessage, OcrAlertMessageDto
from app.util.httpResponse import BaseResponse
import datetime
# import datetime
router = APIRouter()
@ -175,6 +175,14 @@ async def get_alert_detail(
alert_message = await event.get_alert_detail(db, messageId=messageId)
return BaseResponse(code=200, msg="success", data=alert_message)
# 获取未处理告警消息数量
@router.get("/events/alertCount", response_model=BaseResponse)
async def get_alert_count(
db: AsyncSession = Depends(get_db)
):
alert_count = await event.get_alert_count(db)
return BaseResponse(code=200, msg="success", data=alert_count)

View File

@ -1,5 +1,5 @@
from typing import List, Optional, Dict, Any
from sqlalchemy import select, and_, or_, update, bindparam
from sqlalchemy import select, and_, or_, update, bindparam, func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.crud.base import CRUDBase
@ -324,6 +324,22 @@ class CRUDEvent(CRUDBase[Event, EventUpdate, EventUpdate]):
except Exception as e:
print(f"获取告警详情失败: {e}")
async def get_alert_count(
self,
db: AsyncSession
):
try:
query_stmt = (
select(func.count(Message.messageId))
.where(Message.handle == "0")
)
result = await db.execute(query_stmt)
return result.scalar()
except Exception as e:
print(f"{e}")