feat(alerting): converter for db model to notification

This commit is contained in:
bergquist
2016-06-14 16:56:14 +02:00
parent 6eca26e8ec
commit 9a8416416d
5 changed files with 259 additions and 30 deletions

View File

@@ -3,6 +3,7 @@ package sqlstore
import (
"bytes"
"fmt"
"strconv"
"time"
"github.com/go-xorm/xorm"
@@ -43,6 +44,25 @@ func getAlertNotifications(query *m.GetAlertNotificationQuery, sess *xorm.Sessio
params = append(params, query.Name)
}
if query.Id != 0 {
sql.WriteString(` AND alert_notification.id = ?`)
params = append(params, strconv.Itoa(int(query.Id)))
}
if len(query.Ids) > 0 {
sql.WriteString(` AND (`)
for i, id := range query.Ids {
if i != 0 {
sql.WriteString(` OR`)
}
sql.WriteString(` alert_notification.id = ?`)
params = append(params, id)
}
sql.WriteString(`)`)
}
var result []*m.AlertNotification
if err := sess.Sql(sql.String(), params...).Find(&result); err != nil {
return err
@@ -71,15 +91,16 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
Type: cmd.Type,
Created: time.Now(),
Settings: cmd.Settings,
Updated: time.Now(),
}
id, err := sess.Insert(alertNotification)
_, err = sess.Insert(alertNotification)
if err != nil {
return err
}
alertNotification.Id = id
//alertNotification.Id = int(id)
cmd.Result = alertNotification
return nil
})
@@ -87,30 +108,34 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
return inTransaction(func(sess *xorm.Session) (err error) {
alertNotification := &m.AlertNotification{}
var has bool
has, err = sess.Id(cmd.Id).Get(alertNotification)
current := &m.AlertNotification{}
_, err = sess.Id(cmd.Id).Get(current)
if err != nil {
return err
}
if !has {
return fmt.Errorf("Alert notification does not exist")
}
alertNotification := &m.AlertNotification{}
alertNotification.Id = cmd.Id
alertNotification.OrgId = cmd.OrgID
alertNotification.Name = cmd.Name
alertNotification.Type = cmd.Type
alertNotification.Settings = cmd.Settings
alertNotification.Updated = time.Now()
alertNotification.Created = current.Created
_, err = sess.Id(alertNotification.Id).Cols("name", "type", "settings", "updated").Update(alertNotification)
var affected int64
//affected, err = sess.Id(alertNotification.Id).Cols("name", "type", "settings", "updated").Update(alertNotification)
affected, err = sess.Id(alertNotification.Id).Update(alertNotification)
if err != nil {
return err
}
if affected == 0 {
return fmt.Errorf("Could not find alert notification")
}
cmd.Result = alertNotification
return nil
})

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -27,41 +28,62 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
Convey("Can save Alert Notification", func() {
cmd := &m.CreateAlertNotificationCommand{
Name: "ops",
Type: "email",
Name: "ops",
Type: "email",
OrgID: 1,
Settings: simplejson.New(),
}
err = CreateAlertNotificationCommand(cmd)
So(err, ShouldBeNil)
So(cmd.Result.Id, ShouldNotEqual, 0)
So(cmd.Result.OrgId, ShouldNotEqual, 0)
So(cmd.Result.Type, ShouldEqual, "email")
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,
Name: "NewName",
Type: "webhook",
OrgID: cmd.Result.OrgId,
Settings: simplejson.New(),
Id: cmd.Result.Id,
}
err = UpdateAlertNotification(newCmd)
err := UpdateAlertNotification(newCmd)
So(err, ShouldBeNil)
So(newCmd.Result.Name, ShouldEqual, "NewName")
})
})
Convey("Can search using an array of ids", func() {
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
Name: "ops2",
Type: "email",
OrgID: 1,
Settings: simplejson.New(),
}), ShouldBeNil)
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
Name: "slack",
Type: "webhook",
OrgID: 1,
Settings: simplejson.New(),
}), ShouldBeNil)
Convey("search", func() {
query := &m.GetAlertNotificationQuery{
Ids: []int64{1, 2, 3},
OrgID: 1,
}
err := AlertNotificationQuery(query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
})
})
})
}