mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add sql layer for alert notifications
This commit is contained in:
parent
8b91e57ef6
commit
dbf3795aaf
@ -27,6 +27,7 @@ type CreateAlertNotificationCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UpdateAlertNotificationCommand struct {
|
type UpdateAlertNotificationCommand struct {
|
||||||
|
Id int64
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
OrgID int64
|
OrgID int64
|
||||||
|
@ -2,16 +2,25 @@ package sqlstore
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
bus.AddHandler("sql", GetAlertNotifications)
|
bus.AddHandler("sql", AlertNotificationQuery)
|
||||||
|
bus.AddHandler("sql", CreateAlertNotificationCommand)
|
||||||
|
bus.AddHandler("sql", UpdateAlertNotification)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAlertNotifications(query *m.GetAlertNotificationQuery) error {
|
func AlertNotificationQuery(query *m.GetAlertNotificationQuery) error {
|
||||||
|
return getAlertNotifications(query, x.NewSession())
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAlertNotifications(query *m.GetAlertNotificationQuery, sess *xorm.Session) error {
|
||||||
var sql bytes.Buffer
|
var sql bytes.Buffer
|
||||||
params := make([]interface{}, 0)
|
params := make([]interface{}, 0)
|
||||||
|
|
||||||
@ -35,7 +44,7 @@ func GetAlertNotifications(query *m.GetAlertNotificationQuery) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var result []*m.AlertNotification
|
var result []*m.AlertNotification
|
||||||
if err := x.Sql(sql.String(), params...).Find(&result); err != nil {
|
if err := sess.Sql(sql.String(), params...).Find(&result); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,14 +52,66 @@ func GetAlertNotifications(query *m.GetAlertNotificationQuery) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
|
||||||
func CreateAlertNotification(cmd *m.CreateAlertNotificationCommand) error {
|
|
||||||
return inTransaction(func(sess *xorm.Session) error {
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
|
existingQuery := &m.GetAlertNotificationQuery{OrgID: cmd.OrgID, Name: cmd.Name}
|
||||||
|
err := getAlertNotifications(existingQuery, sess)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(existingQuery.Result) > 0 {
|
||||||
|
return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
alertNotification := &m.AlertNotification{
|
||||||
|
OrgId: cmd.OrgID,
|
||||||
|
Name: cmd.Name,
|
||||||
|
Type: cmd.Type,
|
||||||
|
Created: time.Now(),
|
||||||
|
Settings: cmd.Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := sess.Insert(alertNotification)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
alertNotification.Id = id
|
||||||
|
cmd.Result = alertNotification
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
|
func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
|
||||||
|
return inTransaction(func(sess *xorm.Session) (err error) {
|
||||||
|
an := &m.AlertNotification{}
|
||||||
|
|
||||||
}*/
|
var has bool
|
||||||
|
has, err = sess.Id(cmd.Id).Get(an)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !has {
|
||||||
|
return fmt.Errorf("Alert notification does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
an.Name = cmd.Name
|
||||||
|
an.Type = cmd.Type
|
||||||
|
an.Settings = cmd.Settings
|
||||||
|
an.Updated = time.Now()
|
||||||
|
|
||||||
|
_, err = sess.Id(an.Id).Cols("name", "type", "settings", "updated").Update(an)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Result = an
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
func TestAlertNotificationSQLAccess(t *testing.T) {
|
func TestAlertNotificationSQLAccess(t *testing.T) {
|
||||||
Convey("Testing Alert notification sql access", t, func() {
|
Convey("Testing Alert notification sql access", t, func() {
|
||||||
InitTestDB(t)
|
InitTestDB(t)
|
||||||
|
var err error
|
||||||
|
|
||||||
Convey("Alert notifications should be empty", func() {
|
Convey("Alert notifications should be empty", func() {
|
||||||
cmd := &m.GetAlertNotificationQuery{
|
cmd := &m.GetAlertNotificationQuery{
|
||||||
@ -18,19 +19,49 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
|||||||
Name: "email",
|
Name: "email",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := GetAlertNotifications(cmd)
|
err := AlertNotificationQuery(cmd)
|
||||||
fmt.Printf("errror %v", err)
|
fmt.Printf("errror %v", err)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(cmd.Result), ShouldEqual, 0)
|
So(len(cmd.Result), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
/*
|
|
||||||
Convey("Can save Alert Notification", func() {
|
Convey("Can save Alert Notification", func() {
|
||||||
cmd := &m.CreateAlertNotificationCommand{}
|
cmd := &m.CreateAlertNotificationCommand{
|
||||||
|
Name: "ops",
|
||||||
var err error
|
Type: "email",
|
||||||
err = CreateAlertNotification(cmd)
|
}
|
||||||
|
|
||||||
|
err = CreateAlertNotificationCommand(cmd)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
}) */
|
So(cmd.Result.Id, ShouldNotEqual, 0)
|
||||||
|
|
||||||
|
Convey("Cannot save Alert Notification with the same name", func() {
|
||||||
|
err = CreateAlertNotificationCommand(cmd)
|
||||||
|
So(err, ShouldNotBeNil)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Cannot update alert notification that does not exist", func() {
|
||||||
|
newCmd := &m.UpdateAlertNotificationCommand{
|
||||||
|
Name: "NewName",
|
||||||
|
Type: cmd.Result.Type,
|
||||||
|
OrgID: cmd.Result.OrgId,
|
||||||
|
Id: 1337,
|
||||||
|
}
|
||||||
|
err = UpdateAlertNotification(newCmd)
|
||||||
|
So(err, ShouldNotBeNil)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can update alert notification", func() {
|
||||||
|
newCmd := &m.UpdateAlertNotificationCommand{
|
||||||
|
Name: "NewName",
|
||||||
|
Type: cmd.Result.Type,
|
||||||
|
OrgID: cmd.Result.OrgId,
|
||||||
|
Id: cmd.Result.Id,
|
||||||
|
}
|
||||||
|
err = UpdateAlertNotification(newCmd)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(newCmd.Result.Name, ShouldEqual, "NewName")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user