goProject/trunk/framework/monitorNewMgr/monitor_history.go

64 lines
1.9 KiB
Go
Raw Normal View History

2025-01-06 16:01:02 +08:00
package monitorNewMgr
import (
"sync"
"time"
"goutil/stringUtil"
)
const (
// CON_MONITOR_HISTORY_SIZE 历史监控信息保存数量
CON_MONITOR_HISTORY_SIZE = 3
// CON_MONITOR_HISTORY_VALID_SECONDS 历史监控信息有效的秒数
CON_MONITOR_HISTORY_VALID_SECONDS = 30
// CON_MONITOR_HISTORY_SIMILARITY_THERSHOLD 历史监控信息相似度的阈值
CON_MONITOR_HISTORY_SIMILARITY_THERSHOLD = 0.9
)
var (
monitorHistoryList = make([]*MonitorHistory, 0, CON_MONITOR_HISTORY_SIZE)
monitorHistoryMutex sync.Mutex
)
func checkSimilarity(monitorMessage string, timestamp int64) (valid bool) {
//默认监控信息有效
valid = true
monitorHistoryMutex.Lock()
defer monitorHistoryMutex.Unlock()
// 从后往前(按时间从后往前)遍历,以便于可以及时退出
for i := len(monitorHistoryList) - 1; i >= 0; i-- {
item := monitorHistoryList[i]
// 如果已经过期,则不用处理; 返回之前的状态即可(当前的已经过期,则之前的也一定已经过期)
if time.Now().Unix()-item.Timestamp > CON_MONITOR_HISTORY_VALID_SECONDS {
break
}
// 如果两个字符串的长度相差2倍则无须继续判断继续与下一条数据进行比较
lenA, lenB := len(monitorMessage), len(item.MonitorMessage)
if lenA > 2*lenB || lenB > 2*lenA {
continue
}
// 判断两个字符串的相似度(如果相似度超过阈值,则此日志无效)
_, similarity := stringUtil.Similarity(monitorMessage, item.MonitorMessage)
if similarity >= CON_MONITOR_HISTORY_SIMILARITY_THERSHOLD {
valid = false
return // 直接返回,无需将当前日志添加到历史列表中,以提高性能
}
}
// 将消息添加到历史消息列表中
monitorHistoryList = append(monitorHistoryList, newMonitorHistory(monitorMessage, timestamp))
if len(monitorHistoryList) > CON_MONITOR_HISTORY_SIZE {
monitorHistoryList = monitorHistoryList[len(monitorHistoryList)-CON_MONITOR_HISTORY_SIZE:]
}
return
}