Skip to content

Commit

Permalink
fix service (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb authored Mar 2, 2025
1 parent 4844b1d commit 831ddf1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion model/alertrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *AlertRule) Check(points [][]bool) (int, bool) {
if hasPassedRule = boundCheck(len(points), duration, hasPassedRule); hasPassedRule {
continue
}
for timeTick := len(points); timeTick >= len(points)-duration; timeTick-- {
for timeTick := len(points); timeTick > len(points)-duration; timeTick-- {
fail++
if points[timeTick][ruleIndex] {
hasPassedRule = true
Expand Down
8 changes: 5 additions & 3 deletions service/singleton/servicesentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func NewServiceSentinel(serviceSentinelDispatchBus chan<- *model.Service, sc *Se

var mhs []model.ServiceHistory
// 加载当日记录
DB.Where("created_at >= ?", today).Find(&mhs)
DB.Where("created_at >= ? AND server_id = 0", today).Find(&mhs)
totalDelay := make(map[uint64]float32)
totalDelayCount := make(map[uint64]float32)
for _, mh := range mhs {
Expand Down Expand Up @@ -233,7 +233,7 @@ func (ss *ServiceSentinel) loadServiceHistory() error {

// 加载服务监控历史记录
var mhs []model.ServiceHistory
DB.Where("created_at > ? AND created_at < ?", today.AddDate(0, 0, -29), today).Find(&mhs)
DB.Where("created_at > ? AND created_at < ? AND server_id = 0", today.AddDate(0, 0, -29), today).Find(&mhs)
var delayCount = make(map[int]int)
for _, mh := range mhs {
dayIndex := 28 - (int(today.Sub(mh.CreatedAt).Hours()) / 24)
Expand Down Expand Up @@ -412,6 +412,7 @@ func (ss *ServiceSentinel) worker() {
continue
}
css = nil

mh := r.Data
if mh.Type == model.TaskTypeTCPPing || mh.Type == model.TaskTypeICMPPing {
serviceTcpMap, ok := ss.serviceResponsePing[mh.GetId()]
Expand All @@ -426,7 +427,6 @@ func (ss *ServiceSentinel) worker() {
ts.count++
ts.ping = (ts.ping*float32(ts.count-1) + mh.Delay) / float32(ts.count)
if ts.count == Conf.AvgPingCount {
ts.count = 0
if err := DB.Create(&model.ServiceHistory{
ServiceID: mh.GetId(),
AvgDelay: ts.ping,
Expand All @@ -435,10 +435,12 @@ func (ss *ServiceSentinel) worker() {
}).Error; err != nil {
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
}
ts.count = 0
ts.ping = mh.Delay
}
serviceTcpMap[r.Reporter] = ts
}

ss.serviceResponseDataStoreLock.Lock()
// 写入当天状态
if mh.Successful {
Expand Down

0 comments on commit 831ddf1

Please sign in to comment.