grafana/pkg/services/sqlstore/alert_notification.go

157 lines
3.8 KiB
Go
Raw Normal View History

2016-06-13 09:39:00 -05:00
package sqlstore
import (
"bytes"
"fmt"
"strconv"
"time"
2016-06-13 09:39:00 -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() {
bus.AddHandler("sql", AlertNotificationQuery)
bus.AddHandler("sql", CreateAlertNotificationCommand)
bus.AddHandler("sql", UpdateAlertNotification)
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
}
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)
}
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
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
}
func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
2016-06-13 09:39:00 -05:00
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)
}
2016-06-13 09:39:00 -05:00
alertNotification := &m.AlertNotification{
OrgId: cmd.OrgID,
Name: cmd.Name,
Type: cmd.Type,
Created: time.Now(),
Settings: cmd.Settings,
Updated: time.Now(),
}
2016-06-13 09:39:00 -05:00
_, err = sess.Insert(alertNotification)
if err != nil {
return err
}
//alertNotification.Id = int(id)
cmd.Result = alertNotification
return nil
2016-06-13 09:39:00 -05:00
})
}
func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
return inTransaction(func(sess *xorm.Session) (err error) {
current := &m.AlertNotification{}
_, err = sess.Id(cmd.Id).Get(current)
2016-06-13 09:39:00 -05:00
if err != nil {
return err
}
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()
alertNotification.Created = current.Created
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")
}
2016-06-14 01:47:42 -05:00
cmd.Result = alertNotification
return nil
})
}