2016-04-13 03:33:45 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2016-05-09 07:30:28 -05:00
|
|
|
"bytes"
|
2021-03-17 10:06:10 -05:00
|
|
|
"context"
|
2016-04-26 10:36:50 -05:00
|
|
|
"fmt"
|
2016-12-19 09:07:55 -06:00
|
|
|
"strings"
|
2016-12-19 06:24:45 -06:00
|
|
|
"time"
|
2016-12-15 10:01:45 -06:00
|
|
|
|
2016-04-13 03:33:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-04-13 03:33:45 -05:00
|
|
|
)
|
|
|
|
|
2018-02-16 06:06:28 -06:00
|
|
|
// timeNow makes it possible to test usage of time
|
|
|
|
var timeNow = time.Now
|
|
|
|
|
2016-04-13 03:33:45 -05:00
|
|
|
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-06-03 08:24:53 -05:00
|
|
|
bus.AddHandler("sql", GetAllAlertQueryHandler)
|
2016-07-22 06:14:09 -05:00
|
|
|
bus.AddHandler("sql", SetAlertState)
|
2016-09-30 10:37:47 -05:00
|
|
|
bus.AddHandler("sql", GetAlertStatesForDashboard)
|
2016-12-19 07:36:44 -06:00
|
|
|
bus.AddHandler("sql", PauseAlert)
|
|
|
|
bus.AddHandler("sql", PauseAllAlerts)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetAlertById(query *models.GetAlertByIdQuery) error {
|
|
|
|
alert := models.Alert{}
|
2018-01-23 15:28:47 -06:00
|
|
|
has, err := x.ID(query.Id).Get(&alert)
|
2016-04-26 10:36:50 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetAllAlertQueryHandler(query *models.GetAllAlertsQuery) error {
|
|
|
|
var alerts []*models.Alert
|
2018-09-16 05:26:05 -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
|
|
|
|
}
|
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
func deleteAlertByIdInternal(alertId int64, reason string, sess *DBSession) error {
|
2016-10-14 02:43:30 -05:00
|
|
|
sqlog.Debug("Deleting alert", "id", alertId, "reason", reason)
|
|
|
|
|
|
|
|
if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", alertId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Exec("DELETE FROM annotation WHERE alert_id = ?", alertId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-30 14:57:15 -05:00
|
|
|
if _, err := sess.Exec("DELETE FROM alert_notification_state WHERE alert_id = ?", alertId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-06 06:29:30 -05:00
|
|
|
if _, err := sess.Exec("DELETE FROM alert_rule_tag WHERE alert_id = ?", alertId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-14 02:43:30 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func HandleAlertsQuery(query *models.GetAlertsQuery) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
builder := SQLBuilder{}
|
2018-02-16 06:56:04 -06:00
|
|
|
|
|
|
|
builder.Write(`SELECT
|
|
|
|
alert.id,
|
|
|
|
alert.dashboard_id,
|
|
|
|
alert.panel_id,
|
|
|
|
alert.name,
|
|
|
|
alert.state,
|
|
|
|
alert.new_state_date,
|
2018-07-16 11:38:42 -05:00
|
|
|
alert.eval_data,
|
2018-02-16 06:56:04 -06:00
|
|
|
alert.eval_date,
|
|
|
|
alert.execution_error,
|
|
|
|
dashboard.uid as dashboard_uid,
|
|
|
|
dashboard.slug as dashboard_slug
|
|
|
|
FROM alert
|
|
|
|
INNER JOIN dashboard on dashboard.id = alert.dashboard_id `)
|
|
|
|
|
|
|
|
builder.Write(`WHERE alert.org_id = ?`, query.OrgId)
|
2016-05-09 07:30:28 -05:00
|
|
|
|
2018-06-01 07:36:40 -05:00
|
|
|
if len(strings.TrimSpace(query.Query)) > 0 {
|
|
|
|
builder.Write(" AND alert.name "+dialect.LikeStr()+" ?", "%"+query.Query+"%")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(query.DashboardIDs) > 0 {
|
|
|
|
builder.sql.WriteString(` AND alert.dashboard_id IN (?` + strings.Repeat(",?", len(query.DashboardIDs)-1) + `) `)
|
|
|
|
|
|
|
|
for _, dbID := range query.DashboardIDs {
|
|
|
|
builder.AddParams(dbID)
|
|
|
|
}
|
2016-05-10 02:45:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if query.PanelId != 0 {
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.Write(` AND alert.panel_id = ?`, query.PanelId)
|
2016-05-10 02:45:56 -05:00
|
|
|
}
|
2016-05-09 07:30:28 -05:00
|
|
|
|
2017-12-31 07:16:19 -06:00
|
|
|
if len(query.State) > 0 && query.State[0] != "all" {
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.Write(` AND (`)
|
2016-05-09 07:30:28 -05:00
|
|
|
for i, v := range query.State {
|
|
|
|
if i > 0 {
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.Write(" OR ")
|
2016-05-09 07:30:28 -05:00
|
|
|
}
|
2017-11-15 03:48:03 -06:00
|
|
|
if strings.HasPrefix(v, "not_") {
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.Write("state <> ? ")
|
2017-11-15 03:48:03 -06:00
|
|
|
v = strings.TrimPrefix(v, "not_")
|
2017-11-13 03:30:11 -06:00
|
|
|
} else {
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.Write("state = ? ")
|
2017-11-13 03:30:11 -06:00
|
|
|
}
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.AddParams(v)
|
2016-05-09 07:30:28 -05:00
|
|
|
}
|
2018-02-16 06:56:04 -06:00
|
|
|
builder.Write(")")
|
2016-05-09 07:30:28 -05:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
if query.User.OrgRole != models.ROLE_ADMIN {
|
2021-02-24 07:06:22 -06:00
|
|
|
builder.WriteDashboardPermissionFilter(query.User, models.PERMISSION_VIEW)
|
2018-02-16 06:56:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.Write(" ORDER BY name ASC")
|
2017-10-12 03:22:53 -05:00
|
|
|
|
2016-09-14 01:36:44 -05:00
|
|
|
if query.Limit != 0 {
|
2018-05-10 09:54:21 -05:00
|
|
|
builder.Write(dialect.Limit(query.Limit))
|
2016-09-14 01:36:44 -05:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
alerts := make([]*models.AlertListItemDTO, 0)
|
2020-11-10 23:21:08 -06:00
|
|
|
if err := x.SQL(builder.GetSQLString(), builder.params...).Find(&alerts); err != nil {
|
2016-04-26 10:36:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-27 08:21:02 -06:00
|
|
|
for i := range alerts {
|
2017-01-16 11:01:08 -06:00
|
|
|
if alerts[i].ExecutionError == " " {
|
|
|
|
alerts[i].ExecutionError = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 10:36:50 -05:00
|
|
|
query.Result = alerts
|
|
|
|
return nil
|
2016-04-13 03:33:45 -05:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:23:44 -05:00
|
|
|
func deleteAlertDefinition(dashboardId int64, sess *DBSession) error {
|
2020-02-29 06:35:15 -06:00
|
|
|
alerts := make([]*models.Alert, 0)
|
2019-10-22 07:08:18 -05:00
|
|
|
if err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-25 09:18:28 -05:00
|
|
|
|
2016-04-26 08:48:29 -05:00
|
|
|
for _, alert := range alerts {
|
2019-10-22 07:08:18 -05:00
|
|
|
if err := deleteAlertByIdInternal(alert.Id, "Dashboard deleted", sess); err != nil {
|
|
|
|
// If we return an error, the current transaction gets rolled back, so no use
|
|
|
|
// trying to delete more
|
|
|
|
return err
|
|
|
|
}
|
2016-04-26 08:48:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func (ss *SQLStore) SaveAlerts(dashID int64, alerts []*models.Alert) error {
|
|
|
|
return ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
|
|
|
|
existingAlerts, err := GetAlertsByDashboardId2(dashID, sess)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateAlerts(existingAlerts, alerts, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := deleteMissingAlerts(existingAlerts, alerts, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func SaveAlerts(cmd *models.SaveAlertsCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) 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
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
if err := updateAlerts(existingAlerts, cmd.Alerts, sess); err != nil {
|
2016-07-14 06:32:16 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
if err := deleteMissingAlerts(existingAlerts, cmd.Alerts, sess); err != nil {
|
2016-07-14 06:32:16 -05:00
|
|
|
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
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func updateAlerts(existingAlerts []*models.Alert, alerts []*models.Alert, sess *DBSession) error {
|
|
|
|
for _, alert := range alerts {
|
2016-04-26 09:31:13 -05:00
|
|
|
update := false
|
2020-02-29 06:35:15 -06:00
|
|
|
var alertToUpdate *models.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) {
|
2018-02-16 06:06:28 -06:00
|
|
|
alert.Updated = timeNow()
|
2016-04-28 01:42:51 -05:00
|
|
|
alert.State = alertToUpdate.State
|
2018-11-12 03:50:56 -06:00
|
|
|
sess.MustCols("message", "for")
|
2018-11-02 04:38:02 -05:00
|
|
|
|
2018-09-16 05:37:08 -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 {
|
2018-02-16 06:06:28 -06:00
|
|
|
alert.Updated = timeNow()
|
|
|
|
alert.Created = timeNow()
|
2020-02-29 06:35:15 -06:00
|
|
|
alert.State = models.AlertStateUnknown
|
2018-02-16 06:06:28 -06:00
|
|
|
alert.NewStateDate = timeNow()
|
2016-07-14 06:32:16 -05:00
|
|
|
|
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
|
|
|
}
|
2019-06-06 06:29:30 -05:00
|
|
|
tags := alert.GetTagsFromSettings()
|
|
|
|
if _, err := sess.Exec("DELETE FROM alert_rule_tag WHERE alert_id = ?", alert.Id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tags != nil {
|
|
|
|
tags, err := EnsureTagsExist(sess, tags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, tag := range tags {
|
|
|
|
if _, err := sess.Exec("INSERT INTO alert_rule_tag (alert_id, tag_id) VALUES(?,?)", alert.Id, tag.Id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 09:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-13 03:33:45 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func deleteMissingAlerts(alerts []*models.Alert, existingAlerts []*models.Alert, sess *DBSession) error {
|
2016-04-26 09:31:13 -05:00
|
|
|
for _, missingAlert := range alerts {
|
|
|
|
missing := true
|
2016-04-22 10:49:50 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
for _, k := range existingAlerts {
|
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 {
|
2019-10-22 07:08:18 -05:00
|
|
|
if err := deleteAlertByIdInternal(missingAlert.Id, "Removed from dashboard", sess); err != nil {
|
|
|
|
// No use trying to delete more, since we're in a transaction and it will be
|
|
|
|
// rolled back on error.
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetAlertsByDashboardId2(dashboardId int64, sess *DBSession) ([]*models.Alert, error) {
|
|
|
|
alerts := make([]*models.Alert, 0)
|
2016-04-26 08:48:29 -05:00
|
|
|
err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-02-29 06:35:15 -06:00
|
|
|
return []*models.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
|
|
|
}
|
2016-07-22 06:14:09 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func SetAlertState(cmd *models.SetAlertStateCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2020-02-29 06:35:15 -06:00
|
|
|
alert := models.Alert{}
|
2016-07-22 06:14:09 -05:00
|
|
|
|
2018-09-16 05:37:08 -05:00
|
|
|
if has, err := sess.ID(cmd.AlertId).Get(&alert); err != nil {
|
2016-07-22 06:14:09 -05:00
|
|
|
return err
|
|
|
|
} else if !has {
|
2020-11-05 04:57:20 -06:00
|
|
|
return fmt.Errorf("could not find alert")
|
2016-07-22 06:14:09 -05:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
if alert.State == models.AlertStatePaused {
|
|
|
|
return models.ErrCannotChangeStateOnPausedAlert
|
2016-12-19 06:24:45 -06:00
|
|
|
}
|
|
|
|
|
2017-02-21 07:47:02 -06:00
|
|
|
if alert.State == cmd.State {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrRequiresNewState
|
2017-02-21 07:47:02 -06:00
|
|
|
}
|
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
alert.State = cmd.State
|
2018-03-15 18:08:25 -05:00
|
|
|
alert.StateChanges++
|
2018-02-16 06:06:28 -06:00
|
|
|
alert.NewStateDate = timeNow()
|
2016-09-14 07:35:05 -05:00
|
|
|
alert.EvalData = cmd.EvalData
|
|
|
|
|
2016-08-18 10:25:32 -05:00
|
|
|
if cmd.Error == "" {
|
2020-09-22 09:22:19 -05:00
|
|
|
alert.ExecutionError = " " // without this space, xorm skips updating this field
|
2016-08-18 10:25:32 -05:00
|
|
|
} else {
|
|
|
|
alert.ExecutionError = cmd.Error
|
|
|
|
}
|
2016-07-22 06:14:09 -05:00
|
|
|
|
2019-07-22 06:23:27 -05:00
|
|
|
_, err := sess.ID(alert.Id).Update(&alert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-01 07:13:03 -05:00
|
|
|
|
|
|
|
cmd.Result = alert
|
2016-07-22 06:14:09 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2016-09-30 10:37:47 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func PauseAlert(cmd *models.PauseAlertCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2016-12-19 09:07:55 -06:00
|
|
|
if len(cmd.AlertIds) == 0 {
|
|
|
|
return fmt.Errorf("command contains no alertids")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2016-12-15 10:01:45 -06:00
|
|
|
params := make([]interface{}, 0)
|
2016-10-10 07:26:09 -05:00
|
|
|
|
2018-02-16 06:06:28 -06:00
|
|
|
buffer.WriteString(`UPDATE alert SET state = ?, new_state_date = ?`)
|
2016-10-10 07:26:09 -05:00
|
|
|
if cmd.Paused {
|
2020-02-29 06:35:15 -06:00
|
|
|
params = append(params, string(models.AlertStatePaused))
|
2019-10-08 09:25:15 -05:00
|
|
|
params = append(params, timeNow().UTC())
|
2016-10-10 07:26:09 -05:00
|
|
|
} else {
|
2020-02-29 06:35:15 -06:00
|
|
|
params = append(params, string(models.AlertStateUnknown))
|
2019-10-08 09:25:15 -05:00
|
|
|
params = append(params, timeNow().UTC())
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
|
|
|
|
2016-12-19 09:07:55 -06:00
|
|
|
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
|
|
|
|
for _, v := range cmd.AlertIds {
|
|
|
|
params = append(params, v)
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:57:29 -06:00
|
|
|
sqlOrArgs := append([]interface{}{buffer.String()}, params...)
|
|
|
|
|
|
|
|
res, err := sess.Exec(sqlOrArgs...)
|
2016-12-19 09:07:55 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.ResultCount, _ = res.RowsAffected()
|
|
|
|
return nil
|
2016-10-10 07:26:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func PauseAllAlerts(cmd *models.PauseAllAlertCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2016-12-19 06:24:45 -06:00
|
|
|
var newState string
|
|
|
|
if cmd.Paused {
|
2020-02-29 06:35:15 -06:00
|
|
|
newState = string(models.AlertStatePaused)
|
2016-12-19 06:24:45 -06:00
|
|
|
} else {
|
2020-02-29 06:35:15 -06:00
|
|
|
newState = string(models.AlertStateUnknown)
|
2016-12-19 06:24:45 -06:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:25:15 -05:00
|
|
|
res, err := sess.Exec(`UPDATE alert SET state = ?, new_state_date = ?`, newState, timeNow().UTC())
|
2016-12-19 06:24:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.ResultCount, _ = res.RowsAffected()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetAlertStatesForDashboard(query *models.GetAlertStatesForDashboardQuery) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = `SELECT
|
2016-09-30 10:37:47 -05:00
|
|
|
id,
|
|
|
|
dashboard_id,
|
|
|
|
panel_id,
|
|
|
|
state,
|
|
|
|
new_state_date
|
|
|
|
FROM alert
|
|
|
|
WHERE org_id = ? AND dashboard_id = ?`
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query.Result = make([]*models.AlertStateInfoDTO, 0)
|
2020-11-10 23:21:08 -06:00
|
|
|
err := x.SQL(rawSQL, query.OrgId, query.DashboardId).Find(&query.Result)
|
2016-09-30 10:37:47 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|