Skip to content

Commit e2ee42c

Browse files
committed
Update.
1 parent c7887e7 commit e2ee42c

File tree

2 files changed

+49
-92
lines changed

2 files changed

+49
-92
lines changed

cmd/dashboard/controller/api_v1.go

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controller
22

33
import (
44
"log"
5+
"sort"
56
"strconv"
67
"strings"
78
"time"
@@ -150,49 +151,47 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
150151
return
151152
}
152153

153-
// 性能优化:根据数据库类型选择不同的查询方式,限制数据量
154+
// 紧急性能优化:避免全表扫描,使用更高效的查询方式
154155
if singleton.Conf.DatabaseType == "badger" {
155-
// BadgerDB 模式下使用 MonitorAPI,只查询最近7天的ICMP/TCP监控数据
156-
if singleton.MonitorAPI != nil {
157-
// 查询最近7天的监控历史记录
156+
if db.DB != nil {
157+
// 查询最近3天的监控历史记录(减少数据量)
158158
endTime := time.Now()
159-
startTime := endTime.AddDate(0, 0, -7) // 显示7天数据
159+
startTime := endTime.AddDate(0, 0, -3) // 改为3天数据,减少查询量
160160

161-
if db.DB != nil {
162-
// 恢复原始的查询方法
163-
monitorOps := db.NewMonitorHistoryOps(db.DB)
164-
allHistories, err := monitorOps.GetAllMonitorHistoriesInRange(startTime, endTime)
165-
if err != nil {
166-
log.Printf("查询网络监控历史记录失败: %v", err)
167-
c.JSON(200, []any{})
168-
return
169-
}
170-
171-
// 恢复原始的过滤逻辑
172-
monitors := singleton.ServiceSentinelShared.Monitors()
173-
monitorTypeMap := make(map[uint64]uint8)
174-
if monitors != nil {
175-
for _, monitor := range monitors {
176-
monitorTypeMap[monitor.ID] = monitor.Type
177-
}
178-
}
161+
// 获取该服务器的监控配置,避免查询所有数据
162+
monitors := singleton.ServiceSentinelShared.Monitors()
163+
var networkHistories []*model.MonitorHistory
179164

180-
// 修复:正确的记录过滤,不限制总数,应该返回所有匹配的记录
181-
var networkHistories []*model.MonitorHistory
165+
if monitors != nil {
166+
monitorOps := db.NewMonitorHistoryOps(db.DB)
182167

183-
for _, history := range allHistories {
184-
if history != nil && history.ServerID == server.ID {
185-
if monitorType, exists := monitorTypeMap[history.MonitorID]; exists &&
186-
(monitorType == model.TaskTypeICMPPing || monitorType == model.TaskTypeTCPPing) {
187-
networkHistories = append(networkHistories, history)
168+
// 针对每个ICMP/TCP监控器单独查询,避免全表扫描
169+
for _, monitor := range monitors {
170+
if monitor.Type == model.TaskTypeICMPPing || monitor.Type == model.TaskTypeTCPPing {
171+
// 使用更高效的单监控器查询方法,限制返回数量
172+
histories, err := monitorOps.GetMonitorHistoriesByServerAndMonitor(
173+
server.ID, monitor.ID, startTime, endTime, 500) // 限制每个监控器最多500条记录
174+
if err != nil {
175+
log.Printf("查询监控器 %d 的历史记录失败: %v", monitor.ID, err)
176+
continue
188177
}
178+
networkHistories = append(networkHistories, histories...)
189179
}
190180
}
181+
}
191182

192-
c.JSON(200, networkHistories)
193-
} else {
194-
c.JSON(200, []any{})
183+
// 按时间排序并限制总数量,防止内存溢出
184+
sort.Slice(networkHistories, func(i, j int) bool {
185+
return networkHistories[i].CreatedAt.After(networkHistories[j].CreatedAt)
186+
})
187+
188+
// 限制最大返回数量,防止内存问题
189+
maxRecords := 2000
190+
if len(networkHistories) > maxRecords {
191+
networkHistories = networkHistories[:maxRecords]
195192
}
193+
194+
c.JSON(200, networkHistories)
196195
} else {
197196
c.JSON(200, []any{})
198197
}

service/singleton/api.go

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -457,26 +457,26 @@ func (m *MonitorAPIService) GetMonitorHistories(search map[string]any) []*model.
457457
return make([]*model.MonitorHistory, 0)
458458
}
459459

460-
// 获取时间范围(默认最近30天
460+
// 性能优化:获取时间范围(减少到最近3天
461461
endTime := time.Now()
462-
startTime := endTime.AddDate(0, 0, -30)
462+
startTime := endTime.AddDate(0, 0, -3)
463463

464-
// 从BadgerDB查询监控历史记录
465-
// 注意:这里需要查询所有监控历史记录,然后按server_id过滤
464+
// 使用更高效的查询方法,避免全表扫描
466465
monitorOps := db.NewMonitorHistoryOps(db.DB)
467466

468-
// 获取所有监控历史记录并过滤
469-
allHistories, err := monitorOps.GetAllMonitorHistoriesInRange(startTime, endTime)
470-
if err != nil {
471-
log.Printf("MonitorAPIService.GetMonitorHistories: 从BadgerDB查询失败: %v", err)
472-
return make([]*model.MonitorHistory, 0)
473-
}
474-
475-
// 过滤出指定服务器的记录
467+
// 获取该服务器相关的监控器ID列表,针对性查询
476468
var histories []*model.MonitorHistory
477-
for _, history := range allHistories {
478-
if history.ServerID == serverID {
479-
histories = append(histories, history)
469+
if ServiceSentinelShared != nil {
470+
monitors := ServiceSentinelShared.Monitors()
471+
for _, monitor := range monitors {
472+
// 使用针对性查询,限制每个监控器的记录数量
473+
monitorHistories, err := monitorOps.GetMonitorHistoriesByServerAndMonitor(
474+
serverID, monitor.ID, startTime, endTime, 500) // 限制每个监控器最多500条
475+
if err != nil {
476+
log.Printf("MonitorAPIService.GetMonitorHistories: 查询监控器 %d 失败: %v", monitor.ID, err)
477+
continue
478+
}
479+
histories = append(histories, monitorHistories...)
480480
}
481481
}
482482

@@ -495,50 +495,8 @@ func (m *MonitorAPIService) GetMonitorHistories(search map[string]any) []*model.
495495
return make([]*model.MonitorHistory, 0)
496496
}
497497

498-
// 根据数据库类型选择不同的查询方式
499-
if Conf.DatabaseType == "badger" {
500-
// 使用BadgerDB查询监控历史记录
501-
if db.DB != nil {
502-
// 获取服务器ID
503-
var serverID uint64
504-
if sid, ok := search["server_id"]; ok {
505-
if sidVal, ok := sid.(uint64); ok {
506-
serverID = sidVal
507-
}
508-
}
509-
510-
if serverID == 0 {
511-
log.Printf("MonitorAPIService.GetMonitorHistories: 无效的服务器ID")
512-
return make([]*model.MonitorHistory, 0)
513-
}
514-
515-
// 获取时间范围(默认最近30天)
516-
endTime := time.Now()
517-
startTime := endTime.AddDate(0, 0, -30)
518-
519-
// 从BadgerDB查询监控历史记录
520-
monitorOps := db.NewMonitorHistoryOps(db.DB)
521-
522-
// 获取所有监控历史记录并过滤
523-
allHistories, err := monitorOps.GetAllMonitorHistoriesInRange(startTime, endTime)
524-
if err != nil {
525-
log.Printf("MonitorAPIService.GetMonitorHistories: 从BadgerDB查询失败: %v", err)
526-
return make([]*model.MonitorHistory, 0)
527-
}
528-
529-
// 过滤出指定服务器的记录
530-
var filteredHistories []*model.MonitorHistory
531-
for _, h := range allHistories {
532-
if h != nil && h.ServerID == serverID {
533-
filteredHistories = append(filteredHistories, h)
534-
}
535-
}
536-
537-
return filteredHistories
538-
} else {
539-
return make([]*model.MonitorHistory, 0)
540-
}
541-
} else if DB != nil {
498+
// SQLite模式查询
499+
if DB != nil {
542500
// SQLite模式
543501
if err := DB.Model(&model.MonitorHistory{}).
544502
Where(search).

0 commit comments

Comments
 (0)