Skip to content

Commit af0a8d8

Browse files
committed
Update.
1 parent d92baff commit af0a8d8

File tree

5 files changed

+871
-573
lines changed

5 files changed

+871
-573
lines changed

cmd/dashboard/controller/api_v1.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,23 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
151151
return
152152
}
153153

154+
// 解析时间范围参数,默认72小时=3天(向后兼容原逻辑)
155+
rangeParam := strings.ToLower(strings.TrimSpace(c.Query("range")))
156+
// 默认3天
157+
duration := 72 * time.Hour
158+
switch rangeParam {
159+
case "24h", "24", "1d":
160+
duration = 24 * time.Hour
161+
case "72h", "72", "3d", "":
162+
duration = 72 * time.Hour
163+
}
164+
154165
// 根本性能优化:使用正确的高效查询方法,利用BadgerDB的时间索引
155166
if singleton.Conf.DatabaseType == "badger" {
156167
if db.DB != nil {
157-
// 恢复3天数据展示,使用高效查询方法
168+
// 使用range参数决定时间范围
158169
endTime := time.Now()
159-
startTime := endTime.AddDate(0, 0, -3)
170+
startTime := endTime.Add(-duration)
160171

161172
// 获取该服务器的监控配置,展示所有监控器
162173
monitors := singleton.ServiceSentinelShared.Monitors()
@@ -218,7 +229,7 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
218229
return networkHistories[i].CreatedAt.After(networkHistories[j].CreatedAt)
219230
})
220231

221-
log.Printf("API /monitor/%d 返回 %d 条记录(3天数据,所有监控器)", server.ID, len(networkHistories))
232+
log.Printf("API /monitor/%d 返回 %d 条记录(范围: %v,所有监控器)", server.ID, len(networkHistories), duration)
222233
c.JSON(200, networkHistories)
223234
} else {
224235
c.JSON(200, []any{})
@@ -228,8 +239,8 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
228239
if singleton.DB != nil {
229240
var networkHistories []*model.MonitorHistory
230241

231-
// 查询最近7天的数据
232-
startTime := time.Now().AddDate(0, 0, -7)
242+
// 使用range参数决定时间范围(与Badger保持一致)
243+
startTime := time.Now().Add(-duration)
233244

234245
err := singleton.DB.Where("server_id = ? AND created_at > ? AND monitor_id IN (SELECT id FROM monitors WHERE type IN (?, ?))",
235246
server.ID, startTime, model.TaskTypeICMPPing, model.TaskTypeTCPPing).

model/alertrule.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,17 @@ func (r *AlertRule) Enabled() bool {
7979

8080
// Snapshot 对传入的Server进行该通知规则下所有type的检查 返回包含每项检查结果的空接口
8181
func (r *AlertRule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, db *gorm.DB) []interface{} {
82+
// 安全检查:确保AlertRule和Rules不为nil
83+
if r == nil || r.Rules == nil {
84+
return nil
85+
}
86+
8287
var point []interface{}
8388
for i := 0; i < len(r.Rules); i++ {
84-
point = append(point, r.Rules[i].Snapshot(cycleTransferStats, server, db))
89+
// 安全检查:确保单个Rule不为nil
90+
if r.Rules[i] != nil {
91+
point = append(point, r.Rules[i].Snapshot(cycleTransferStats, server, db))
92+
}
8593
}
8694
return point
8795
}

model/rule.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ func percentage(used, total uint64) float64 {
9898

9999
// Snapshot 未通过规则返回 struct{}{}, 通过返回 nil
100100
func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, db *gorm.DB) interface{} {
101+
// 安全检查:确保server不为nil
102+
if server == nil {
103+
return nil
104+
}
105+
101106
// 监控全部但是排除了此服务器
102107
if u.Cover == RuleCoverAll && u.Ignore[server.ID] {
103108
return nil
@@ -106,6 +111,16 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
106111
if u.Cover == RuleCoverIgnoreAll && !u.Ignore[server.ID] {
107112
return nil
108113
}
114+
115+
// 安全检查:确保server.State不为nil(除了offline类型的规则)
116+
if server.State == nil && u.Type != "offline" {
117+
return nil
118+
}
119+
120+
// 安全检查:确保server.Host不为nil(对于需要Host信息的规则)
121+
if server.Host == nil && (u.Type == "memory" || u.Type == "swap" || u.Type == "disk") {
122+
return nil
123+
}
109124

110125
// 循环区间流量检测 · 优化检测频率,确保超限时能及时检测
111126
if u.IsTransferDurationRule() && u.NextTransferAt[server.ID].After(time.Now()) {
@@ -252,14 +267,17 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
252267
} else {
253268
u.LastCycleStatus[server.ID] = nil
254269
}
255-
if cycleTransferStats.ServerName[server.ID] != server.Name {
256-
cycleTransferStats.ServerName[server.ID] = server.Name
270+
// 安全检查:确保cycleTransferStats不为nil
271+
if cycleTransferStats != nil {
272+
if cycleTransferStats.ServerName[server.ID] != server.Name {
273+
cycleTransferStats.ServerName[server.ID] = server.Name
274+
}
275+
cycleTransferStats.Transfer[server.ID] = uint64(src)
276+
cycleTransferStats.NextUpdate[server.ID] = u.NextTransferAt[server.ID]
277+
// 自动更新周期流量展示起止时间
278+
cycleTransferStats.From = u.GetTransferDurationStart()
279+
cycleTransferStats.To = u.GetTransferDurationEnd()
257280
}
258-
cycleTransferStats.Transfer[server.ID] = uint64(src)
259-
cycleTransferStats.NextUpdate[server.ID] = u.NextTransferAt[server.ID]
260-
// 自动更新周期流量展示起止时间
261-
cycleTransferStats.From = u.GetTransferDurationStart()
262-
cycleTransferStats.To = u.GetTransferDurationEnd()
263281
}
264282

265283
if u.Type == "offline" {

0 commit comments

Comments
 (0)