2016-06-13 09:39:00 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-06-14 01:33:50 -05:00
|
|
|
"fmt"
|
2016-06-14 09:56:14 -05:00
|
|
|
"strconv"
|
2016-06-14 01:33:50 -05:00
|
|
|
"time"
|
2016-06-13 09:39:00 -05:00
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
"github.com/go-xorm/xorm"
|
2016-06-13 09:39:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-06-14 01:33:50 -05:00
|
|
|
bus.AddHandler("sql", AlertNotificationQuery)
|
|
|
|
bus.AddHandler("sql", CreateAlertNotificationCommand)
|
|
|
|
bus.AddHandler("sql", UpdateAlertNotification)
|
2016-06-16 08:21:44 -05:00
|
|
|
bus.AddHandler("sql", DeleteAlertNotification)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
|
|
|
|
_, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2016-06-13 09:39:00 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
func AlertNotificationQuery(query *m.GetAlertNotificationQuery) error {
|
|
|
|
return getAlertNotifications(query, x.NewSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAlertNotifications(query *m.GetAlertNotificationQuery, sess *xorm.Session) error {
|
2016-06-13 09:39:00 -05:00
|
|
|
var sql bytes.Buffer
|
|
|
|
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
|
|
|
|
`)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
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(`)`)
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:39:00 -05:00
|
|
|
var result []*m.AlertNotification
|
2016-06-14 01:33:50 -05:00
|
|
|
if err := sess.Sql(sql.String(), params...).Find(&result); err != nil {
|
2016-06-13 09:39:00 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = result
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
|
2016-06-13 09:39:00 -05:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
2016-06-14 01:33:50 -05:00
|
|
|
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)
|
|
|
|
}
|
2016-06-13 09:39:00 -05:00
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
alertNotification := &m.AlertNotification{
|
|
|
|
OrgId: cmd.OrgID,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Created: time.Now(),
|
|
|
|
Settings: cmd.Settings,
|
2016-06-14 09:56:14 -05:00
|
|
|
Updated: time.Now(),
|
2016-06-14 01:33:50 -05:00
|
|
|
}
|
2016-06-13 09:39:00 -05:00
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
_, err = sess.Insert(alertNotification)
|
2016-06-14 01:33:50 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
//alertNotification.Id = int(id)
|
2016-06-14 01:33:50 -05:00
|
|
|
cmd.Result = alertNotification
|
|
|
|
return nil
|
2016-06-13 09:39:00 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
|
2016-06-14 01:33:50 -05:00
|
|
|
return inTransaction(func(sess *xorm.Session) (err error) {
|
2016-06-14 09:56:14 -05:00
|
|
|
current := &m.AlertNotification{}
|
|
|
|
_, err = sess.Id(cmd.Id).Get(current)
|
2016-06-13 09:39:00 -05:00
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
alertNotification := &m.AlertNotification{}
|
|
|
|
alertNotification.Id = cmd.Id
|
|
|
|
alertNotification.OrgId = cmd.OrgID
|
2016-06-14 01:47:42 -05:00
|
|
|
alertNotification.Name = cmd.Name
|
|
|
|
alertNotification.Type = cmd.Type
|
|
|
|
alertNotification.Settings = cmd.Settings
|
|
|
|
alertNotification.Updated = time.Now()
|
2016-06-14 09:56:14 -05:00
|
|
|
alertNotification.Created = current.Created
|
2016-06-14 01:33:50 -05:00
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
var affected int64
|
|
|
|
//affected, err = sess.Id(alertNotification.Id).Cols("name", "type", "settings", "updated").Update(alertNotification)
|
|
|
|
affected, err = sess.Id(alertNotification.Id).Update(alertNotification)
|
2016-06-14 01:33:50 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
if affected == 0 {
|
|
|
|
return fmt.Errorf("Could not find alert notification")
|
|
|
|
}
|
|
|
|
|
2016-06-14 01:47:42 -05:00
|
|
|
cmd.Result = alertNotification
|
2016-06-14 01:33:50 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|