Skip to content

Commit 51d3c7b

Browse files
committed
Update.
1 parent ec6d59f commit 51d3c7b

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

cmd/dashboard/controller/api_v1.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"log"
45
"strconv"
56
"strings"
67

@@ -149,16 +150,50 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
149150

150151
// 根据数据库类型选择不同的查询方式
151152
if singleton.Conf.DatabaseType == "badger" {
152-
// BadgerDB 模式下使用 MonitorAPI
153+
// BadgerDB 模式下使用 MonitorAPI,只查询ICMP/TCP监控数据
153154
if singleton.MonitorAPI != nil {
154-
c.JSON(200, singleton.MonitorAPI.GetMonitorHistories(map[string]any{"server_id": server.ID}))
155+
// 获取所有监控历史记录
156+
allHistories := singleton.MonitorAPI.GetMonitorHistories(map[string]any{"server_id": server.ID})
157+
158+
// 过滤出ICMP和TCP监控记录
159+
var networkHistories []*model.MonitorHistory
160+
for _, history := range allHistories {
161+
// 检查监控类型是否为ICMP或TCP
162+
if history.MonitorID > 0 {
163+
// 从监控配置中获取监控类型
164+
if monitors := singleton.ServiceSentinelShared.Monitors(); monitors != nil {
165+
for _, monitor := range monitors {
166+
if monitor.ID == history.MonitorID &&
167+
(monitor.Type == model.TaskTypeICMPPing || monitor.Type == model.TaskTypeTCPPing) {
168+
networkHistories = append(networkHistories, history)
169+
break
170+
}
171+
}
172+
}
173+
}
174+
}
175+
c.JSON(200, networkHistories)
155176
} else {
156177
c.JSON(200, []interface{}{})
157178
}
158179
} else {
159-
// SQLite 模式下使用 MonitorAPI
160-
if singleton.MonitorAPI != nil {
161-
c.JSON(200, singleton.MonitorAPI.GetMonitorHistories(map[string]any{"server_id": server.ID}))
180+
// SQLite 模式下直接查询ICMP/TCP监控数据
181+
if singleton.DB != nil {
182+
var networkHistories []*model.MonitorHistory
183+
184+
// 查询ICMP和TCP监控历史记录
185+
err := singleton.DB.Where("server_id = ? AND monitor_id IN (SELECT id FROM monitors WHERE type IN (?, ?))",
186+
server.ID, model.TaskTypeICMPPing, model.TaskTypeTCPPing).
187+
Order("created_at DESC").
188+
Limit(1000). // 限制返回数量,避免数据过多
189+
Find(&networkHistories).Error
190+
191+
if err != nil {
192+
log.Printf("查询网络监控历史记录失败: %v", err)
193+
c.JSON(200, []interface{}{})
194+
} else {
195+
c.JSON(200, networkHistories)
196+
}
162197
} else {
163198
c.JSON(200, []interface{}{})
164199
}

cmd/dashboard/controller/common_page.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,24 @@ func (cp *commonPage) network(c *gin.Context) {
367367
log.Printf("network: 从BadgerDB查询监控历史记录失败: %v", err)
368368
monitorHistories = []model.MonitorHistory{}
369369
} else {
370-
// 过滤出指定服务器的记录并转换为值数组
370+
// 修复:过滤出指定服务器的ICMP/TCP监控记录
371371
var filteredHistories []model.MonitorHistory
372372
for _, h := range allHistories {
373373
if h != nil && h.ServerID == id {
374-
filteredHistories = append(filteredHistories, *h)
374+
// 检查是否为ICMP或TCP监控
375+
if monitors := singleton.ServiceSentinelShared.Monitors(); monitors != nil {
376+
for _, monitor := range monitors {
377+
if monitor.ID == h.MonitorID &&
378+
(monitor.Type == model.TaskTypeICMPPing || monitor.Type == model.TaskTypeTCPPing) {
379+
filteredHistories = append(filteredHistories, *h)
380+
break
381+
}
382+
}
383+
}
375384
}
376385
}
377386
monitorHistories = filteredHistories
378-
// 移除频繁的监控历史记录查询日志
387+
log.Printf("network: 为服务器 %d 找到 %d 条ICMP/TCP监控历史记录", id, len(filteredHistories))
379388
}
380389
} else {
381390
log.Printf("network: BadgerDB未初始化或无效服务器ID,使用空数组")

resource/template/theme-default/network.html

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
return [];
231231
}
232232

233-
// 按MonitorID分组数据
233+
// 修复:按MonitorID分组数据,确保同一时间点的不同监控点数据都能显示
234234
const groupedData = {};
235235
const monitorNames = {};
236236
const serverNames = {};
@@ -239,12 +239,16 @@
239239
const monitorId = history.MonitorID || history.monitor_id || 0;
240240
const serverId = history.ServerID || history.server_id || 0;
241241

242-
if (!groupedData[monitorId]) {
243-
groupedData[monitorId] = {
242+
// 修复:为每个监控点创建唯一的key,避免数据覆盖
243+
const uniqueKey = `${monitorId}_${serverId}`;
244+
245+
if (!groupedData[uniqueKey]) {
246+
groupedData[uniqueKey] = {
244247
created_at: [],
245248
avg_delay: [],
246249
monitor_id: monitorId,
247-
server_id: serverId
250+
server_id: serverId,
251+
unique_key: uniqueKey
248252
};
249253
}
250254

@@ -261,8 +265,8 @@
261265
// 添加延迟数据
262266
const avgDelay = history.AvgDelay || history.avg_delay || 0;
263267

264-
groupedData[monitorId].created_at.push(timestamp);
265-
groupedData[monitorId].avg_delay.push(avgDelay);
268+
groupedData[uniqueKey].created_at.push(timestamp);
269+
groupedData[uniqueKey].avg_delay.push(avgDelay);
266270

267271
// 记录监控器和服务器名称
268272
if (!monitorNames[monitorId]) {
@@ -276,22 +280,36 @@
276280
}
277281
});
278282

279-
// 转换为图表需要的格式
283+
// 修复:转换为图表需要的格式,确保每个监控点都能正确显示
280284
const result = [];
281-
Object.keys(groupedData).forEach(monitorId => {
282-
const data = groupedData[monitorId];
285+
Object.keys(groupedData).forEach(uniqueKey => {
286+
const data = groupedData[uniqueKey];
287+
const monitorId = data.monitor_id;
283288
const serverId = data.server_id;
284289

290+
// 将时间戳和延迟数据配对并排序
291+
const paired = data.created_at.map((time, index) => ({
292+
time: time,
293+
delay: data.avg_delay[index]
294+
})).sort((a, b) => a.time - b.time);
295+
296+
// 生成清晰的显示名称
297+
const monitorName = monitorNames[monitorId] || `Monitor ${monitorId}`;
298+
const serverName = serverNames[serverId] || `Server ${serverId}`;
299+
285300
result.push({
286-
monitor_id: parseInt(monitorId),
301+
monitor_id: monitorId,
287302
server_id: serverId,
288-
monitor_name: monitorNames[monitorId],
289-
server_name: serverNames[serverId],
290-
created_at: data.created_at,
291-
avg_delay: data.avg_delay
303+
unique_key: uniqueKey,
304+
monitor_name: monitorName,
305+
server_name: serverName,
306+
display_name: `${serverName} - ${monitorName}`,
307+
created_at: paired.map(p => p.time),
308+
avg_delay: paired.map(p => p.delay)
292309
});
293310
});
294311

312+
console.log('处理后的监控数据:', result);
295313
return result;
296314
},
297315
getFontLogoClass(str) {
@@ -463,7 +481,8 @@
463481
if (lossRate > 99) {
464482
datal = [];
465483
}
466-
legendName = processedData[i].monitor_name +" "+ lossRate + "%";
484+
// 修复:使用更清晰的图例名称,区分不同监控点
485+
legendName = (processedData[i].display_name || processedData[i].monitor_name) + " " + lossRate + "%";
467486
tLegendData.push(legendName);
468487
tSeries.push({
469488
name: legendName,

0 commit comments

Comments
 (0)