alerting: only check frequency when not send once

This commit is contained in:
bergquist
2018-06-04 22:19:27 +02:00
parent 0e647db485
commit 93124f38fa
5 changed files with 135 additions and 81 deletions
+4 -50
View File
@@ -192,17 +192,7 @@ func GetAlertNotifications(c *m.ReqContext) Response {
result := make([]*dtos.AlertNotification, 0)
for _, notification := range query.Result {
result = append(result, &dtos.AlertNotification{
Id: notification.Id,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Created: notification.Created,
Updated: notification.Updated,
Frequency: notification.Frequency.String(),
NotifyOnce: notification.NotifyOnce,
Settings: notification.Settings,
})
result = append(result, dtos.NewAlertNotification(notification))
}
return JSON(200, result)
@@ -218,19 +208,7 @@ func GetAlertNotificationByID(c *m.ReqContext) Response {
return Error(500, "Failed to get alert notifications", err)
}
result := &dtos.AlertNotification{
Id: query.Result.Id,
Name: query.Result.Name,
Type: query.Result.Type,
IsDefault: query.Result.IsDefault,
Created: query.Result.Created,
Updated: query.Result.Updated,
Frequency: query.Result.Frequency.String(),
NotifyOnce: query.Result.NotifyOnce,
Settings: query.Result.Settings,
}
return JSON(200, result)
return JSON(200, dtos.NewAlertNotification(query.Result))
}
func CreateAlertNotification(c *m.ReqContext, cmd m.CreateAlertNotificationCommand) Response {
@@ -240,19 +218,7 @@ func CreateAlertNotification(c *m.ReqContext, cmd m.CreateAlertNotificationComma
return Error(500, "Failed to create alert notification", err)
}
result := &dtos.AlertNotification{
Id: cmd.Result.Id,
Name: cmd.Result.Name,
Type: cmd.Result.Type,
IsDefault: cmd.Result.IsDefault,
Created: cmd.Result.Created,
Updated: cmd.Result.Updated,
Frequency: cmd.Result.Frequency.String(),
NotifyOnce: cmd.Result.NotifyOnce,
Settings: cmd.Result.Settings,
}
return JSON(200, result)
return JSON(200, dtos.NewAlertNotification(cmd.Result))
}
func UpdateAlertNotification(c *m.ReqContext, cmd m.UpdateAlertNotificationCommand) Response {
@@ -262,19 +228,7 @@ func UpdateAlertNotification(c *m.ReqContext, cmd m.UpdateAlertNotificationComma
return Error(500, "Failed to update alert notification", err)
}
result := &dtos.AlertNotification{
Id: cmd.Result.Id,
Name: cmd.Result.Name,
Type: cmd.Result.Type,
IsDefault: cmd.Result.IsDefault,
Created: cmd.Result.Created,
Updated: cmd.Result.Updated,
Frequency: cmd.Result.Frequency.String(),
NotifyOnce: cmd.Result.NotifyOnce,
Settings: cmd.Result.Settings,
}
return JSON(200, result)
return JSON(200, dtos.NewAlertNotification(cmd.Result))
}
func DeleteAlertNotification(c *m.ReqContext) Response {
+19
View File
@@ -2,6 +2,7 @@ package api
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
@@ -11,6 +12,24 @@ import (
. "github.com/smartystreets/goconvey/convey"
)
func TestRemoveZeroUnitsFromInterval(t *testing.T) {
tcs := []struct {
interval time.Duration
expected string
}{
{interval: time.Duration(time.Hour), expected: "1h"},
{interval: time.Duration(time.Hour + time.Minute), expected: "1h1m"},
{interval: time.Duration((time.Hour * 10) + time.Minute), expected: "10h1m"},
}
for _, tc := range tcs {
got := removeZeroesFromDuration(tc.interval)
if got != tc.expected {
t.Errorf("expected %s got %s internval: %v", tc.expected, got, tc.interval)
}
}
}
func TestAlertingApiEndpoint(t *testing.T) {
Convey("Given an alert in a dashboard with an acl", t, func() {
+39 -14
View File
@@ -1,26 +1,51 @@
package dtos
import (
"strings"
"time"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
type AlertRule struct {
Id int64 `json:"id"`
DashboardId int64 `json:"dashboardId"`
PanelId int64 `json:"panelId"`
Name string `json:"name"`
Message string `json:"message"`
State m.AlertStateType `json:"state"`
NewStateDate time.Time `json:"newStateDate"`
EvalDate time.Time `json:"evalDate"`
EvalData *simplejson.Json `json:"evalData"`
ExecutionError string `json:"executionError"`
Url string `json:"url"`
CanEdit bool `json:"canEdit"`
Id int64 `json:"id"`
DashboardId int64 `json:"dashboardId"`
PanelId int64 `json:"panelId"`
Name string `json:"name"`
Message string `json:"message"`
State models.AlertStateType `json:"state"`
NewStateDate time.Time `json:"newStateDate"`
EvalDate time.Time `json:"evalDate"`
EvalData *simplejson.Json `json:"evalData"`
ExecutionError string `json:"executionError"`
Url string `json:"url"`
CanEdit bool `json:"canEdit"`
}
func removeZeroesFromDuration(interval time.Duration) string {
frequency := interval.String()
frequency = strings.Replace(frequency, "0h", "", 1)
frequency = strings.Replace(frequency, "0m", "", 1)
frequency = strings.Replace(frequency, "0s", "", 1)
return frequency
}
func NewAlertNotification(notification *models.AlertNotification) *AlertNotification {
return &AlertNotification{
Id: notification.Id,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Created: notification.Created,
Updated: notification.Updated,
Frequency: removeZeroesFromDuration(notification.Frequency),
NotifyOnce: notification.NotifyOnce,
Settings: notification.Settings,
}
}
type AlertNotification struct {
@@ -42,7 +67,7 @@ type AlertTestCommand struct {
type AlertTestResult struct {
Firing bool `json:"firing"`
State m.AlertStateType `json:"state"`
State models.AlertStateType `json:"state"`
ConditionEvals string `json:"conditionEvals"`
TimeMs string `json:"timeMs"`
Error string `json:"error,omitempty"`