mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
alerting: move queries from evalcontext to notifier base
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user