| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import { Hono } from "hono";
- import debug from "debug";
- import type {
- DeviceAlert,
- } from "../client/share/monitorTypes.ts";
- import {
- DeleteStatus,
- } from "../client/share/types.ts";
- import type { Variables, WithAuth } from "./middlewares.ts";
- const log = {
- api: debug("api:monitor"),
- };
- // 设备告警记录路由
- export function createAlertsRoutes(withAuth: WithAuth) {
- const alertsRoutes = new Hono<{ Variables: Variables }>();
-
- // 获取设备告警记录列表
- alertsRoutes.get("/", withAuth, async (c) => {
- try {
- const apiClient = c.get('apiClient');
-
- // 获取分页参数
- const page = Number(c.req.query("page")) || 1;
- const pageSize = Number(c.req.query("pageSize")) || 10;
- const offset = (page - 1) * pageSize;
-
- // 获取筛选参数
- const deviceId = c.req.query("device_id")
- ? Number(c.req.query("device_id"))
- : null;
- const metricType = c.req.query("metric_type");
- const alertLevel = c.req.query("alert_level")
- ? Number(c.req.query("alert_level"))
- : null;
- const status = c.req.query("status");
- const startTime = c.req.query("start_time");
- const endTime = c.req.query("end_time");
-
- // 构建查询
- let query = apiClient.database
- .table("device_alerts")
- .where("is_deleted", DeleteStatus.NOT_DELETED)
- .orderBy("created_at", "desc");
-
- // 应用筛选条件
- if (deviceId) {
- query = query.where("device_id", deviceId);
- }
-
- if (metricType) {
- query = query.where("metric_type", metricType);
- }
-
- if (alertLevel !== null) {
- query = query.where("alert_level", alertLevel);
- }
-
- if (status) {
- query = query.where("status", status);
- }
-
- if (startTime && endTime) {
- query = query.whereBetween("created_at", [startTime, endTime]);
- } else if (startTime) {
- query = query.where("created_at", ">=", startTime);
- } else if (endTime) {
- query = query.where("created_at", "<=", endTime);
- }
-
- // 获取总记录数
- const total = await query.clone().count();
-
- // 获取分页数据
- const data = await query.limit(pageSize).offset(offset);
-
- return c.json({
- data,
- total: Number(total),
- page,
- pageSize,
- });
- } catch (error) {
- log.api("获取告警记录失败:", error);
- return c.json({ error: "获取告警记录失败" }, 500);
- }
- });
-
- // 获取单个告警记录
- alertsRoutes.get("/:id", withAuth, async (c) => {
- try {
- const id = Number(c.req.param("id"));
-
- if (!id || isNaN(id)) {
- return c.json({ error: "无效的告警ID" }, 400);
- }
-
- const apiClient = c.get('apiClient');
- const alert = await apiClient.database
- .table("device_alerts")
- .where("id", id)
- .where("is_deleted", DeleteStatus.NOT_DELETED)
- .first();
-
- if (!alert) {
- return c.json({ error: "告警不存在" }, 404);
- }
-
- return c.json(alert);
- } catch (error) {
- log.api("获取告警详情失败:", error);
- return c.json({ error: "获取告警详情失败" }, 500);
- }
- });
-
- // 更新告警状态
- alertsRoutes.put("/:id", withAuth, async (c) => {
- try {
- const id = Number(c.req.param("id"));
-
- if (!id || isNaN(id)) {
- return c.json({ error: "无效的告警ID" }, 400);
- }
-
- const data = (await c.req.json()) as Partial<DeviceAlert>;
-
- // 只允许更新状态
- if (!data.status) {
- return c.json({ error: "状态不能为空" }, 400);
- }
-
- const apiClient = c.get('apiClient');
-
- // 检查告警是否存在
- const alert = await apiClient.database
- .table("device_alerts")
- .where("id", id)
- .where("is_deleted", DeleteStatus.NOT_DELETED)
- .first();
-
- if (!alert) {
- return c.json({ error: "告警不存在" }, 404);
- }
-
- // 更新告警
- await apiClient.database.table("device_alerts").where("id", id).update({
- status: data.status,
- updated_at: apiClient.database.fn.now(),
- });
-
- // 获取更新后的数据
- const [updatedAlert] = await apiClient.database
- .table("device_alerts")
- .where("id", id);
-
- return c.json({
- message: "告警状态更新成功",
- data: updatedAlert,
- });
- } catch (error) {
- log.api("更新告警状态失败:", error);
- return c.json({ error: "更新告警状态失败" }, 500);
- }
- });
-
- return alertsRoutes;
- }
-
|