Skip to content

Commit cbdbfb9

Browse files
committed
Update.
1 parent 261be1e commit cbdbfb9

File tree

2 files changed

+27
-107
lines changed

2 files changed

+27
-107
lines changed

cmd/dashboard/controller/api_v1.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,50 +154,48 @@ 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-
// 恢复3天数据展示,通过优化查询效率解决性能问题
157+
// 恢复原始查询逻辑:查询所有监控历史记录
158158
endTime := time.Now()
159-
startTime := endTime.AddDate(0, 0, -3) // 恢复3天数据
159+
startTime := endTime.AddDate(0, 0, -7) // 恢复原始的7天数据
160160

161161
if db.DB != nil {
162-
// 性能优化:直接按服务器ID和时间范围查询,避免查询所有记录
162+
// 恢复原始的查询方法
163163
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+
}
164170

165-
// 预先获取监控配置
171+
// 恢复原始的过滤逻辑
166172
monitors := singleton.ServiceSentinelShared.Monitors()
167173
monitorTypeMap := make(map[uint64]uint8)
168-
icmpTcpMonitorIDs := []uint64{}
169174
if monitors != nil {
170175
for _, monitor := range monitors {
171176
monitorTypeMap[monitor.ID] = monitor.Type
172-
if monitor.Type == model.TaskTypeICMPPing || monitor.Type == model.TaskTypeTCPPing {
173-
icmpTcpMonitorIDs = append(icmpTcpMonitorIDs, monitor.ID)
174-
}
175177
}
176178
}
177179

178-
log.Printf("服务器 %d 的ICMP/TCP监控器ID列表: %v", server.ID, icmpTcpMonitorIDs)
179-
180-
// 根本性能优化:使用高效的数据库查询,恢复合理的数据量
180+
// 恢复原始的记录过滤
181181
var networkHistories []*model.MonitorHistory
182+
count := 0
183+
maxRecords := 1000 // 恢复原始的记录数限制
182184

