Skip to content

Commit abcdc72

Browse files
committed
Update.
1 parent ddc2253 commit abcdc72

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

cmd/dashboard/controller/common_page.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"golang.org/x/crypto/bcrypt"
2222
"golang.org/x/sync/singleflight"
2323

24+
"github.com/xos/serverstatus/db"
2425
"github.com/xos/serverstatus/model"
2526
"github.com/xos/serverstatus/pkg/mygin"
2627
"github.com/xos/serverstatus/pkg/utils"
@@ -337,10 +338,77 @@ func (cp *commonPage) network(c *gin.Context) {
337338
var monitorHistories interface{}
338339
if singleton.Conf.DatabaseType == "badger" {
339340
// BadgerDB 模式,查询监控历史记录
341+
// 性能优化:只查询默认服务器的数据,其他服务器数据通过AJAX异步加载
342+
if id > 0 {
343+
// 使用高效的API查询方法,而不是全表扫描
344+
endTime := time.Now()
345+
startTime := endTime.AddDate(0, 0, -3) // 3天数据
340346

341-
// 性能优化:不在network页面加载时查询历史数据,改为异步加载
342-
// 这样可以让页面快速渲染,数据通过AJAX异步获取
343-
monitorHistories = []model.MonitorHistory{}
347+
// 获取该服务器的监控配置,只查询ICMP/TCP类型
348+
monitors := singleton.ServiceSentinelShared.Monitors()
349+
var networkHistories []*model.MonitorHistory
350+
351+
if monitors != nil {
352+
// 使用我们之前优化的并发查询方法
353+
type monitorResult struct {
354+
histories []*model.MonitorHistory
355+
err error
356+
}
357+
358+
// 创建通道收集结果
359+
resultChan := make(chan monitorResult, len(monitors))
360+
activeQueries := 0
361+
362+
// 并发查询所有ICMP/TCP监控器
363+
for _, monitor := range monitors {
364+
if monitor.Type == model.TaskTypeICMPPing || monitor.Type == model.TaskTypeTCPPing {
365+
activeQueries++
366+
go func(monitorID uint64) {
367+
// 使用高效的时间范围查询
368+
monitorOps := db.NewMonitorHistoryOps(db.DB)
369+
allHistories, err := monitorOps.GetMonitorHistoriesByMonitorID(
370+
monitorID, startTime, endTime)
371+
372+
var serverHistories []*model.MonitorHistory
373+
if err == nil {
374+
// 快速过滤出该服务器的记录
375+
for _, history := range allHistories {
376+
if history.ServerID == id {
377+
serverHistories = append(serverHistories, history)
378+
}
379+
}
380+
}
381+
382+
resultChan <- monitorResult{
383+
histories: serverHistories,
384+
err: err,
385+
}
386+
}(monitor.ID)
387+
}
388+
}
389+
390+
// 收集所有并发查询结果
391+
for i := 0; i < activeQueries; i++ {
392+
result := <-resultChan
393+
if result.err != nil {
394+
log.Printf("并发查询监控历史记录失败: %v", result.err)
395+
continue
396+
}
397+
networkHistories = append(networkHistories, result.histories...)
398+
}
399+
}
400+
401+
// 转换为[]model.MonitorHistory格式
402+
var filteredHistories []model.MonitorHistory
403+
for _, h := range networkHistories {
404+
if h != nil {
405+
filteredHistories = append(filteredHistories, *h)
406+
}
407+
}
408+
monitorHistories = filteredHistories
409+
} else {
410+
monitorHistories = []model.MonitorHistory{}
411+
}
344412

345413
// 序列化为JSON
346414
var err error

0 commit comments

Comments
 (0)