From c893e5d24183c604bdea8904a9a3091f9e3e3502 Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 5 Sep 2016 21:33:05 +0200 Subject: [PATCH] feat(notifications): add support for default notifications ref #5883 --- pkg/api/alerting.go | 11 ++-- pkg/api/dtos/alerting.go | 11 ++-- pkg/models/alert_notifications.go | 31 +++++----- pkg/services/alerting/notifier.go | 5 +- pkg/services/sqlstore/alert_notification.go | 61 +++++++++++-------- .../sqlstore/alert_notification_test.go | 4 +- pkg/services/sqlstore/migrations/alert_mig.go | 4 ++ .../alerting/notification_edit_ctrl.ts | 3 +- .../alerting/partials/notification_edit.html | 9 +++ .../alerting/partials/notifications_list.html | 7 ++- 10 files changed, 89 insertions(+), 57 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 10492cb9a47..83a370499c5 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -157,11 +157,12 @@ func GetAlertNotifications(c *middleware.Context) Response { for _, notification := range query.Result { result = append(result, dtos.AlertNotification{ - Id: notification.Id, - Name: notification.Name, - Type: notification.Type, - Created: notification.Created, - Updated: notification.Updated, + Id: notification.Id, + Name: notification.Name, + Type: notification.Type, + IsDefault: notification.IsDefault, + Created: notification.Created, + Updated: notification.Updated, }) } diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go index 474f0a5a048..83b21757d32 100644 --- a/pkg/api/dtos/alerting.go +++ b/pkg/api/dtos/alerting.go @@ -22,11 +22,12 @@ type AlertRule struct { } type AlertNotification struct { - Id int64 `json:"id"` - Name string `json:"name"` - Type string `json:"type"` - Created time.Time `json:"created"` - Updated time.Time `json:"updated"` + Id int64 `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + IsDefault bool `json:"isDefault"` + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` } type AlertTestCommand struct { diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go index 464d6dc88da..1e586b4974e 100644 --- a/pkg/models/alert_notifications.go +++ b/pkg/models/alert_notifications.go @@ -7,29 +7,32 @@ import ( ) type AlertNotification struct { - Id int64 `json:"id"` - OrgId int64 `json:"-"` - Name string `json:"name"` - Type string `json:"type"` - Settings *simplejson.Json `json:"settings"` - Created time.Time `json:"created"` - Updated time.Time `json:"updated"` + Id int64 `json:"id"` + OrgId int64 `json:"-"` + Name string `json:"name"` + Type string `json:"type"` + IsDefault bool `json:"isDefault"` + Settings *simplejson.Json `json:"settings"` + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` } type CreateAlertNotificationCommand struct { - Name string `json:"name" binding:"Required"` - Type string `json:"type" binding:"Required"` - Settings *simplejson.Json `json:"settings"` + Name string `json:"name" binding:"Required"` + Type string `json:"type" binding:"Required"` + IsDefault bool `json:"isDefault"` + Settings *simplejson.Json `json:"settings"` OrgId int64 `json:"-"` Result *AlertNotification } type UpdateAlertNotificationCommand struct { - Id int64 `json:"id" binding:"Required"` - Name string `json:"name" binding:"Required"` - Type string `json:"type" binding:"Required"` - Settings *simplejson.Json `json:"settings" binding:"Required"` + Id int64 `json:"id" binding:"Required"` + Name string `json:"name" binding:"Required"` + Type string `json:"type" binding:"Required"` + IsDefault bool `json:"isDefault"` + Settings *simplejson.Json `json:"settings" binding:"Required"` OrgId int64 `json:"-"` Result *AlertNotification diff --git a/pkg/services/alerting/notifier.go b/pkg/services/alerting/notifier.go index 55090d9772f..b82d4dd3864 100644 --- a/pkg/services/alerting/notifier.go +++ b/pkg/services/alerting/notifier.go @@ -88,11 +88,12 @@ func (n *RootNotifier) uploadImage(context *EvalContext) error { } func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64) ([]Notifier, error) { + query := &m.GetAlertNotificationsQuery{OrgId: orgId} + if len(notificationIds) == 0 { - return []Notifier{}, nil + query.Ids = []int64{0} } - query := &m.GetAlertNotificationsQuery{OrgId: orgId, Ids: notificationIds} if err := bus.Dispatch(query); err != nil { return nil, err } diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index c5b3a2f5975..d33e6d90151 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -40,33 +40,36 @@ func getAlertNotificationsInternal(query *m.GetAlertNotificationsQuery, sess *xo params := make([]interface{}, 0) sql.WriteString(`SELECT - alert_notification.id, - alert_notification.org_id, - alert_notification.name, - alert_notification.type, - alert_notification.created, - alert_notification.updated, - alert_notification.settings - FROM alert_notification - `) + alert_notification.id, + alert_notification.org_id, + alert_notification.name, + alert_notification.type, + alert_notification.created, + alert_notification.updated, + alert_notification.settings, + alert_notification.is_default + FROM alert_notification + `) sql.WriteString(` WHERE alert_notification.org_id = ?`) params = append(params, query.OrgId) - if query.Name != "" { - sql.WriteString(` AND alert_notification.name = ?`) - params = append(params, query.Name) - } + if query.Name != "" || query.Id != 0 || len(query.Ids) > 0 { + if query.Name != "" { + sql.WriteString(` AND alert_notification.name = ?`) + params = append(params, query.Name) + } - if query.Id != 0 { - sql.WriteString(` AND alert_notification.id = ?`) - params = append(params, query.Id) - } + if query.Id != 0 { + sql.WriteString(` AND alert_notification.id = ?`) + params = append(params, query.Id) + } - if len(query.Ids) > 0 { - sql.WriteString(` AND alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + ")") - for _, v := range query.Ids { - params = append(params, v) + if len(query.Ids) > 0 { + sql.WriteString(` AND ((alert_notification.is_default = 1) OR alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + "))") + for _, v := range query.Ids { + params = append(params, v) + } } } @@ -93,12 +96,13 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error } alertNotification := &m.AlertNotification{ - OrgId: cmd.OrgId, - Name: cmd.Name, - Type: cmd.Type, - Settings: cmd.Settings, - Created: time.Now(), - Updated: time.Now(), + OrgId: cmd.OrgId, + Name: cmd.Name, + Type: cmd.Type, + Settings: cmd.Settings, + Created: time.Now(), + Updated: time.Now(), + IsDefault: cmd.IsDefault, } if _, err = sess.Insert(alertNotification); err != nil { @@ -132,6 +136,9 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error { current.Settings = cmd.Settings current.Name = cmd.Name current.Type = cmd.Type + current.IsDefault = cmd.IsDefault + + sess.UseBool("is_default") if affected, err := sess.Id(cmd.Id).Update(current); err != nil { return err diff --git a/pkg/services/sqlstore/alert_notification_test.go b/pkg/services/sqlstore/alert_notification_test.go index 4cbcf13a000..f9ecf0e5234 100644 --- a/pkg/services/sqlstore/alert_notification_test.go +++ b/pkg/services/sqlstore/alert_notification_test.go @@ -63,10 +63,12 @@ func TestAlertNotificationSQLAccess(t *testing.T) { cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, Settings: simplejson.New()} cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, Settings: simplejson.New()} cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, Settings: simplejson.New()} + cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, Settings: simplejson.New()} So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil) So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil) So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil) + So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil) Convey("search", func() { query := &m.GetAlertNotificationsQuery{ @@ -76,7 +78,7 @@ func TestAlertNotificationSQLAccess(t *testing.T) { err := GetAlertNotifications(query) So(err, ShouldBeNil) - So(len(query.Result), ShouldEqual, 2) + So(len(query.Result), ShouldEqual, 3) }) }) }) diff --git a/pkg/services/sqlstore/migrations/alert_mig.go b/pkg/services/sqlstore/migrations/alert_mig.go index 63d61d8b196..b6956be41c7 100644 --- a/pkg/services/sqlstore/migrations/alert_mig.go +++ b/pkg/services/sqlstore/migrations/alert_mig.go @@ -62,5 +62,9 @@ func addAlertMigrations(mg *Migrator) { } mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification)) + mg.AddMigration("Add column is_default", NewAddColumnMigration(alert_notification, &Column{ + Name: "is_default", Type: DB_Bool, Nullable: false, Default: "0", + })) mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0])) + } diff --git a/public/app/features/alerting/notification_edit_ctrl.ts b/public/app/features/alerting/notification_edit_ctrl.ts index 99193a9d7d1..d465a9021a6 100644 --- a/public/app/features/alerting/notification_edit_ctrl.ts +++ b/public/app/features/alerting/notification_edit_ctrl.ts @@ -17,7 +17,8 @@ export class AlertNotificationEditCtrl { } else { this.model = { type: 'email', - settings: {} + settings: {}, + isDefault: false }; } } diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html index 5664d201967..c9f181eacf9 100644 --- a/public/app/features/alerting/partials/notification_edit.html +++ b/public/app/features/alerting/partials/notification_edit.html @@ -25,6 +25,15 @@ +
+ + +
diff --git a/public/app/features/alerting/partials/notifications_list.html b/public/app/features/alerting/partials/notifications_list.html index df3cbfcc65d..8777d6f6a1b 100644 --- a/public/app/features/alerting/partials/notifications_list.html +++ b/public/app/features/alerting/partials/notifications_list.html @@ -10,7 +10,7 @@
- +
@@ -25,7 +25,10 @@ -
Name Type {{notification.type}} + + + default + edit