183-
for _, monitorID := range icmpTcpMonitorIDs {
184-
// 使用优化的查询方法,每个监控器200条记录(3天数据)
185-
histories, err := monitorOps.GetMonitorHistoriesByServerAndMonitorOptimized(server.ID, monitorID, startTime, endTime, 200)
186-
if err != nil {
187-
log.Printf("查询监控器 %d 的历史记录失败: %v", monitorID, err)
188-
continue
185+
for _, history := range allHistories {
186+
if count >= maxRecords {
187+
break
188+
}
189+
if history != nil && history.ServerID == server.ID {
190+
if monitorType, exists := monitorTypeMap[history.MonitorID]; exists &&
191+
(monitorType == model.TaskTypeICMPPing || monitorType == model.TaskTypeTCPPing) {
192+
networkHistories = append(networkHistories, history)
193+
count++
194+
}
189195
}
190-
191-
log.Printf("监控器 %d 在服务器 %d 上找到 %d 条记录", monitorID, server.ID, len(histories))
192-
networkHistories = append(networkHistories, histories...)
193196
}
194197

195198
log.Printf("服务器 %d 最终返回 %d 条ICMP/TCP监控记录", server.ID, len(networkHistories))
196-
197-
// 确保返回空数组而不是null
198-
if networkHistories == nil {
199-
networkHistories = []*model.MonitorHistory{}
200-
}
201199
c.JSON(200, networkHistories)
202200
} else {
203201
c.JSON(200, []any{})
@@ -206,17 +204,16 @@ func (v *apiV1) monitorHistoriesById(c *gin.Context) {
206204
c.JSON(200, []any{})
207205
}
208206
} else {
209-
// SQLite 模式下直接查询ICMP/TCP监控数据,限制时间范围和数量
207+
// SQLite 模式下恢复原始查询逻辑
210208
if singleton.DB != nil {
211209
var networkHistories []*model.MonitorHistory
212210

213-
// 性能优化:只查询最近7天的数据
214-
startTime := time.Now().AddDate(0, 0, -7)
211+
// 恢复原始的30天数据查询
212+
startTime := time.Now().AddDate(0, 0, -30)
215213

216214
err := singleton.DB.Where("server_id = ? AND created_at > ? AND monitor_id IN (SELECT id FROM monitors WHERE type IN (?, ?))",
217215
server.ID, startTime, model.TaskTypeICMPPing, model.TaskTypeTCPPing).
218216
Order("created_at DESC").
219-
Limit(500). // 减少到500条记录
220217
Find(&networkHistories).Error
221218

222219
if err != nil {

db/model_ops.go

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -254,84 +254,7 @@ func (o *MonitorHistoryOps) GetMonitorHistoriesByServerAndMonitor(serverID, moni
254254
return histories, nil
255255
}
256256

257-
// GetMonitorHistoriesByServerAndMonitorOptimized 高效查询特定服务器和监控器的历史记录
258-
func (o *MonitorHistoryOps) GetMonitorHistoriesByServerAndMonitorOptimized(serverID, monitorID uint64, startTime, endTime time.Time, limit int) ([]*model.MonitorHistory, error) {
259-
var histories []*model.MonitorHistory
260-
261-
// 关键优化:使用更精确的key前缀,减少扫描范围
262-
// BadgerDB中的key格式通常是 "monitor_history:timestamp:serverid:monitorid" 或类似格式
263-
// 我们需要找到最优的扫描策略
264-
265-
// 修复采样策略:确保不丢失数据
266-
err := o.db.db.View(func(txn *badger.Txn) error {
267-
opts := badger.DefaultIteratorOptions
268-
opts.PrefetchValues = true
269-
opts.PrefetchSize = 100
270-
it := txn.NewIterator(opts)
271-
defer it.Close()
272-
273-
prefix := "monitor_history:"
274-
count := 0
275-
maxScan := limit * 10 // 增加扫描范围,确保找到数据
276-
277-
for it.Seek([]byte(prefix)); it.ValidForPrefix([]byte(prefix)) && count < limit && maxScan > 0; it.Next() {
278-
maxScan--
279-
item := it.Item()
280-
281-
err := item.Value(func(val []byte) error {
282-
// 快速解析:只解析必要字段
283-
var quickCheck struct {
284-
ServerID uint64 `json:"ServerID"`
285-
MonitorID uint64 `json:"MonitorID"`
286-
CreatedAt time.Time `json:"CreatedAt"`
287-
}
288-
289-
if err := json.Unmarshal(val, &quickCheck); err != nil {
290-
return nil
291-
}
292-
293-
// 精确匹配
294-
if quickCheck.ServerID == serverID &&
295-
quickCheck.MonitorID == monitorID &&
296-
quickCheck.CreatedAt.After(startTime) &&
297-
quickCheck.CreatedAt.Before(endTime) {
298-
// 完整解析
299-
var history model.MonitorHistory
300-
if err := json.Unmarshal(val, &history); err != nil {
301-
return nil
302-
}
303-
304-
histories = append(histories, &history)
305-
count++
306-
}
307-
308-
return nil
309-
})
310-
311-
if err != nil {
312-
return err
313-
}
314-
}
315-
316-
return nil
317-
})
318-
319-
if err != nil {
320-
return nil, fmt.Errorf("failed to query monitor histories optimized: %w", err)
321-
}
322-
323-
// 按时间排序(最新的在前)
324-
sort.Slice(histories, func(i, j int) bool {
325-
return histories[i].CreatedAt.After(histories[j].CreatedAt)
326-
})
327-
328-
// 限制结果数量
329-
if len(histories) > limit {
330-
histories = histories[:limit]
331-
}
332-
333-
return histories, nil
334-
}
257+
// 删除有问题的优化方法,恢复使用原始的GetMonitorHistoriesByServerAndMonitor方法
335258

336259
// CleanupOldMonitorHistories removes monitor histories older than maxAge
337260
func (o *MonitorHistoryOps) CleanupOldMonitorHistories(maxAge time.Duration) (int, error) {

0 commit comments

Comments
 (0)