2016-04-13 03:33:45 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2016-05-09 07:30:28 -05:00
|
|
|
"bytes"
|
2016-04-26 10:36:50 -05:00
|
|
|
"fmt"
|
2016-05-16 08:39:09 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-04-25 09:18:28 -05:00
|
|
|
"github.com/go-xorm/xorm"
|
2016-04-13 03:33:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", SaveAlerts)
|
2016-05-09 07:30:28 -05:00
|
|
|
bus.AddHandler("sql", HandleAlertsQuery)
|
2016-04-26 10:36:50 -05:00
|
|
|
bus.AddHandler("sql", GetAlertById)
|
2016-05-02 09:07:19 -05:00
|
|
|
bus.AddHandler("sql", DeleteAlertById)
|
2016-06-03 08:24:53 -05:00
|
|
|
bus.AddHandler("sql", GetAllAlertQueryHandler)
|
2016-06-09 03:00:34 -05:00
|
|
|
//bus.AddHandler("sql", HeartBeat)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2016-06-09 03:00:34 -05:00
|
|
|
/*
|
|
|
|
func HeartBeat(query *m.HeartBeatCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
now := time.Now().Sub(0, 0, 0, 5)
|
|
|
|
activeTime := time.Now().Sub(0, 0, 0, 5)
|
|
|
|
ownHeartbeats := make([]m.HeartBeat, 0)
|
|
|
|
err := x.Where("server_id = ?", query.ServerId).Find(&ownHeartbeats)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len(ownHeartbeats)) > 0 && ownHeartbeats[0].Updated > activeTime {
|
|
|
|
//update
|
|
|
|
x.Insert(&m.HeartBeat{ServerId: query.ServerId, Created: now, Updated: now})
|
|
|
|
} else {
|
|
|
|
thisServer := ownHeartbeats[0]
|
|
|
|
thisServer.Updated = now
|
|
|
|
x.Id(thisServer.Id).Update(&thisServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
activeServers := make([]m.HeartBeat, 0)
|
|
|
|
err = x.Where("server_id = ? and updated > ", query.ServerId, now.String()).OrderBy("id").Find(&activeServers)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, pos := range activeServers {
|
|
|
|
if pos.ServerId == query.ServerId {
|
|
|
|
query.Result = &m.AlertingClusterInfo{
|
|
|
|
ClusterSize: len(activeServers),
|
|
|
|
UptimePosition: i,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-04-28 01:23:50 -05:00
|
|
|
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
2016-06-11 03:26:48 -05:00
|
|
|
alert := m.Alert{}
|
2016-04-26 10:36:50 -05:00
|
|
|
has, err := x.Id(query.Id).Get(&alert)
|
|
|
|
if !has {
|
|
|
|
return fmt.Errorf("could not find alert")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-28 03:59:46 -05:00
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
query.Result = &alert
|
2016-04-26 10:36:50 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 08:24:53 -05:00
|
|
|
func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
|
2016-06-11 03:26:48 -05:00
|
|
|
var alerts []*m.Alert
|
2016-06-11 03:54:24 -05:00
|
|
|
err := x.Sql("select * from alert").Find(&alerts)
|
2016-06-03 08:24:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = alerts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-02 09:07:19 -05:00
|
|
|
func DeleteAlertById(cmd *m.DeleteAlertCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
2016-06-11 03:54:24 -05:00
|
|
|
if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", cmd.AlertId); err != nil {
|
2016-05-02 09:07:19 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:30:28 -05:00
|
|
|
func HandleAlertsQuery(query *m.GetAlertsQuery) error {
|
|
|
|
var sql bytes.Buffer
|
|
|
|
params := make([]interface{}, 0)
|
|
|
|
|
|
|
|
sql.WriteString(`SELECT *
|
2016-06-11 03:54:24 -05:00
|
|
|
from alert
|
2016-05-09 07:30:28 -05:00
|
|
|
`)
|
|
|
|
|
|
|
|
sql.WriteString(`WHERE org_id = ?`)
|
|
|
|
params = append(params, query.OrgId)
|
|
|
|
|
2016-05-10 02:45:56 -05:00
|
|
|
if query.DashboardId != 0 {
|
|
|
|
sql.WriteString(` AND dashboard_id = ?`)
|
|
|
|
params = append(params, query.DashboardId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.PanelId != 0 {
|
|
|
|
sql.WriteString(` AND panel_id = ?`)
|
|
|
|
params = append(params, query.PanelId)
|
|
|
|
}
|
2016-05-09 07:30:28 -05:00
|
|
|
|
2016-05-10 02:45:56 -05:00
|
|
|
if len(query.State) > 0 {
|
2016-05-09 07:30:28 -05:00
|
|
|
sql.WriteString(` AND (`)
|
|
|
|
for i, v := range query.State {
|
|
|
|
if i > 0 {
|
|
|
|
sql.WriteString(" OR ")
|
|
|
|
}
|
|
|
|
sql.WriteString("state = ? ")
|
|
|
|
params = append(params, strings.ToUpper(v))
|
|
|
|
}
|
|
|
|
sql.WriteString(")")
|
|
|
|
}
|
|
|
|
|
2016-06-11 03:26:48 -05:00
|
|
|
alerts := make([]*m.Alert, 0)
|
2016-05-09 07:30:28 -05:00
|
|
|
if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil {
|
2016-04-26 10:36:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = alerts
|
|
|
|
return nil
|
2016-04-13 03:33:45 -05:00
|
|
|
}
|
|
|
|
|
2016-04-26 08:48:29 -05:00
|
|
|
func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
|
2016-06-11 03:54:24 -05:00
|
|
|
alerts := make([]*m.Alert, 0)
|
2016-04-26 08:48:29 -05:00
|
|
|
sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
2016-04-25 09:18:28 -05:00
|
|
|
|
2016-04-26 08:48:29 -05:00
|
|
|
for _, alert := range alerts {
|
2016-06-11 03:54:24 -05:00
|
|
|
_, err := sess.Exec("DELETE FROM alert WHERE id = ? ", alert.Id)
|
2016-04-25 09:18:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-11 03:54:24 -05:00
|
|
|
sqlog.Debug("Alert deleted (due to dashboard deletion)", "name", alert.Name, "id", alert.Id)
|
2016-04-26 08:48:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-13 03:33:45 -05:00
|
|
|
func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
2016-04-26 08:48:29 -05:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
2016-07-14 06:32:16 -05:00
|
|
|
existingAlerts, err := GetAlertsByDashboardId2(cmd.DashboardId, sess)
|
2016-04-26 08:48:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-22 09:51:24 -05:00
|
|
|
|
2016-07-14 06:32:16 -05:00
|
|
|
if err := upsertAlerts(existingAlerts, cmd, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := deleteMissingAlerts(existingAlerts, cmd, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-22 09:51:24 -05:00
|
|
|
|
2016-04-26 09:31:13 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2016-04-26 08:48:29 -05:00
|
|
|
|
2016-07-14 06:32:16 -05:00
|
|
|
func upsertAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error {
|
2016-06-28 16:40:58 -05:00
|
|
|
for _, alert := range cmd.Alerts {
|
2016-04-26 09:31:13 -05:00
|
|
|
update := false
|
2016-06-11 03:26:48 -05:00
|
|
|
var alertToUpdate *m.Alert
|
2016-04-25 09:18:28 -05:00
|
|
|
|
2016-07-14 06:32:16 -05:00
|
|
|
for _, k := range existingAlerts {
|
2016-04-26 09:31:13 -05:00
|
|
|
if alert.PanelId == k.PanelId {
|
|
|
|
update = true
|
|
|
|
alert.Id = k.Id
|
|
|
|
alertToUpdate = k
|
2016-06-11 07:08:55 -05:00
|
|
|
break
|
2016-04-26 09:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 08:48:29 -05:00
|
|
|
|
2016-04-26 09:31:13 -05:00
|
|
|
if update {
|
2016-06-09 15:21:28 -05:00
|
|
|
if alertToUpdate.ContainsUpdates(alert) {
|
2016-05-16 08:39:09 -05:00
|
|
|
alert.Updated = time.Now()
|
2016-04-28 01:42:51 -05:00
|
|
|
alert.State = alertToUpdate.State
|
2016-06-06 06:50:47 -05:00
|
|
|
_, err := sess.Id(alert.Id).Update(alert)
|
2016-04-25 09:18:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-26 09:31:13 -05:00
|
|
|
|
2016-06-11 03:54:24 -05:00
|
|
|
sqlog.Debug("Alert updated", "name", alert.Name, "id", alert.Id)
|
2016-04-26 09:31:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-16 08:39:09 -05:00
|
|
|
alert.Updated = time.Now()
|
|
|
|
alert.Created = time.Now()
|
2016-07-14 06:32:16 -05:00
|
|
|
alert.State = "UNKNOWN"
|
|
|
|
alert.CreatedBy = cmd.UserId
|
|
|
|
alert.UpdatedBy = cmd.UserId
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
_, err := sess.Insert(alert)
|
2016-04-26 09:31:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-22 09:51:24 -05:00
|
|
|
}
|
2016-06-11 03:54:24 -05:00
|
|
|
|
|
|
|
sqlog.Debug("Alert inserted", "name", alert.Name, "id", alert.Id)
|
2016-04-13 03:33:45 -05:00
|
|
|
}
|
2016-04-26 09:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-13 03:33:45 -05:00
|
|
|
|
2016-06-28 16:40:58 -05:00
|
|
|
func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error {
|
2016-04-26 09:31:13 -05:00
|
|
|
for _, missingAlert := range alerts {
|
|
|
|
missing := true
|
2016-04-22 10:49:50 -05:00
|
|
|
|
2016-06-28 16:40:58 -05:00
|
|
|
for _, k := range cmd.Alerts {
|
2016-04-26 09:31:13 -05:00
|
|
|
if missingAlert.PanelId == k.PanelId {
|
|
|
|
missing = false
|
2016-06-11 07:08:55 -05:00
|
|
|
break
|
2016-04-22 10:49:50 -05:00
|
|
|
}
|
2016-04-26 09:31:13 -05:00
|
|
|
}
|
2016-04-22 10:49:50 -05:00
|
|
|
|
2016-04-26 09:31:13 -05:00
|
|
|
if missing {
|
2016-06-11 03:54:24 -05:00
|
|
|
_, err := sess.Exec("DELETE FROM alert WHERE id = ?", missingAlert.Id)
|
2016-04-26 09:31:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-22 10:49:50 -05:00
|
|
|
|
2016-06-11 03:54:24 -05:00
|
|
|
sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id)
|
2016-04-22 10:49:50 -05:00
|
|
|
}
|
2016-04-26 09:31:13 -05:00
|
|
|
}
|
2016-04-26 08:48:29 -05:00
|
|
|
|
2016-04-26 09:31:13 -05:00
|
|
|
return nil
|
2016-04-26 08:48:29 -05:00
|
|
|
}
|
|
|
|
|
2016-06-11 03:26:48 -05:00
|
|
|
func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.Alert, error) {
|
|
|
|
alerts := make([]*m.Alert, 0)
|
2016-04-26 08:48:29 -05:00
|
|
|
err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-06-11 03:26:48 -05:00
|
|
|
return []*m.Alert{}, err
|
2016-04-22 10:49:50 -05:00
|
|
|
}
|
|
|
|
|
2016-04-26 08:48:29 -05:00
|
|
|
return alerts, nil
|
2016-04-13 03:33:45 -05:00
|
|
|
}
|