|
@@ -24,7 +24,7 @@ type NotifierPlugin struct {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type NotificationService interface {
|
|
type NotificationService interface {
|
|
|
- Send(context *EvalContext) error
|
|
|
|
|
|
|
+ SendIfNeeded(context *EvalContext) error
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func NewNotificationService() NotificationService {
|
|
func NewNotificationService() NotificationService {
|
|
@@ -41,14 +41,12 @@ func newNotificationService() *notificationService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (n *notificationService) Send(context *EvalContext) error {
|
|
|
|
|
- notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
|
|
|
|
|
|
|
+func (n *notificationService) SendIfNeeded(context *EvalContext) error {
|
|
|
|
|
+ notifiers, err := n.getNeededNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- n.log.Info("Sending notifications for", "ruleId", context.Rule.Id, "sent count", len(notifiers))
|
|
|
|
|
-
|
|
|
|
|
if len(notifiers) == 0 {
|
|
if len(notifiers) == 0 {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
@@ -110,7 +108,7 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (n *notificationService) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) (NotifierSlice, error) {
|
|
|
|
|
|
|
+func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []int64, context *EvalContext) (NotifierSlice, error) {
|
|
|
query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
|
|
query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
if err := bus.Dispatch(query); err != nil {
|
|
@@ -122,7 +120,7 @@ func (n *notificationService) getNotifiers(orgId int64, notificationIds []int64,
|
|
|
if not, err := n.createNotifierFor(notification); err != nil {
|
|
if not, err := n.createNotifierFor(notification); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
} else {
|
|
} else {
|
|
|
- if shouldUseNotification(not, context) {
|
|
|
|
|
|
|
+ if not.ShouldNotify(context) {
|
|
|
result = append(result, not)
|
|
result = append(result, not)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -140,18 +138,6 @@ func (n *notificationService) createNotifierFor(model *m.AlertNotification) (Not
|
|
|
return notifierPlugin.Factory(model)
|
|
return notifierPlugin.Factory(model)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
|
|
|
|
|
- if !context.Firing {
|
|
|
|
|
- return true
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if context.Error != nil {
|
|
|
|
|
- return true
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return notifier.PassesFilter(context.Rule)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
|
|
type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
|
|
|
|
|
|
|
|
var notifierFactories map[string]*NotifierPlugin = make(map[string]*NotifierPlugin)
|
|
var notifierFactories map[string]*NotifierPlugin = make(map[string]*NotifierPlugin)
|