Fix multiple bugs

This commit is contained in:
John Baublitz
2018-06-04 13:19:14 +02:00
committed by bergquist
parent 3cb0e27e1c
commit fca97535d1
8 changed files with 95 additions and 36 deletions
+2 -2
View File
@@ -151,8 +151,8 @@ func (c *EvalContext) LastNotify(notifierId int64) *time.Time {
NotifierId: notifierId,
}
if err := bus.Dispatch(cmd); err != nil {
c.log.Warn("Could not determine last time alert",
c.Rule.Name, "notified")
c.log.Warn("Could not determine last time alert notifier fired",
"Alert name", c.Rule.Name, "Error", err)
return nil
}
+1
View File
@@ -41,6 +41,7 @@ func defaultShouldNotify(context *alerting.EvalContext, notifyOnce bool, frequen
if context.PrevAlertState == context.Rule.State && notifyOnce {
return false
}
// Do not notify if interval has not elapsed
if !notifyOnce && lastNotify != nil && lastNotify.Add(frequency).After(time.Now()) {
return false
}
+8 -5
View File
@@ -3,6 +3,7 @@ package notifiers
import (
"context"
"testing"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
@@ -18,19 +19,19 @@ func TestBaseNotifier(t *testing.T) {
Convey("can parse false value", func() {
bJson.Set("uploadImage", false)
base := NewNotifierBase(1, false, "name", "email", bJson)
base := NewNotifierBase(1, false, "name", "email", true, 0, bJson)
So(base.UploadImage, ShouldBeFalse)
})
Convey("can parse true value", func() {
bJson.Set("uploadImage", true)
base := NewNotifierBase(1, false, "name", "email", bJson)
base := NewNotifierBase(1, false, "name", "email", true, 0, bJson)
So(base.UploadImage, ShouldBeTrue)
})
Convey("default value should be true for backwards compatibility", func() {
base := NewNotifierBase(1, false, "name", "email", bJson)
base := NewNotifierBase(1, false, "name", "email", true, 0, bJson)
So(base.UploadImage, ShouldBeTrue)
})
})
@@ -41,7 +42,8 @@ func TestBaseNotifier(t *testing.T) {
State: m.AlertStatePending,
})
context.Rule.State = m.AlertStateOK
So(defaultShouldNotify(context), ShouldBeFalse)
timeNow := time.Now()
So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeFalse)
})
Convey("ok -> alerting", func() {
@@ -49,7 +51,8 @@ func TestBaseNotifier(t *testing.T) {
State: m.AlertStateOK,
})
context.Rule.State = m.AlertStateAlerting
So(defaultShouldNotify(context), ShouldBeTrue)
timeNow := time.Now()
So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeTrue)
})
})
})