feat(alerting): make sure dashboard id exists

This commit is contained in:
bergquist 2016-04-20 09:38:44 +02:00
parent ca3ad7d17c
commit 7860a2a1b8
4 changed files with 9 additions and 7 deletions

View File

@ -150,7 +150,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
}
saveAlertCommand := m.SaveAlertsCommand{
DashboardId: dash.Id,
DashboardId: cmd.Result.Id,
OrgId: c.OrgId,
UserId: c.UserId,
Alerts: cmd.GetAlertModels(),

View File

@ -20,7 +20,6 @@ type Alert struct {
}
func (cmd *SaveDashboardCommand) GetAlertModels() *[]Alert {
dash := NewDashboardFromJson(cmd.Dashboard)
alerts := make([]Alert, 0)
for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
@ -31,7 +30,7 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]Alert {
alerting := panel.Get("alerting")
alert := Alert{
DashboardId: dash.Id,
DashboardId: cmd.Result.Id,
PanelId: panel.Get("id").MustInt64(),
Id: alerting.Get("id").MustInt64(),
QueryRefId: alerting.Get("query_ref").MustString(),

View File

@ -258,6 +258,9 @@ func TestAlertModel(t *testing.T) {
UserId: 1,
OrgId: 1,
Overwrite: true,
Result: &Dashboard{
Id: 1,
},
}
alerts := *cmd.GetAlertModels()
@ -267,7 +270,7 @@ func TestAlertModel(t *testing.T) {
So(len(alerts), ShouldEqual, 1)
for _, v := range alerts {
So(v.DashboardId, ShouldNotEqual, 0)
So(v.DashboardId, ShouldEqual, 1)
So(v.PanelId, ShouldNotEqual, 0)
So(v.WarnLevel, ShouldEqual, 30)

View File

@ -3,9 +3,8 @@ package sqlstore
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestAlertingDataAccess(t *testing.T) {
@ -15,7 +14,7 @@ func TestAlertingDataAccess(t *testing.T) {
Convey("Can create alert", func() {
items := []m.Alert{
m.Alert{
{
PanelId: 1,
DashboardId: 1,
Query: "Query",
@ -29,6 +28,7 @@ func TestAlertingDataAccess(t *testing.T) {
Aggregator: "avg",
},
}
cmd := m.SaveAlertsCommand{
Alerts: &items,
DashboardId: 1,