Initial commit: migrate badge-admin from /tmp to /home/gpsystem
via HAPI (https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
This commit is contained in:
172
app/routers/alarms.py
Normal file
172
app/routers/alarms.py
Normal file
@@ -0,0 +1,172 @@
|
||||
"""
|
||||
Alarms Router - 报警管理接口
|
||||
API endpoints for alarm record queries, acknowledgement, and statistics.
|
||||
"""
|
||||
|
||||
import math
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import get_db
|
||||
from app.models import AlarmRecord
|
||||
from app.schemas import (
|
||||
AlarmAcknowledge,
|
||||
AlarmRecordResponse,
|
||||
APIResponse,
|
||||
PaginatedList,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/alarms", tags=["Alarms / 报警管理"])
|
||||
|
||||
|
||||
@router.get(
|
||||
"",
|
||||
response_model=APIResponse[PaginatedList[AlarmRecordResponse]],
|
||||
summary="获取报警记录列表 / List alarms",
|
||||
)
|
||||
async def list_alarms(
|
||||
device_id: int | None = Query(default=None, description="设备ID / Device ID"),
|
||||
alarm_type: str | None = Query(default=None, description="报警类型 / Alarm type"),
|
||||
acknowledged: bool | None = Query(default=None, description="是否已确认 / Acknowledged status"),
|
||||
start_time: datetime | None = Query(default=None, description="开始时间 / Start time (ISO 8601)"),
|
||||
end_time: datetime | None = Query(default=None, description="结束时间 / End time (ISO 8601)"),
|
||||
page: int = Query(default=1, ge=1, description="页码 / Page number"),
|
||||
page_size: int = Query(default=20, ge=1, le=100, description="每页数量 / Items per page"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
获取报警记录列表,支持按设备、报警类型、确认状态、时间范围过滤。
|
||||
List alarm records with filters for device, alarm type, acknowledged status, and time range.
|
||||
"""
|
||||
query = select(AlarmRecord)
|
||||
count_query = select(func.count(AlarmRecord.id))
|
||||
|
||||
if device_id is not None:
|
||||
query = query.where(AlarmRecord.device_id == device_id)
|
||||
count_query = count_query.where(AlarmRecord.device_id == device_id)
|
||||
|
||||
if alarm_type:
|
||||
query = query.where(AlarmRecord.alarm_type == alarm_type)
|
||||
count_query = count_query.where(AlarmRecord.alarm_type == alarm_type)
|
||||
|
||||
if acknowledged is not None:
|
||||
query = query.where(AlarmRecord.acknowledged == acknowledged)
|
||||
count_query = count_query.where(AlarmRecord.acknowledged == acknowledged)
|
||||
|
||||
if start_time:
|
||||
query = query.where(AlarmRecord.recorded_at >= start_time)
|
||||
count_query = count_query.where(AlarmRecord.recorded_at >= start_time)
|
||||
|
||||
if end_time:
|
||||
query = query.where(AlarmRecord.recorded_at <= end_time)
|
||||
count_query = count_query.where(AlarmRecord.recorded_at <= end_time)
|
||||
|
||||
total_result = await db.execute(count_query)
|
||||
total = total_result.scalar() or 0
|
||||
|
||||
offset = (page - 1) * page_size
|
||||
query = query.order_by(AlarmRecord.recorded_at.desc()).offset(offset).limit(page_size)
|
||||
result = await db.execute(query)
|
||||
alarms = list(result.scalars().all())
|
||||
|
||||
return APIResponse(
|
||||
data=PaginatedList(
|
||||
items=[AlarmRecordResponse.model_validate(a) for a in alarms],
|
||||
total=total,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
total_pages=math.ceil(total / page_size) if total else 0,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/stats",
|
||||
response_model=APIResponse[dict],
|
||||
summary="获取报警统计 / Get alarm statistics",
|
||||
)
|
||||
async def alarm_stats(db: AsyncSession = Depends(get_db)):
|
||||
"""
|
||||
获取报警统计:总数、未确认数、按类型分组统计。
|
||||
Get alarm statistics: total, unacknowledged count, and breakdown by type.
|
||||
"""
|
||||
# Total alarms
|
||||
total_result = await db.execute(select(func.count(AlarmRecord.id)))
|
||||
total = total_result.scalar() or 0
|
||||
|
||||
# Unacknowledged alarms
|
||||
unack_result = await db.execute(
|
||||
select(func.count(AlarmRecord.id)).where(AlarmRecord.acknowledged == False) # noqa: E712
|
||||
)
|
||||
unacknowledged = unack_result.scalar() or 0
|
||||
|
||||
# By type
|
||||
type_result = await db.execute(
|
||||
select(AlarmRecord.alarm_type, func.count(AlarmRecord.id))
|
||||
.group_by(AlarmRecord.alarm_type)
|
||||
.order_by(func.count(AlarmRecord.id).desc())
|
||||
)
|
||||
by_type = {row[0]: row[1] for row in type_result.all()}
|
||||
|
||||
return APIResponse(
|
||||
data={
|
||||
"total": total,
|
||||
"unacknowledged": unacknowledged,
|
||||
"acknowledged": total - unacknowledged,
|
||||
"by_type": by_type,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{alarm_id}",
|
||||
response_model=APIResponse[AlarmRecordResponse],
|
||||
summary="获取报警详情 / Get alarm details",
|
||||
)
|
||||
async def get_alarm(alarm_id: int, db: AsyncSession = Depends(get_db)):
|
||||
"""
|
||||
按ID获取报警记录详情。
|
||||
Get alarm record details by ID.
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(AlarmRecord).where(AlarmRecord.id == alarm_id)
|
||||
)
|
||||
alarm = result.scalar_one_or_none()
|
||||
if alarm is None:
|
||||
raise HTTPException(status_code=404, detail=f"Alarm {alarm_id} not found / 未找到报警记录{alarm_id}")
|
||||
return APIResponse(data=AlarmRecordResponse.model_validate(alarm))
|
||||
|
||||
|
||||
@router.put(
|
||||
"/{alarm_id}/acknowledge",
|
||||
response_model=APIResponse[AlarmRecordResponse],
|
||||
summary="确认报警 / Acknowledge alarm",
|
||||
)
|
||||
async def acknowledge_alarm(
|
||||
alarm_id: int,
|
||||
body: AlarmAcknowledge | None = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
确认(或取消确认)报警记录。
|
||||
Acknowledge (or un-acknowledge) an alarm record.
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(AlarmRecord).where(AlarmRecord.id == alarm_id)
|
||||
)
|
||||
alarm = result.scalar_one_or_none()
|
||||
if alarm is None:
|
||||
raise HTTPException(status_code=404, detail=f"Alarm {alarm_id} not found / 未找到报警记录{alarm_id}")
|
||||
|
||||
acknowledged = body.acknowledged if body else True
|
||||
alarm.acknowledged = acknowledged
|
||||
|
||||
await db.flush()
|
||||
await db.refresh(alarm)
|
||||
return APIResponse(
|
||||
message="Alarm acknowledged / 报警已确认" if acknowledged else "Alarm un-acknowledged / 已取消确认",
|
||||
data=AlarmRecordResponse.model_validate(alarm),
|
||||
)
|
||||
Reference in New Issue
Block a user