Просмотр исходного кода

alerting: move queries from evalcontext to notifier base

bergquist 7 лет назад
Родитель
Сommit
bcbae7aa62

+ 0 - 15
pkg/services/alerting/eval_context.go

@@ -143,18 +143,3 @@ func (c *EvalContext) GetNewState() m.AlertStateType {
 
 
 	return m.AlertStateOK
 	return m.AlertStateOK
 }
 }
-
-func (c *EvalContext) LastNotify(notifierId int64) *time.Time {
-	cmd := &m.GetLatestNotificationQuery{
-		OrgId:      c.Rule.OrgId,
-		AlertId:    c.Rule.Id,
-		NotifierId: notifierId,
-	}
-	if err := bus.Dispatch(cmd); err != nil {
-		c.log.Warn("Could not determine last time alert notifier fired",
-			"Alert name", c.Rule.Name, "Error", err)
-		return nil
-	}
-
-	return &cmd.Result.SentAt
-}

+ 2 - 0
pkg/services/alerting/interfaces.go

@@ -15,6 +15,8 @@ type Notifier interface {
 	Notify(evalContext *EvalContext) error
 	Notify(evalContext *EvalContext) error
 	GetType() string
 	GetType() string
 	NeedsImage() bool
 	NeedsImage() bool
+
+	// ShouldNotify checks this evaluation should send an alert notification
 	ShouldNotify(evalContext *EvalContext) bool
 	ShouldNotify(evalContext *EvalContext) bool
 
 
 	GetNotifierId() int64
 	GetNotifierId() int64

+ 1 - 0
pkg/services/alerting/notifier.go

@@ -131,6 +131,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []
 		if err != nil {
 		if err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
+
 		if not.ShouldNotify(context) {
 		if not.ShouldNotify(context) {
 			result = append(result, not)
 			result = append(result, not)
 		}
 		}

+ 20 - 3
pkg/services/alerting/notifiers/base.go

@@ -3,6 +3,8 @@ package notifiers
 import (
 import (
 	"time"
 	"time"
 
 
+	"github.com/grafana/grafana/pkg/bus"
+	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/services/alerting"
 	"github.com/grafana/grafana/pkg/services/alerting"
 )
 )
@@ -15,6 +17,8 @@ type NotifierBase struct {
 	UploadImage  bool
 	UploadImage  bool
 	SendReminder bool
 	SendReminder bool
 	Frequency    time.Duration
 	Frequency    time.Duration
+
+	log log.Logger
 }
 }
 
 
 func NewNotifierBase(model *models.AlertNotification) NotifierBase {
 func NewNotifierBase(model *models.AlertNotification) NotifierBase {
@@ -32,6 +36,7 @@ func NewNotifierBase(model *models.AlertNotification) NotifierBase {
 		UploadImage:  uploadImage,
 		UploadImage:  uploadImage,
 		SendReminder: model.SendReminder,
 		SendReminder: model.SendReminder,
 		Frequency:    model.Frequency,
 		Frequency:    model.Frequency,
+		log:          log.New("alerting.notifier." + model.Name),
 	}
 	}
 }
 }
 
 
@@ -55,12 +60,24 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ
 	if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
 	if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
 		return false
 		return false
 	}
 	}
+
 	return true
 	return true
 }
 }
 
 
-func (n *NotifierBase) ShouldNotify(context *alerting.EvalContext) bool {
-	lastNotify := context.LastNotify(n.Id)
-	return defaultShouldNotify(context, n.SendReminder, n.Frequency, lastNotify)
+// ShouldNotify checks this evaluation should send an alert notification
+func (n *NotifierBase) ShouldNotify(c *alerting.EvalContext) bool {
+	cmd := &models.GetLatestNotificationQuery{
+		OrgId:      c.Rule.OrgId,
+		AlertId:    c.Rule.Id,
+		NotifierId: n.Id,
+	}
+
+	if err := bus.Dispatch(cmd); err != nil {
+		n.log.Error("Could not determine last time alert notifier fired", "Alert name", c.Rule.Name, "Error", err)
+		return false
+	}
+
+	return defaultShouldNotify(c, n.SendReminder, n.Frequency, &cmd.Result.SentAt)
 }
 }
 
 
 func (n *NotifierBase) GetType() string {
 func (n *NotifierBase) GetType() string {