alerting: move queries from evalcontext to notifier base

This commit is contained in:
bergquist
2018-06-05 12:07:02 +02:00
parent 05245e9b3d
commit bcbae7aa62
4 changed files with 23 additions and 18 deletions

View File

@@ -143,18 +143,3 @@ func (c *EvalContext) GetNewState() m.AlertStateType {
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
}

View File

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

View File

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

View File

@@ -3,6 +3,8 @@ package notifiers
import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -15,6 +17,8 @@ type NotifierBase struct {
UploadImage bool
SendReminder bool
Frequency time.Duration
log log.Logger
}
func NewNotifierBase(model *models.AlertNotification) NotifierBase {
@@ -32,6 +36,7 @@ func NewNotifierBase(model *models.AlertNotification) NotifierBase {
UploadImage: uploadImage,
SendReminder: model.SendReminder,
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) {
return false
}
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 {