diff --git a/backend/src/jobs/agent-task.ts b/backend/src/jobs/agent-task.ts index 50fbcfd..cc13c44 100644 --- a/backend/src/jobs/agent-task.ts +++ b/backend/src/jobs/agent-task.ts @@ -288,9 +288,7 @@ export default { // 默认执行监控检查任务 let result: any = await checkAgentsStatus(c); - const now = new Date(); - const hour = now.getUTCHours(); - const minute = now.getUTCMinutes(); + // 每天应该生成 agent_daily_stats 记录,从 agent_metrics_24h 中聚合数据 return result; }, diff --git a/backend/src/jobs/index.ts b/backend/src/jobs/index.ts index 3ab6dd5..f74ec01 100644 --- a/backend/src/jobs/index.ts +++ b/backend/src/jobs/index.ts @@ -23,13 +23,12 @@ export const runScheduledTasks = async (event: any, env: any, ctx: any) => { } }; - // 清理30天以前的历史记录 export async function cleanupOldRecords(db: Bindings["DB"]) { console.log("开始清理30天以前的历史记录..."); // 清理监控状态历史记录 - const deleteStatusHistoryResult = await db + await db .prepare( ` DELETE FROM monitor_status_history @@ -38,8 +37,28 @@ export async function cleanupOldRecords(db: Bindings["DB"]) { ) .run(); + // 清理30天以前的 monitor_daily_stats + await db + .prepare( + ` + DELETE FROM monitor_daily_stats + WHERE date < datetime('now', '-30 days') + ` + ) + .run(); + + // 清理30天以前的 agent_daily_stats + await db + .prepare( + ` + DELETE FROM agent_daily_stats + WHERE date < datetime('now', '-30 days') + ` + ) + .run(); + // 清理通知历史记录 - const deleteNotificationHistoryResult = await db + await db .prepare( ` DELETE FROM notification_history @@ -48,11 +67,7 @@ export async function cleanupOldRecords(db: Bindings["DB"]) { ) .run(); - console.log( - `清理完成:删除了 ${deleteStatusHistoryResult} 条状态历史记录,${deleteNotificationHistoryResult} 条通知历史记录` - ); - return { - success: true + success: true, }; -} \ No newline at end of file +} diff --git a/backend/src/models/agent.ts b/backend/src/models/agent.ts index 5256d00..1dea593 100644 --- a/backend/src/models/agent.ts +++ b/backend/src/models/agent.ts @@ -11,6 +11,18 @@ export interface Agent { ip_addresses: string | null; // 存储多个IP地址的JSON字符串 os: string | null; version: string | null; + metrics: Metrics[] | null; +} + +export interface Metrics { + id: number; + agent_id: number; + timestamp: string; + cpu: CPUInfo; + memory: MemoryInfo; + disks: DiskInfo[]; + networks: NetworkInfo[]; + load: LoadInfo; } export interface CPUInfo { diff --git a/backend/src/repositories/agent.ts b/backend/src/repositories/agent.ts index 7e9e605..c33312c 100644 --- a/backend/src/repositories/agent.ts +++ b/backend/src/repositories/agent.ts @@ -1,5 +1,5 @@ import { Bindings } from "../models/db"; -import { Agent } from "../models/agent"; +import { Agent, Metrics } from "../models/agent"; /** * 客户端相关的数据库操作 @@ -25,6 +25,18 @@ export async function getAgentsByIds(db: Bindings["DB"], agentIds: number[]) { .all(); } +// 批量获取客户端指标 +export async function getAgentMetricsByIds(db: Bindings["DB"], agentIds: number[]) { + if (agentIds.length === 0) { + return { results: [] }; + } + const placeholders = agentIds.map(() => "?").join(","); + return await db + .prepare(`SELECT * FROM agent_metrics_24h WHERE agent_id IN (${placeholders})`) + .bind(...agentIds) + .all(); +} + // 获取单个客户端详情 export async function getAgentById(db: Bindings["DB"], id: number) { return await db diff --git a/backend/src/services/StatusService.ts b/backend/src/services/StatusService.ts index e34e6af..4ff9aca 100644 --- a/backend/src/services/StatusService.ts +++ b/backend/src/services/StatusService.ts @@ -247,6 +247,16 @@ export async function getStatusPagePublicData(env: { DB: Bindings["DB"] }) { if (agentIds.length > 0) { const agentsResult = await repositories.getAgentsByIds(env.DB, agentIds); + const agentsMetricsResult = + await repositories.getAgentMetricsByIds(env.DB, agentIds); + if (agentsMetricsResult.results) { + for (const agent of agentsResult.results || []) { + const agentMetrics = agentsMetricsResult.results.filter( + (m) => m.agent_id === agent.id + ); + agent.metrics = agentMetrics; + } + } if (agentsResult.results) { agents = agentsResult.results;