Skip to content

Commit 63eaee8

Browse files
authored
Merge pull request #36 from CHENPrime-coder/fix/all_metrics_503
refactor: 移除获取所有客户端指标的相关代码,改为按需获取单个客户端指标
2 parents bb3dec3 + 7f7a113 commit 63eaee8

File tree

5 files changed

+16
-53
lines changed

5 files changed

+16
-53
lines changed

backend/src/api/agents.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
registerAgentService,
1212
updateAgentStatusService,
1313
getAgentMetrics,
14-
getAllAgentMetrics,
1514
} from "../services/AgentService";
1615

1716
const agents = new Hono<{
@@ -169,20 +168,6 @@ agents.get("/:id/metrics", async (c) => {
169168
)
170169
})
171170

172-
// 获取所有客户端的指标
173-
agents.get("/metrics", async (c) => {
174-
const result = await getAllAgentMetrics(c.env.DB);
175-
return c.json(
176-
{
177-
success: result.success,
178-
metrics: result.results,
179-
message: "获取所有客户端指标成功",
180-
},
181-
200
182-
)
183-
})
184-
185-
186171
// 获取单个客户端
187172
agents.get("/:id", async (c) => {
188173
const agentId = Number(c.req.param("id"));

backend/src/repositories/agent.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,3 @@ export async function getAgentMetrics(db: Bindings["DB"], agentId: number) {
224224
.bind(agentId)
225225
.all();
226226
}
227-
228-
// 获取所有客户端资源指标
229-
export async function getAllAgentMetrics(db: Bindings["DB"]) {
230-
return await db.prepare("SELECT * FROM agent_metrics_24h").all<Metrics>();
231-
}

backend/src/services/AgentService.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,4 @@ export async function setAgentInactive(env: any, id: number) {
474474

475475
export async function getAgentMetrics(db: Bindings["DB"], agentId: number) {
476476
return await AgentRepository.getAgentMetrics(db, agentId);
477-
}
478-
479-
export async function getAllAgentMetrics(db: Bindings["DB"]) {
480-
return await AgentRepository.getAllAgentMetrics(db);
481477
}

frontend/src/api/agents.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,3 @@ export const getAgentMetrics = async (
8787
};
8888
}
8989
};
90-
91-
export const getAllAgentMetrics = async (): Promise<{
92-
success: boolean;
93-
metrics?: MetricHistory[];
94-
message?: string;
95-
}> => {
96-
try {
97-
const response = await api.get(`/api/agents/metrics`);
98-
return response.data;
99-
} catch (error) {
100-
console.error(`获取所有客户端的指标失败:`, error);
101-
return {
102-
success: false,
103-
message: "获取所有客户端指标失败",
104-
};
105-
}
106-
};

frontend/src/pages/agents/AgentsList.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import {
2727
getAllAgents,
2828
deleteAgent,
29-
getAllAgentMetrics,
29+
getAgentMetrics,
3030
} from "../../api/agents";
3131
import AgentCard from "../../components/AgentCard";
3232
import { useTranslation } from "react-i18next";
@@ -68,19 +68,23 @@ const AgentsList = () => {
6868
setError(null);
6969
// 获取所有客户端
7070
const response = await getAllAgents();
71-
// 获取所有客户端的指标数据
72-
const metricsResponse = await getAllAgentMetrics();
7371

74-
if (response.agents && metricsResponse.metrics) {
72+
if (response.agents) {
7573
// 合并指标数据到客户端数据
76-
const agentsWithMetrics = response.agents.map((agent) => {
77-
const metrics = metricsResponse.metrics?.filter(
78-
(metric) => metric.agent_id === agent.id
79-
);
80-
console.log("获取到的 metrics 数据: ", metrics);
81-
console.log("获取到的 agent 数据: ", agent);
82-
return { ...agent, metrics };
83-
});
74+
const agentsWithMetrics = await Promise.all(
75+
response.agents.map(async (agent) => {
76+
// 获取指定客户端的指标数据
77+
const metricsResponse = await getAgentMetrics(agent.id);
78+
if (!metricsResponse.success) {
79+
console.error("获取指标数据失败:", metricsResponse.message);
80+
return agent; // 返回原始客户端数据
81+
}
82+
const metrics = metricsResponse.agent;
83+
console.log("获取到的 metrics 数据: ", metrics);
84+
console.log("获取到的 agent 数据: ", agent);
85+
return { ...agent, metrics };
86+
})
87+
);
8488
setAgents(agentsWithMetrics);
8589
}
8690
console.log("获取到的 agent 数据: ", response);

0 commit comments

Comments
 (0)