From aaa5b24b3197cdf5970a54911d1c71478ceaf628 Mon Sep 17 00:00:00 2001 From: zhangqihui Date: Mon, 14 Dec 2020 15:55:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=94=B1=E4=BA=8Efired=5Fat=E4=BC=9A?= =?UTF-8?q?=E5=8F=98=EF=BC=8C=E4=B8=80=E6=9D=A1=E5=91=8A=E8=AD=A6=E5=9C=A8?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E5=89=8D=EF=BC=8C=E9=9C=80=E8=A6=81=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=B8=8B=E8=BF=99=E6=9D=A1=E5=91=8A=E8=AD=A6=E7=9A=84?= =?UTF-8?q?=E8=80=81=E5=91=8A=E8=AD=A6=EF=BC=8C=E4=B8=8D=E7=84=B6=E8=80=81?= =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B2=A1=E4=BA=BA=E7=BB=B4=E6=8A=A4=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E4=B8=80=E7=9B=B4=E5=8F=91=E9=80=81=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/alert-gateway/models/alerts.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/alert-gateway/models/alerts.go b/cmd/alert-gateway/models/alerts.go index e7b4ea7..a51b733 100644 --- a/cmd/alert-gateway/models/alerts.go +++ b/cmd/alert-gateway/models/alerts.go @@ -361,6 +361,10 @@ func (u *Alerts) AlertsHandler(alert *common.Alerts) { Id int64 Status uint8 } + var delAlerts []struct { + Id int64 + Status uint8 + } a := &alertForQuery{Alert: &elemt} a.setFields() @@ -381,6 +385,18 @@ func (u *Alerts) AlertsHandler(alert *common.Alerts) { continue } } else { + //插入新告警之前,将老的告警的status字段该为3,3不会发告警 + _, err := Ormer().Raw("SELECT id,status FROM alert WHERE rule_id =? AND labels=? AND status !=?", a.ruleId, a.label, 0).QueryRows(&delAlerts) + if len(delAlerts) > 0 { + //之前未解决的告警记录,需要删掉 + for _, id := range delAlerts { + //_, err = Ormer().Raw("DELETE FROM alert WHERE id=?", id.Id).Exec() + _, err = Ormer().Raw("UPDATE alert SET status =? WHERE id=?", 3, id.Id).Exec() + if err != nil { + logs.Error("database delete alert error:%s\n", err) + } + } + } // insert an new alert var alert Alerts alert.Id = 0 //reset the "Id" to 0,which is very important:after a record is inserted,the value of "Id" will not be 0,but the auto primary key of the record @@ -396,7 +412,7 @@ func (u *Alerts) AlertsHandler(alert *common.Alerts) { alert.ConfirmedAt = &todayZero alert.ConfirmedBefore = &todayZero alert.ResolvedAt = &todayZero - _, err := Ormer().Insert(&alert) + _, err = Ormer().Insert(&alert) if err != nil { logs.Error("Insert alter failed:%s", err) } From b9929219ef433ef1438358c7c81a5c04c729d8e0 Mon Sep 17 00:00:00 2001 From: zhangqihui Date: Tue, 15 Dec 2020 18:21:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=94=B1=E4=BA=8Efired=5Fat=E4=BC=9A?= =?UTF-8?q?=E5=8F=98=EF=BC=8C=E4=B8=80=E6=9D=A1=E5=91=8A=E8=AD=A6=E5=9C=A8?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E5=89=8D=EF=BC=8C=E9=9C=80=E8=A6=81=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=B8=8B=E8=BF=99=E6=9D=A1=E5=91=8A=E8=AD=A6=E7=9A=84?= =?UTF-8?q?=E8=80=81=E5=91=8A=E8=AD=A6=EF=BC=8C=E4=B8=8D=E7=84=B6=E8=80=81?= =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B2=A1=E4=BA=BA=E7=BB=B4=E6=8A=A4=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E4=B8=80=E7=9B=B4=E5=8F=91=E9=80=81=E5=91=8A=E8=AD=A6?= =?UTF-8?q?,=E6=9A=82=E6=97=B6=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E9=80=89=E6=8B=A9=E5=88=A0=E9=99=A4=E8=80=81?= =?UTF-8?q?=E5=91=8A=E8=AD=A6=EF=BC=8C=E8=80=8C=E6=98=AF=E6=9B=B4=E6=96=B0?= =?UTF-8?q?status=E4=B8=BA3=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/alert-gateway/models/alerts.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/alert-gateway/models/alerts.go b/cmd/alert-gateway/models/alerts.go index a51b733..371bfaa 100644 --- a/cmd/alert-gateway/models/alerts.go +++ b/cmd/alert-gateway/models/alerts.go @@ -361,7 +361,7 @@ func (u *Alerts) AlertsHandler(alert *common.Alerts) { Id int64 Status uint8 } - var delAlerts []struct { + var updateAlerts []struct { Id int64 Status uint8 } @@ -385,15 +385,15 @@ func (u *Alerts) AlertsHandler(alert *common.Alerts) { continue } } else { - //插入新告警之前,将老的告警的status字段该为3,3不会发告警 - _, err := Ormer().Raw("SELECT id,status FROM alert WHERE rule_id =? AND labels=? AND status !=?", a.ruleId, a.label, 0).QueryRows(&delAlerts) - if len(delAlerts) > 0 { - //之前未解决的告警记录,需要删掉 - for _, id := range delAlerts { + //插入新告警之前,将老的告警的status字段update为3,3不会发告警 + _, err := Ormer().Raw("SELECT id,status FROM alert WHERE rule_id =? AND labels=? AND status !=?", a.ruleId, a.label, 0).QueryRows(&updateAlerts) + if len(updateAlerts) > 0 { + //old alert ,updaate status + for _, id := range updateAlerts { //_, err = Ormer().Raw("DELETE FROM alert WHERE id=?", id.Id).Exec() _, err = Ormer().Raw("UPDATE alert SET status =? WHERE id=?", 3, id.Id).Exec() if err != nil { - logs.Error("database delete alert error:%s\n", err) + logs.Error("database update alert error:%s\n", err) } } }