Ensure getAgentById returns agent entity

This commit is contained in:
阿杰鲁 committed 2025-11-05 08:16:44 +08:00
1 parent 75507a0f33
commit f7dfe115f0
3 files changed
+39 -20

No files matched your search

+9 -11
View File
@@ -99,8 +99,7 @@ async function handleAgentOfflineNotification(
// 获取客户端完整信息
const agent = await getAgentById(agentId);
const agentData = agent[0];
if (!agentData) {
if (!agent) {
console.error(`找不到客户端数据 (ID: ${agentId})`);
return;
}
@@ -111,16 +110,16 @@ async function handleAgentOfflineNotification(
status: "offline",
previous_status: "online", // 添加previous_status变量
time: new Date().toLocaleString("zh-CN"),
hostname: agentData.hostname || "未知",
ip_addresses: getFormattedIPAddresses(agentData.ip_addresses),
os: agentData.os || "未知",
hostname: agent.hostname || "未知",
ip_addresses: getFormattedIPAddresses(agent.ip_addresses),
os: agent.os || "未知",
error: "客户端连接超时",
details: `主机名: ${
agentData.hostname || "未知"
agent.hostname || "未知"
}\nIP地址: ${getFormattedIPAddresses(
agentData.ip_addresses
)}\n操作系统: ${agentData.os || "未知"}\n最后连接时间: ${new Date(
agentData.updated_at
agent.ip_addresses
)}\n操作系统: ${agent.os || "未知"}\n最后连接时间: ${new Date(
agent.updated_at
).toLocaleString("zh-CN")}`,
};
@@ -163,8 +162,7 @@ export async function handleAgentThresholdNotification(
console.error(`找不到客户端 (ID: ${agentId})`);
throw new Error(`找不到客户端 (ID: ${agentId})`);
}
// @ts-ignore
const userId = agent.created_by; // 获取 userId
// 根据具体的指标类型
+6 -1
View File
@@ -31,12 +31,17 @@ export async function getAgentMetricsByIds(agentIds: number[]) {
}
// 获取单个客户端详情
export async function getAgentById(id: number) {
export async function getAgentById(id: number): Promise<Agent | null> {
const agent = await db
.select()
.from(agents)
.where(eq(agents.id, id))
.limit(1);
if (!agent || agent.length === 0) {
return null;
}
return agent[0];
}
+24 -8
View File
@@ -1,4 +1,5 @@
import { Bindings } from "../models/db";
import type { Agent } from "../models/agent";
import * as AgentRepository from "../repositories";
import { generateToken, verifyToken } from "../utils/jwt";
import { handleAgentThresholdNotification } from "../jobs/agent-task";
@@ -84,15 +85,29 @@ export async function updateAgentService(
if (updateData.ip_addresses && updateData.ip_addresses.length > 0) {
agent.ip_addresses = JSON.stringify(updateData.ip_addresses);
} else {
} else if (updateData.ip_addresses) {
agent.ip_addresses = "[]";
}
agent.name = updateData.name;
agent.hostname = updateData.hostname;
agent.os = updateData.os;
agent.version = updateData.version;
agent.status = updateData.status;
if (typeof updateData.name === "string") {
agent.name = updateData.name;
}
if (updateData.hostname !== undefined) {
agent.hostname = updateData.hostname ?? null;
}
if (updateData.os !== undefined) {
agent.os = updateData.os ?? null;
}
if (updateData.version !== undefined) {
agent.version = updateData.version ?? null;
}
if (updateData.status !== undefined) {
agent.status = updateData.status ?? null;
}
// 执行更新
const updatedAgent = await AgentRepository.updateAgent(agent);
@@ -359,8 +374,9 @@ export async function updateAgentStatusService(status: any) {
}
}
export async function getAgentById(id: number) {
return await AgentRepository.getAgentById(id);
export async function getAgentById(id: number): Promise<Agent | null> {
const agent = await AgentRepository.getAgentById(id);
return agent ?? null;
}
export async function getActiveAgents() {