From 0b88aba906cd60ce4f001a8874eabdf4b1b93fc1 Mon Sep 17 00:00:00 2001 From: liugq Date: Wed, 27 Apr 2022 09:45:28 +0800 Subject: [PATCH] save last alert notification time to kv --- service/alerting/constants.go | 11 +++++++++++ service/alerting/elasticsearch/engine.go | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 service/alerting/constants.go diff --git a/service/alerting/constants.go b/service/alerting/constants.go new file mode 100644 index 00000000..013f80b0 --- /dev/null +++ b/service/alerting/constants.go @@ -0,0 +1,11 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * web: https://infinilabs.com + * mail: hello#infini.ltd */ + +package alerting + +const ( + KVLastNotificationTime = "alert_last_notification_time" + KVLastTermStartTime = "alert_last_term_start_time" + KVLastEscalationTime = "alert_last_escalation_time" +) diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index 5c95e2cd..fc600a0e 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -13,8 +13,10 @@ import ( log "github.com/cihub/seelog" "github.com/valyala/fasttemplate" "infini.sh/console/model/alerting" + alerting2 "infini.sh/console/service/alerting" "infini.sh/console/service/alerting/action" "infini.sh/framework/core/elastic" + "infini.sh/framework/core/kv" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" "io" @@ -534,6 +536,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error { Objects: rule.Resource.Objects, ConditionResult: checkResults, Conditions: rule.Conditions, + State: alerting.AlertStateNormal, } if err != nil { return err @@ -575,7 +578,18 @@ func (engine *Engine) Do(rule *alerting.Rule) error { alertItem.Error = err.Error() return err } - period := time.Now().Sub(rule.LastNotificationTime) + if rule.LastNotificationTime.IsZero() { + timeBytes, err := kv.GetValue(alerting2.KVLastNotificationTime, []byte(rule.ID)) + if err != nil { + return fmt.Errorf("get last notification time from kv error: %w", err) + } + rule.LastNotificationTime, err = time.ParseInLocation(time.RFC3339, string(timeBytes), time.UTC) + if err != nil { + return fmt.Errorf("parse last notification time from kv error: %w", err) + } + } + period := time.Now().Sub(rule.LastNotificationTime.Local()) + //log.Error(lastAlertItem.ID, period, periodDuration) if lastAlertItem.ID == "" || period > periodDuration { @@ -583,6 +597,8 @@ func (engine *Engine) Do(rule *alerting.Rule) error { alertItem.ActionExecutionResults = actionResults //todo init last notification time when create task (by last alert item is notified) rule.LastNotificationTime = time.Now() + strTime := time.Now().UTC().Format(time.RFC3339) + kv.AddValue(alerting2.KVLastNotificationTime, []byte(rule.ID), []byte(strTime)) alertItem.IsNotified = true //kv.AddValue(alerting2.KVLastNotificationTime, []byte(rule.ID), []byte(rule.LastNotificationTime.Format(time.RFC3339))) }