Просмотр исходного кода

feat(alerting): make sure dashboard id exists

bergquist 9 лет назад
Родитель
Сommit
7860a2a1b8
4 измененных файлов с 9 добавлено и 7 удалено
  1. 1 1
      pkg/api/dashboard.go
  2. 1 2
      pkg/models/alerts.go
  3. 4 1
      pkg/models/alerts_test.go
  4. 3 3
      pkg/services/sqlstore/alerting_test.go

+ 1 - 1
pkg/api/dashboard.go

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

+ 1 - 2
pkg/models/alerts.go

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

+ 4 - 1
pkg/models/alerts_test.go

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

+ 3 - 3
pkg/services/sqlstore/alerting_test.go

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