import { Hono } from "hono"; import debug from "debug"; import type { AlertHandleLog, } from "../client/share/monitorTypes.ts"; import { AlertStatus, HandleType, } 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 createAlertHandlesRoutes(withAuth: WithAuth) { const alertHandlesRoutes = new Hono<{ Variables: Variables }>(); // 获取告警处理记录列表 alertHandlesRoutes.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 alertId = c.req.query("alert_id") ? Number(c.req.query("alert_id")) : null; const handlerId = c.req.query("handler_id") ? Number(c.req.query("handler_id")) : null; const handleType = c.req.query("handle_type"); const problemType = c.req.query("problem_type"); const startTime = c.req.query("start_time"); const endTime = c.req.query("end_time"); // 构建查询 let query = apiClient.database .table("alert_handle_logs") .where("is_deleted", DeleteStatus.NOT_DELETED) .orderBy("handle_time", "desc"); // 应用筛选条件 if (alertId) { query = query.where("alert_id", alertId); } if (handlerId) { query = query.where("handler_id", handlerId); } if (handleType) { query = query.where("handle_type", handleType); } if (problemType) { query = query.where("problem_type", problemType); } if (startTime && endTime) { query = query.whereBetween("handle_time", [startTime, endTime]); } else if (startTime) { query = query.where("handle_time", ">=", startTime); } else if (endTime) { query = query.where("handle_time", "<=", endTime); } // 获取总记录数 const total = await query.clone().count(); // 获取分页数据 const logs = await query.limit(pageSize).offset(offset); // 处理JSON字段 const data = logs.map((log) => ({ ...log, attachments: log.attachments || [], notify_items: log.notify_items || [], })); return c.json({ data, total: Number(total), page, pageSize, }); } catch (error) { log.api("获取告警处理记录失败:", error); return c.json({ error: "获取告警处理记录失败" }, 500); } }); // 获取单个告警处理记录 alertHandlesRoutes.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 handleLog = await apiClient.database .table("alert_handle_logs") .where("id", id) .where("is_deleted", DeleteStatus.NOT_DELETED) .first(); if (!handleLog) { return c.json({ error: "处理记录不存在" }, 404); } // 处理JSON字段 handleLog.attachments = handleLog.attachments ? JSON.parse(handleLog.attachments) : []; handleLog.notify_items = handleLog.notify_items ? JSON.parse(handleLog.notify_items) : []; return c.json(handleLog); } catch (error) { log.api("获取告警处理记录详情失败:", error); return c.json({ error: "获取告警处理记录详情失败" }, 500); } }); // 创建告警处理记录 alertHandlesRoutes.post("/", withAuth, async (c) => { try { const data = (await c.req.json()) as Partial; const user = c.get("user")!; // 验证必填字段 if (!data.alert_id) { return c.json({ error: "告警ID不能为空" }, 400); } if (!data.handle_type) { return c.json({ error: "处理类型不能为空" }, 400); } if (!data.problem_type) { return c.json({ error: "问题类型不能为空" }, 400); } const apiClient = c.get('apiClient'); // 检查告警是否存在 const alert = await apiClient.database .table("device_alerts") .where("id", data.alert_id) .where("is_deleted", DeleteStatus.NOT_DELETED) .first(); if (!alert) { return c.json({ error: "告警不存在" }, 404); } // 处理JSON字段 const insertData = { ...data, handler_id: user.id, attachments: data.attachments ? JSON.stringify(data.attachments) : null, notify_items: data.notify_items ? JSON.stringify(data.notify_items) : null, handle_time: apiClient.database.fn.now(), created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now(), }; // 插入处理记录 const [id] = await apiClient.database .table("alert_handle_logs") .insert(insertData); // 更新告警状态 let newStatus = alert.status; switch (data.handle_type) { case HandleType.CONFIRM: newStatus = AlertStatus.HANDLING; break; case HandleType.RESOLVE: newStatus = AlertStatus.RESOLVED; break; case HandleType.IGNORE: newStatus = AlertStatus.IGNORED; break; } await apiClient.database .table("device_alerts") .where("id", data.alert_id) .update({ status: newStatus, updated_at: apiClient.database.fn.now(), }); // 获取插入的数据 const [insertedLog] = await apiClient.database .table("alert_handle_logs") .where("id", id); // 处理返回的JSON字段 insertedLog.attachments = insertedLog.attachments || []; insertedLog.notify_items = insertedLog.notify_items || []; return c.json({ message: "告警处理记录创建成功", data: insertedLog, }); } catch (error) { log.api("创建告警处理记录失败:", error); return c.json({ error: "创建告警处理记录失败" }, 500); } }); return alertHandlesRoutes; }