Skip to content

Commit 5135812

Browse files
committed
Update.
1 parent 638b58a commit 5135812

File tree

2 files changed

+50
-78
lines changed

2 files changed

+50
-78
lines changed

db/model_ops.go

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -262,80 +262,64 @@ func (o *MonitorHistoryOps) GetMonitorHistoriesByServerAndMonitorOptimized(serve
262262
// BadgerDB中的key格式通常是 "monitor_history:timestamp:serverid:monitorid" 或类似格式
263263
// 我们需要找到最优的扫描策略
264264

265+
// 超级优化:使用采样策略,大幅减少扫描量
265266
err := o.db.db.View(func(txn *badger.Txn) error {
266267
opts := badger.DefaultIteratorOptions
267-
opts.PrefetchValues = false // 先不预取值,只扫描key
268-
opts.PrefetchSize = 1000
268+
opts.PrefetchValues = true // 启用预取,但限制大小
269+
opts.PrefetchSize = 50 // 大幅减少预取大小
269270
it := txn.NewIterator(opts)
270271
defer it.Close()
271272

272273
prefix := "monitor_history:"
273274
count := 0
275+
maxScan := limit * 3 // 限制最大扫描数量
276+
scanCount := 0
274277

275-
// 第一阶段:快速扫描key,过滤出匹配的记录
276-
var matchingKeys [][]byte
278+
// 使用采样策略:每隔几条记录才检查一次
279+
sampleRate := 5 // 每5条记录检查一次
280+
sampleCounter := 0
281+
282+
for it.Seek([]byte(prefix)); it.ValidForPrefix([]byte(prefix)) && count < limit && scanCount < maxScan; it.Next() {
283+
scanCount++
284+
sampleCounter++
285+
286+
// 采样策略:不是每条记录都检查
287+
if sampleCounter%sampleRate != 0 {
288+
continue
289+
}
277290

278-
for it.Seek([]byte(prefix)); it.ValidForPrefix([]byte(prefix)) && count < limit*2; it.Next() {
279291
item := it.Item()
280-
key := item.Key()
281292

282-
// 快速检查:先获取值来判断是否匹配条件
283293
err := item.Value(func(val []byte) error {
284-
// 快速解析:只解析必要的字段
294+
// 超快速解析:使用更简单的结构
285295
var quickCheck struct {
286-
ServerID uint64 `json:"ServerID"`
287-
MonitorID uint64 `json:"MonitorID"`
288-
CreatedAt time.Time `json:"CreatedAt"`
296+
ServerID uint64 `json:"ServerID"`
297+
MonitorID uint64 `json:"MonitorID"`
289298
}
290299

291-
if err := json.Unmarshal(val, &quickCheck); err != nil {
292-
return nil // 跳过无效记录
300+
// 只解析前面的字段,减少JSON解析时间
301+
parseLen := len(val)
302+
if parseLen > 200 {
303+
parseLen = 200
293304
}
294-
295-
// 快速过滤
296-
if quickCheck.ServerID == serverID &&
297-
quickCheck.MonitorID == monitorID &&
298-
quickCheck.CreatedAt.After(startTime) &&
299-
quickCheck.CreatedAt.Before(endTime) {
300-
// 复制key以避免BadgerDB的内存重用问题
301-
keyCopy := make([]byte, len(key))
302-
copy(keyCopy, key)
303-
matchingKeys = append(matchingKeys, keyCopy)
304-
count++
305-
}
306-
307-
return nil
308-
})
309-
310-
if err != nil {
311-
return err
312-
}
313-
}
314-
315-
// 第二阶段:只处理匹配的记录,按时间排序
316-
type recordWithTime struct {
317-
history *model.MonitorHistory
318-
time time.Time
319-
}
320-
321-
var records []recordWithTime
322-
323-
for _, key := range matchingKeys {
324-
item, err := txn.Get(key)
325-
if err != nil {
326-
continue
327-
}
328-
329-
err = item.Value(func(val []byte) error {
330-
var history model.MonitorHistory
331-
if err := json.Unmarshal(val, &history); err != nil {
305+
if err := json.Unmarshal(val[:parseLen], &quickCheck); err != nil {
332306
return nil
333307
}
334308

335-
records = append(records, recordWithTime{
336-
history: &history,
337-
time: history.CreatedAt,
338-
})
309+
// 快速过滤:只检查ID匹配
310+
if quickCheck.ServerID == serverID && quickCheck.MonitorID == monitorID {
311+
// 完整解析
312+
var history model.MonitorHistory
313+
if err := json.Unmarshal(val, &history); err != nil {
314+
return nil
315+
}
316+
317+
// 时间过滤
318+
if history.CreatedAt.After(startTime) && history.CreatedAt.Before(endTime) {
319+
histories = append(histories, &history)
320+
count++
321+
}
322+
}
339323

340324
return nil
341325
})
@@ -345,28 +329,23 @@ func (o *MonitorHistoryOps) GetMonitorHistoriesByServerAndMonitorOptimized(serve
345329
}
346330
}
347331

348-
// 按时间排序(最新的在前)
349-
sort.Slice(records, func(i, j int) bool {
350-
return records[i].time.After(records[j].time)
351-
})
352-
353-
// 限制结果数量
354-
maxResults := limit
355-
if len(records) < maxResults {
356-
maxResults = len(records)
357-
}
358-
359-
for i := 0; i < maxResults; i++ {
360-
histories = append(histories, records[i].history)
361-
}
362-
363332
return nil
364333
})
365334

366335
if err != nil {
367336
return nil, fmt.Errorf("failed to query monitor histories optimized: %w", err)
368337
}
369338

339+
// 按时间排序(最新的在前)
340+
sort.Slice(histories, func(i, j int) bool {
341+
return histories[i].CreatedAt.After(histories[j].CreatedAt)
342+
})
343+
344+
// 限制结果数量
345+
if len(histories) > limit {
346+
histories = histories[:limit]
347+
}
348+
370349
return histories, nil
371350
}
372351

resource/template/theme-default/network.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,11 @@
431431
});
432432
},
433433
getMonitorName(monitorId) {
434-
// 调试:输出监控配置信息
435-
console.log('获取监控名称 - ID:', monitorId);
436-
console.log('监控配置:', monitorConfigs);
437-
console.log('配置已加载:', this.monitorConfigsLoaded);
438-
439-
// 从监控配置中获取名称
434+
// 恢复使用后台配置的监控名称
440435
if (monitorConfigs[monitorId] && monitorConfigs[monitorId].Name) {
441-
console.log('找到监控配置:', monitorConfigs[monitorId].Name);
442436
return monitorConfigs[monitorId].Name;
443437
}
444438
// 如果没有配置,返回默认名称
445-
console.log('使用默认名称: Monitor', monitorId);
446439
return `Monitor ${monitorId}`;
447440
},
448441
parseMonitorInfo(monitorInfo) {

0 commit comments

Comments
 (0)