mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): removes pause per datasource
This commit is contained in:
@@ -34,10 +34,6 @@ type AlertQuery struct {
|
||||
To string
|
||||
}
|
||||
|
||||
func (c *QueryCondition) GetDatasourceId() (datasourceId *int64, exist bool) {
|
||||
return &c.Query.DatasourceId, true
|
||||
}
|
||||
|
||||
func (c *QueryCondition) Eval(context *alerting.EvalContext) (*alerting.ConditionResult, error) {
|
||||
timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To)
|
||||
|
||||
|
||||
@@ -30,5 +30,4 @@ type ConditionResult struct {
|
||||
|
||||
type Condition interface {
|
||||
Eval(result *EvalContext) (*ConditionResult, error)
|
||||
GetDatasourceId() (datasourceId *int64, exist bool)
|
||||
}
|
||||
|
||||
@@ -86,6 +86,10 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(cmd); err != nil {
|
||||
if err == m.ErrCannotChangeStateOnPausedAlert {
|
||||
handler.log.Error("Cannot change state on alert thats pause", "error", err)
|
||||
return err
|
||||
}
|
||||
handler.log.Error("Failed to save state", "error", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,6 @@ func (f *FakeCondition) Eval(context *EvalContext) (*ConditionResult, error) {
|
||||
return &ConditionResult{}, nil
|
||||
}
|
||||
|
||||
func (c *FakeCondition) GetDatasourceId() (datasourceId *int64, exist bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func TestAlertRuleModel(t *testing.T) {
|
||||
Convey("Testing alert rule", t, func() {
|
||||
|
||||
|
||||
@@ -3,9 +3,8 @@ package sqlstore
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -21,6 +20,7 @@ func init() {
|
||||
bus.AddHandler("sql", SetAlertState)
|
||||
bus.AddHandler("sql", GetAlertStatesForDashboard)
|
||||
bus.AddHandler("sql", PauseAlertRule)
|
||||
bus.AddHandler("sql", PauseAllAlertRule)
|
||||
}
|
||||
|
||||
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
||||
@@ -230,6 +230,10 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error {
|
||||
return fmt.Errorf("Could not find alert")
|
||||
}
|
||||
|
||||
if alert.State == m.AlertStatePaused {
|
||||
return m.ErrCannotChangeStateOnPausedAlert
|
||||
}
|
||||
|
||||
alert.State = cmd.State
|
||||
alert.StateChanges += 1
|
||||
alert.NewStateDate = time.Now()
|
||||
@@ -248,6 +252,10 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error {
|
||||
|
||||
func PauseAlertRule(cmd *m.PauseAlertCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
if len(cmd.AlertIds) == 0 {
|
||||
return fmt.Errorf("command contains no alertids")
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
@@ -258,11 +266,9 @@ func PauseAlertRule(cmd *m.PauseAlertCommand) error {
|
||||
params = append(params, string(m.AlertStatePending))
|
||||
}
|
||||
|
||||
if len(cmd.AlertIds) > 0 {
|
||||
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
|
||||
for _, v := range cmd.AlertIds {
|
||||
params = append(params, v)
|
||||
}
|
||||
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
|
||||
for _, v := range cmd.AlertIds {
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
res, err := sess.Exec(buffer.String(), params...)
|
||||
@@ -274,6 +280,24 @@ func PauseAlertRule(cmd *m.PauseAlertCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func PauseAllAlertRule(cmd *m.PauseAllAlertCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var newState string
|
||||
if cmd.Paused {
|
||||
newState = string(m.AlertStatePaused)
|
||||
} else {
|
||||
newState = string(m.AlertStatePending)
|
||||
}
|
||||
|
||||
res, err := sess.Exec(`UPDATE alert SET state = ?`, newState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.ResultCount, _ = res.RowsAffected()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func GetAlertStatesForDashboard(query *m.GetAlertStatesForDashboardQuery) error {
|
||||
var rawSql = `SELECT
|
||||
id,
|
||||
|
||||
@@ -39,6 +39,37 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Can set new states", func() {
|
||||
Convey("new state ok", func() {
|
||||
cmd := &m.SetAlertStateCommand{
|
||||
AlertId: 1,
|
||||
State: m.AlertStateOK,
|
||||
}
|
||||
|
||||
err = SetAlertState(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("can pause alert", func() {
|
||||
cmd := &m.PauseAllAlertCommand{
|
||||
Paused: true,
|
||||
}
|
||||
|
||||
err = PauseAllAlertRule(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("cannot updated paused alert", func() {
|
||||
cmd := &m.SetAlertStateCommand{
|
||||
AlertId: 1,
|
||||
State: m.AlertStateOK,
|
||||
}
|
||||
|
||||
err = SetAlertState(cmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can read properties", func() {
|
||||
alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1}
|
||||
err2 := HandleAlertsQuery(&alertQuery)
|
||||
|
||||
Reference in New Issue
Block a user