Skip to content

Commit 2453dca

Browse files
committed
Update.
1 parent eafc679 commit 2453dca

File tree

2 files changed

+81
-29
lines changed

2 files changed

+81
-29
lines changed

cmd/dashboard/controller/api_v1.go

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,51 +154,42 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
154154
if singleton.Conf.DatabaseType == "badger" {
155155
// BadgerDB 模式下使用 MonitorAPI,只查询最近7天的ICMP/TCP监控数据
156156
if singleton.MonitorAPI != nil {
157-
// 极致性能优化:只获取最近1天的数据,大幅提高性能
157+
// 根本性性能优化:恢复3天数据,但优化查询方式
158158
endTime := time.Now()
159-
startTime := endTime.AddDate(0, 0, -1) // 从3天减少到1天
159+
startTime := endTime.AddDate(0, 0, -3) // 恢复3天数据
160160

161161
if db.DB != nil {
162+
// 性能优化:直接按服务器ID和时间范围查询,避免查询所有记录
162163
monitorOps := db.NewMonitorHistoryOps(db.DB)
163-
allHistories, err := monitorOps.GetAllMonitorHistoriesInRange(startTime, endTime)
164-
if err != nil {
165-
log.Printf("查询网络监控历史记录失败: %v", err)
166-
c.JSON(200, []any{})
167-
return
168-
}
169164

170-
// 性能优化:预先获取监控配置,避免重复查询
165+
// 预先获取监控配置
171166
monitors := singleton.ServiceSentinelShared.Monitors()
172167
monitorTypeMap := make(map[uint64]uint8)
168+
icmpTcpMonitorIDs := []uint64{}
173169
if monitors != nil {
174170
for _, monitor := range monitors {
175171
monitorTypeMap[monitor.ID] = monitor.Type
172+
if monitor.Type == model.TaskTypeICMPPing || monitor.Type == model.TaskTypeTCPPing {
173+
icmpTcpMonitorIDs = append(icmpTcpMonitorIDs, monitor.ID)
174+
}
176175
}
177176
}
178177

179-
// 过滤出ICMP和TCP监控记录,限制数量
180-
var networkHistories []*model.MonitorHistory
181-
count := 0
182-
maxRecords := 100 // 大幅减少记录数,提高性能
178+
log.Printf("服务器 %d 的ICMP/TCP监控器ID列表: %v", server.ID, icmpTcpMonitorIDs)
183179

184-
log.Printf("开始过滤服务器 %d 的监控历史记录,总记录数: %d", server.ID, len(allHistories))
180+
// 性能优化:直接查询指定服务器和监控器的记录
181+
var networkHistories []*model.MonitorHistory
185182

186-
for _, history := range allHistories {
187-
if count >= maxRecords {
188-
break
189-
}
190-
if history != nil && history.ServerID == server.ID {
191-
if monitorType, exists := monitorTypeMap[history.MonitorID]; exists &&
192-
(monitorType == model.TaskTypeICMPPing || monitorType == model.TaskTypeTCPPing) {
193-
log.Printf("找到ICMP/TCP监控记录: MonitorID=%d, ServerID=%d, Type=%d",
194-
history.MonitorID, history.ServerID, monitorType)
195-
networkHistories = append(networkHistories, history)
196-
count++
197-
} else {
198-
log.Printf("跳过非ICMP/TCP监控记录: MonitorID=%d, ServerID=%d, Type=%d",
199-
history.MonitorID, history.ServerID, monitorType)
200-
}
183+
for _, monitorID := range icmpTcpMonitorIDs {
184+
// 为每个监控器单独查询,避免全表扫描
185+
histories, err := monitorOps.GetMonitorHistoriesByServerAndMonitor(server.ID, monitorID, startTime, endTime, 200)
186+
if err != nil {
187+
log.Printf("查询监控器 %d 的历史记录失败: %v", monitorID, err)
188+
continue
201189
}
190+
191+
log.Printf("监控器 %d 在服务器 %d 上找到 %d 条记录", monitorID, server.ID, len(histories))
192+
networkHistories = append(networkHistories, histories...)
202193
}
203194

204195
log.Printf("服务器 %d 最终返回 %d 条ICMP/TCP监控记录", server.ID, len(networkHistories))

db/model_ops.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"log"
8+
"sort"
89
"strconv"
910
"time"
1011

@@ -193,6 +194,66 @@ func (o *MonitorHistoryOps) GetAllMonitorHistoriesInRange(startTime, endTime tim
193194
return histories, err
194195
}
195196

197+
// GetMonitorHistoriesByServerAndMonitor gets monitor histories for specific server and monitor within time range
198+
func (o *MonitorHistoryOps) GetMonitorHistoriesByServerAndMonitor(serverID, monitorID uint64, startTime, endTime time.Time, limit int) ([]*model.MonitorHistory, error) {
199+
var histories []*model.MonitorHistory
200+
prefix := "monitor_history:"
201+
202+
err := o.db.db.View(func(txn *badger.Txn) error {
203+
opts := badger.DefaultIteratorOptions
204+
opts.PrefetchValues = true
205+
opts.PrefetchSize = 100
206+
it := txn.NewIterator(opts)
207+
defer it.Close()
208+
209+
count := 0
210+
for it.Seek([]byte(prefix)); it.Valid() && count < limit; it.Next() {
211+
item := it.Item()
212+
key := item.Key()
213+
214+
// 检查key是否以prefix开头
215+
if !bytes.HasPrefix(key, []byte(prefix)) {
216+
break
217+
}
218+
219+
err := item.Value(func(val []byte) error {
220+
var history model.MonitorHistory
221+
if err := json.Unmarshal(val, &history); err != nil {
222+
return err
223+
}
224+
225+
// 过滤条件:服务器ID、监控器ID、时间范围
226+
if history.ServerID == serverID &&
227+
history.MonitorID == monitorID &&
228+
history.CreatedAt.After(startTime) &&
229+
history.CreatedAt.Before(endTime) {
230+
histories = append(histories, &history)
231+
count++
232+
}
233+
234+
return nil
235+
})
236+
237+
if err != nil {
238+
return err
239+
}
240+
}
241+
242+
return nil
243+
})
244+
245+
if err != nil {
246+
return nil, fmt.Errorf("failed to query monitor histories: %w", err)
247+
}
248+
249+
// 按时间排序(最新的在前)
250+
sort.Slice(histories, func(i, j int) bool {
251+
return histories[i].CreatedAt.After(histories[j].CreatedAt)
252+
})
253+
254+
return histories, nil
255+
}
256+
196257
// CleanupOldMonitorHistories removes monitor histories older than maxAge
197258
func (o *MonitorHistoryOps) CleanupOldMonitorHistories(maxAge time.Duration) (int, error) {
198259
// Get all monitor IDs

0 commit comments

Comments
 (0)