feat: 13个统计/聚合API + 前端同步 + 待完成功能文档

API新增:
- GET /api/system/overview 系统总览(在线率/今日统计/表大小)
- GET /api/locations/stats 位置统计(类型分布/小时趋势)
- GET /api/locations/track-summary/{id} 轨迹摘要(距离/时长/速度)
- POST /api/alarms/batch-acknowledge 批量确认告警
- GET /api/attendance/report 考勤日报表(每设备每天汇总)
- GET /api/bluetooth/stats 蓝牙统计(类型/TOP信标/RSSI分布)
- GET /api/heartbeats/stats 心跳统计(活跃设备/电量/间隔分析)
- GET /api/fences/stats 围栏统计(绑定/进出状态/今日事件)
- GET /api/fences/{id}/events 围栏进出事件历史
- GET /api/commands/stats 指令统计(成功率/类型/趋势)

API增强:
- devices/stats: 新增by_type/battery_distribution/signal_distribution
- alarms/stats: 新增today/by_source/daily_trend/top_devices
- attendance/stats: 新增today/by_source/daily_trend/by_device

前端同步:
- 仪表盘: 今日告警/考勤/定位卡片 + 在线率
- 告警页: 批量确认按钮 + 今日计数
- 考勤页: 今日计数
- 轨迹: 加载后显示距离/时长/速度摘要
- 蓝牙/围栏/指令页: 统计面板

文档: CLAUDE.md待完成功能按优先级重新规划

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
This commit is contained in:
2026-03-31 10:11:33 +00:00
parent b25eafc483
commit 8157f9cb52
10 changed files with 1044 additions and 51 deletions

View File

@@ -147,6 +147,74 @@ async def _send_to_device(
# ---------------------------------------------------------------------------
@router.get(
"/stats",
response_model=APIResponse[dict],
summary="指令统计 / Command statistics",
)
async def command_stats(
days: int = Query(default=7, ge=1, le=90, description="趋势天数"),
db: AsyncSession = Depends(get_db),
):
"""
指令统计:总数、按状态分布、按类型分布、成功率、按天趋势。
Command stats: total, by status, by type, success rate, daily trend.
"""
from sqlalchemy import func, select
from datetime import timedelta
from app.models import CommandLog
total = (await db.execute(select(func.count(CommandLog.id)))).scalar() or 0
# By status
status_result = await db.execute(
select(CommandLog.status, func.count(CommandLog.id))
.group_by(CommandLog.status)
)
by_status = {row[0]: row[1] for row in status_result.all()}
# By type
type_result = await db.execute(
select(CommandLog.command_type, func.count(CommandLog.id))
.group_by(CommandLog.command_type)
)
by_type = {row[0]: row[1] for row in type_result.all()}
# Success rate
sent = by_status.get("sent", 0) + by_status.get("success", 0)
failed = by_status.get("failed", 0)
total_attempted = sent + failed
success_rate = round(sent / total_attempted * 100, 1) if total_attempted else 0
# Daily trend
now = now_cst()
cutoff = now - timedelta(days=days)
trend_result = await db.execute(
select(
func.date(CommandLog.created_at).label("day"),
func.count(CommandLog.id),
)
.where(CommandLog.created_at >= cutoff)
.group_by("day").order_by("day")
)
daily_trend = {str(row[0]): row[1] for row in trend_result.all()}
# Today
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
today_count = (await db.execute(
select(func.count(CommandLog.id)).where(CommandLog.created_at >= today_start)
)).scalar() or 0
return APIResponse(data={
"total": total,
"today": today_count,
"by_status": by_status,
"by_type": by_type,
"success_rate": success_rate,
"daily_trend": daily_trend,
})
@router.get(
"",
response_model=APIResponse[PaginatedList[CommandResponse]],