feat: edc-web 设备日志管理页 + 在线状态实时刷新

- 新增 /device-logs 设备事件日志管理页 (admin 权限)
  - 支持按设备序列号/事件类型筛选查询
  - 支持 admin 按条件删除日志
  - 不同事件类型彩色标识 (在线=绿, 离线=红, 通信不良=橙)
- 新增 /api/devices/<id>/status 设备状态 API
- 设备列表页:每 5s 异步刷新所有设备在线状态
- 测试操作页:顶部显示设备状态,每 5s 异步刷新
- dnt_info state 支持三态显示 (在线/离线/通信不良)
- 导航栏增加「设备日志」入口 (admin only)
This commit is contained in:
wangfq
2026-06-10 09:14:32 +08:00
parent 60c11fe719
commit ee136cc707
11 changed files with 366 additions and 7 deletions

View File

@@ -827,3 +827,79 @@ def delete_test_data(serial: str = "", date_from: str = "",
return cnt
finally:
conn.close()
# ─── tb_device_log ─────────────────────────────────────────────────
def get_device_logs(page: int = 1, per_page: int = 30,
serial: str = "", event_type: str = "") -> tuple[list[dict], int]:
"""分页查询设备事件日志,返回 (records, total)"""
conn = get_conn()
try:
with conn.cursor() as cur:
where = []
params = []
if serial:
where.append("device_serial LIKE %s")
params.append(f"%{serial}%")
if event_type:
where.append("event_type = %s")
params.append(event_type)
where_clause = " AND ".join(where) if where else "1=1"
cur.execute(
f"SELECT COUNT(*) as total FROM tb_device_log WHERE {where_clause}",
params,
)
total = cur.fetchone()["total"]
offset = (page - 1) * per_page
cur.execute(
f"SELECT * FROM tb_device_log WHERE {where_clause} "
f"ORDER BY id DESC LIMIT %s OFFSET %s",
params + [per_page, offset],
)
return cur.fetchall(), total
finally:
conn.close()
def delete_device_logs(serial: str = "", event_type: str = "",
date_from: str = "", date_to: str = "") -> int:
"""删除符合条件的设备日志,返回删除行数。至少需要一个条件。"""
conn = get_conn()
try:
with conn.cursor() as cur:
where = []
params = []
if serial:
where.append("device_serial LIKE %s")
params.append(f"%{serial}%")
if event_type:
where.append("event_type = %s")
params.append(event_type)
if date_from:
where.append("create_time >= %s")
params.append(date_from if len(date_from) > 10 else date_from)
if date_to:
where.append("create_time <= %s")
params.append(date_to if len(date_to) > 10 else date_to + " 23:59:59")
if not where:
return 0
where_clause = " AND ".join(where)
cur.execute(
f"SELECT COUNT(*) as cnt FROM tb_device_log WHERE {where_clause}",
params,
)
cnt = cur.fetchone()["cnt"]
cur.execute(
f"DELETE FROM tb_device_log WHERE {where_clause}", params,
)
conn.commit()
return cnt
finally:
conn.close()