fix: status data without metrics
This commit is contained in:
1 parent
06cb37589f
commit
8b5a7c118a
5 files changed
+60
-13
No files matched your search
@@ -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;
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<Agent>();
|
||||
}
|
||||
|
||||
// 批量获取客户端指标
|
||||
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<Metrics>();
|
||||
}
|
||||
|
||||
// 获取单个客户端详情
|
||||
export async function getAgentById(db: Bindings["DB"], id: number) {
|
||||
return await db
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user