mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #14229 from pbakulev/configurable-alert-notification
Configurable alert notification
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -17,11 +18,15 @@ func init() {
|
||||
bus.AddHandler("sql", CreateAlertNotificationCommand)
|
||||
bus.AddHandler("sql", UpdateAlertNotification)
|
||||
bus.AddHandler("sql", DeleteAlertNotification)
|
||||
bus.AddHandler("sql", GetAlertNotificationsToSend)
|
||||
bus.AddHandler("sql", GetAllAlertNotifications)
|
||||
bus.AddHandlerCtx("sql", GetOrCreateAlertNotificationState)
|
||||
bus.AddHandlerCtx("sql", SetAlertNotificationStateToCompleteCommand)
|
||||
bus.AddHandlerCtx("sql", SetAlertNotificationStateToPendingCommand)
|
||||
|
||||
bus.AddHandler("sql", GetAlertNotificationsWithUid)
|
||||
bus.AddHandler("sql", UpdateAlertNotificationWithUid)
|
||||
bus.AddHandler("sql", DeleteAlertNotificationWithUid)
|
||||
bus.AddHandler("sql", GetAlertNotificationsWithUidToSend)
|
||||
}
|
||||
|
||||
func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
|
||||
@@ -39,10 +44,33 @@ func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteAlertNotificationWithUid(cmd *m.DeleteAlertNotificationWithUidCommand) error {
|
||||
existingNotification := &m.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
||||
if err := getAlertNotificationWithUidInternal(existingNotification, newSession()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if existingNotification.Result != nil {
|
||||
deleteCommand := &m.DeleteAlertNotificationCommand{
|
||||
Id: existingNotification.Result.Id,
|
||||
OrgId: existingNotification.Result.OrgId,
|
||||
}
|
||||
if err := bus.Dispatch(deleteCommand); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAlertNotifications(query *m.GetAlertNotificationsQuery) error {
|
||||
return getAlertNotificationInternal(query, newSession())
|
||||
}
|
||||
|
||||
func GetAlertNotificationsWithUid(query *m.GetAlertNotificationsWithUidQuery) error {
|
||||
return getAlertNotificationWithUidInternal(query, newSession())
|
||||
}
|
||||
|
||||
func GetAllAlertNotifications(query *m.GetAllAlertNotificationsQuery) error {
|
||||
results := make([]*m.AlertNotification, 0)
|
||||
if err := x.Where("org_id = ?", query.OrgId).Find(&results); err != nil {
|
||||
@@ -53,12 +81,13 @@ func GetAllAlertNotifications(query *m.GetAllAlertNotificationsQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) error {
|
||||
func GetAlertNotificationsWithUidToSend(query *m.GetAlertNotificationsWithUidToSendQuery) error {
|
||||
var sql bytes.Buffer
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
sql.WriteString(`SELECT
|
||||
sql.WriteString(`SELECT
|
||||
alert_notification.id,
|
||||
alert_notification.uid,
|
||||
alert_notification.org_id,
|
||||
alert_notification.name,
|
||||
alert_notification.type,
|
||||
@@ -77,9 +106,10 @@ func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) erro
|
||||
|
||||
sql.WriteString(` AND ((alert_notification.is_default = ?)`)
|
||||
params = append(params, dialect.BooleanStr(true))
|
||||
if len(query.Ids) > 0 {
|
||||
sql.WriteString(` OR alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + ")")
|
||||
for _, v := range query.Ids {
|
||||
|
||||
if len(query.Uids) > 0 {
|
||||
sql.WriteString(` OR alert_notification.uid IN (?` + strings.Repeat(",?", len(query.Uids)-1) + ")")
|
||||
for _, v := range query.Uids {
|
||||
params = append(params, v)
|
||||
}
|
||||
}
|
||||
@@ -142,16 +172,70 @@ func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *DBS
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAlertNotificationWithUidInternal(query *m.GetAlertNotificationsWithUidQuery, sess *DBSession) error {
|
||||
var sql bytes.Buffer
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
sql.WriteString(`SELECT
|
||||
alert_notification.id,
|
||||
alert_notification.uid,
|
||||
alert_notification.org_id,
|
||||
alert_notification.name,
|
||||
alert_notification.type,
|
||||
alert_notification.created,
|
||||
alert_notification.updated,
|
||||
alert_notification.settings,
|
||||
alert_notification.is_default,
|
||||
alert_notification.disable_resolve_message,
|
||||
alert_notification.send_reminder,
|
||||
alert_notification.frequency
|
||||
FROM alert_notification
|
||||
`)
|
||||
|
||||
sql.WriteString(` WHERE alert_notification.org_id = ? AND alert_notification.uid = ?`)
|
||||
params = append(params, query.OrgId, query.Uid)
|
||||
|
||||
results := make([]*m.AlertNotification, 0)
|
||||
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
query.Result = nil
|
||||
} else {
|
||||
query.Result = results[0]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
|
||||
err := getAlertNotificationInternal(existingQuery, sess)
|
||||
if cmd.Uid == "" {
|
||||
if uid, uidGenerationErr := generateNewAlertNotificationUid(sess, cmd.OrgId); uidGenerationErr != nil {
|
||||
return uidGenerationErr
|
||||
} else {
|
||||
cmd.Uid = uid
|
||||
}
|
||||
}
|
||||
existingQuery := &m.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
||||
err := getAlertNotificationWithUidInternal(existingQuery, sess)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if existingQuery.Result != nil {
|
||||
return fmt.Errorf("Alert notification uid %s already exists", cmd.Uid)
|
||||
}
|
||||
|
||||
// check if name exists
|
||||
sameNameQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
|
||||
if err := getAlertNotificationInternal(sameNameQuery, sess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sameNameQuery.Result != nil {
|
||||
return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
|
||||
}
|
||||
|
||||
@@ -168,6 +252,7 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
|
||||
}
|
||||
|
||||
alertNotification := &m.AlertNotification{
|
||||
Uid: cmd.Uid,
|
||||
OrgId: cmd.OrgId,
|
||||
Name: cmd.Name,
|
||||
Type: cmd.Type,
|
||||
@@ -189,6 +274,22 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
|
||||
})
|
||||
}
|
||||
|
||||
func generateNewAlertNotificationUid(sess *DBSession, orgId int64) (string, error) {
|
||||
for i := 0; i < 3; i++ {
|
||||
uid := util.GenerateShortUid()
|
||||
exists, err := sess.Where("org_id=? AND uid=?", orgId, uid).Get(&m.AlertNotification{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return uid, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", m.ErrAlertNotificationFailedGenerateUniqueUid
|
||||
}
|
||||
|
||||
func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
|
||||
return inTransaction(func(sess *DBSession) (err error) {
|
||||
current := m.AlertNotification{}
|
||||
@@ -241,6 +342,39 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateAlertNotificationWithUid(cmd *m.UpdateAlertNotificationWithUidCommand) error {
|
||||
getAlertNotificationWithUidQuery := &m.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
||||
|
||||
if err := getAlertNotificationWithUidInternal(getAlertNotificationWithUidQuery, newSession()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
current := getAlertNotificationWithUidQuery.Result
|
||||
|
||||
if current == nil {
|
||||
return fmt.Errorf("Cannot update, alert notification uid %s doesn't exist", cmd.Uid)
|
||||
}
|
||||
|
||||
updateNotification := &m.UpdateAlertNotificationCommand{
|
||||
Id: current.Id,
|
||||
Name: cmd.Name,
|
||||
Type: cmd.Type,
|
||||
SendReminder: cmd.SendReminder,
|
||||
DisableResolveMessage: cmd.DisableResolveMessage,
|
||||
Frequency: cmd.Frequency,
|
||||
IsDefault: cmd.IsDefault,
|
||||
Settings: cmd.Settings,
|
||||
|
||||
OrgId: cmd.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(updateNotification); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToCompleteCommand) error {
|
||||
return inTransactionCtx(ctx, func(sess *DBSession) error {
|
||||
version := cmd.Version
|
||||
|
||||
@@ -220,11 +220,38 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
||||
So(cmd.Result.Type, ShouldEqual, "email")
|
||||
So(cmd.Result.Frequency, ShouldEqual, 10*time.Second)
|
||||
So(cmd.Result.DisableResolveMessage, ShouldBeFalse)
|
||||
So(cmd.Result.Uid, ShouldNotBeEmpty)
|
||||
|
||||
Convey("Cannot save Alert Notification with the same name", func() {
|
||||
err = CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Cannot save Alert Notification with the same name and another uid", func() {
|
||||
anotherUidCmd := &models.CreateAlertNotificationCommand{
|
||||
Name: cmd.Name,
|
||||
Type: cmd.Type,
|
||||
OrgId: 1,
|
||||
SendReminder: cmd.SendReminder,
|
||||
Frequency: cmd.Frequency,
|
||||
Settings: cmd.Settings,
|
||||
Uid: "notifier1",
|
||||
}
|
||||
err = CreateAlertNotificationCommand(anotherUidCmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Can save Alert Notification with another name and another uid", func() {
|
||||
anotherUidCmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "another ops",
|
||||
Type: cmd.Type,
|
||||
OrgId: 1,
|
||||
SendReminder: cmd.SendReminder,
|
||||
Frequency: cmd.Frequency,
|
||||
Settings: cmd.Settings,
|
||||
Uid: "notifier2",
|
||||
}
|
||||
err = CreateAlertNotificationCommand(anotherUidCmd)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Can update alert notification", func() {
|
||||
newCmd := &models.UpdateAlertNotificationCommand{
|
||||
@@ -274,12 +301,12 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
||||
So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
|
||||
|
||||
Convey("search", func() {
|
||||
query := &models.GetAlertNotificationsToSendQuery{
|
||||
Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
|
||||
query := &models.GetAlertNotificationsWithUidToSendQuery{
|
||||
Uids: []string{cmd1.Result.Uid, cmd2.Result.Uid, "112341231"},
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetAlertNotificationsToSend(query)
|
||||
err := GetAlertNotificationsWithUidToSend(query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 3)
|
||||
})
|
||||
|
||||
@@ -137,4 +137,21 @@ func addAlertMigrations(mg *Migrator) {
|
||||
mg.AddMigration("Add for to alert table", NewAddColumnMigration(alertV1, &Column{
|
||||
Name: "for", Type: DB_BigInt, Nullable: true,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Add column uid in alert_notification", NewAddColumnMigration(alert_notification, &Column{
|
||||
Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: true,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Update uid column values in alert_notification", new(RawSqlMigration).
|
||||
Sqlite("UPDATE alert_notification SET uid=printf('%09d',id) WHERE uid IS NULL;").
|
||||
Postgres("UPDATE alert_notification SET uid=lpad('' || id,9,'0') WHERE uid IS NULL;").
|
||||
Mysql("UPDATE alert_notification SET uid=lpad(id,9,'0') WHERE uid IS NULL;"))
|
||||
|
||||
mg.AddMigration("Add unique index alert_notification_org_id_uid", NewAddIndexMigration(alert_notification, &Index{
|
||||
Cols: []string{"org_id", "uid"}, Type: UniqueIndex,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Remove unique index org_id_name", NewDropIndexMigration(alert_notification, &Index{
|
||||
Cols: []string{"org_id", "name"}, Type: UniqueIndex,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user