Skip to content

Commit ecdf188

Browse files
committed
feat(alerting): removes pause per datasource
1 parent 6b9db0c commit ecdf188

File tree

11 files changed

+83
-65
lines changed

11 files changed

+83
-65
lines changed

pkg/api/alerting.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -287,20 +287,11 @@ func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response {
287287
}
288288

289289
//POST /api/alerts/pause
290-
func PauseAlerts(c *middleware.Context, dto dtos.PauseAlertsCommand) Response {
291-
updateCmd := models.PauseAlertCommand{
292-
OrgId: c.OrgId,
290+
func PauseAlerts(c *middleware.Context, dto dtos.PauseAllAlertsCommand) Response {
291+
updateCmd := models.PauseAllAlertCommand{
293292
Paused: dto.Paused,
294293
}
295294

296-
if len(dto.DataSourceIds) > 0 {
297-
alertIdsToUpdate, err := getAlertIdsToUpdate(dto)
298-
if err != nil {
299-
return ApiError(500, "Failed to pause alerts", err)
300-
}
301-
updateCmd.AlertIds = alertIdsToUpdate
302-
}
303-
304295
if err := bus.Dispatch(&updateCmd); err != nil {
305296
return ApiError(500, "Failed to pause alerts", err)
306297
}
@@ -320,36 +311,3 @@ func PauseAlerts(c *middleware.Context, dto dtos.PauseAlertsCommand) Response {
320311

321312
return Json(200, result)
322313
}
323-
324-
func getAlertIdsToUpdate(pauseAlertCmd dtos.PauseAlertsCommand) ([]int64, error) {
325-
cmd := &models.GetAllAlertsQuery{}
326-
if err := bus.Dispatch(cmd); err != nil {
327-
return nil, err
328-
}
329-
330-
var alertIdsToUpdate []int64
331-
for _, alert := range cmd.Result {
332-
alert, err := alerting.NewRuleFromDBAlert(alert)
333-
if err != nil {
334-
return nil, err
335-
}
336-
337-
for _, condition := range alert.Conditions {
338-
id, exist := condition.GetDatasourceId()
339-
if exist && existInSlice(pauseAlertCmd.DataSourceIds, *id) {
340-
alertIdsToUpdate = append(alertIdsToUpdate, alert.Id)
341-
}
342-
}
343-
}
344-
345-
return alertIdsToUpdate, nil
346-
}
347-
348-
func existInSlice(slice []int64, value int64) bool {
349-
for _, v := range slice {
350-
if v == value {
351-
return true
352-
}
353-
}
354-
return false
355-
}

pkg/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ func Register(r *macaron.Macaron) {
256256
r.Group("/alerts", func() {
257257
r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest))
258258
r.Post("/:alertId/pause", bind(dtos.PauseAlertCommand{}), wrap(PauseAlert), reqEditorRole)
259-
r.Post("/pause", bind(dtos.PauseAlertsCommand{}), wrap(PauseAlerts), reqGrafanaAdmin)
260259
r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
261260
r.Get("/", wrap(GetAlerts))
262261
r.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
@@ -290,6 +289,7 @@ func Register(r *macaron.Macaron) {
290289
r.Get("/users/:id/quotas", wrap(GetUserQuotas))
291290
r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
292291
r.Get("/stats", AdminGetStats)
292+
r.Post("/pause-all-alerts", bind(dtos.PauseAllAlertsCommand{}), wrap(PauseAlerts))
293293
}, reqGrafanaAdmin)
294294

295295
// rendering

pkg/api/dtos/alerting.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ type PauseAlertCommand struct {
6565
Paused bool `json:"paused"`
6666
}
6767

68-
type PauseAlertsCommand struct {
69-
DataSourceIds []int64 `json:"datasourceId"`
70-
Paused bool `json:"paused"`
68+
type PauseAllAlertsCommand struct {
69+
Paused bool `json:"paused"`
7170
}

pkg/metrics/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var (
5050

5151
// Timers
5252
M_DataSource_ProxyReq_Timer Timer
53-
M_Alerting_Execution_Time Timer
53+
M_Alerting_Execution_Time Timer
5454

5555
// StatTotals
5656
M_Alerting_Active_Alerts Gauge

pkg/models/alert.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package models
33
import (
44
"time"
55

6+
"fmt"
7+
68
"github.com/grafana/grafana/pkg/components/simplejson"
79
)
810

@@ -31,6 +33,10 @@ const (
3133
ExecutionErrorKeepState ExecutionErrorOption = "keep_state"
3234
)
3335

36+
var (
37+
ErrCannotChangeStateOnPausedAlert error = fmt.Errorf("Cannot change state on pause alert")
38+
)
39+
3440
func (s AlertStateType) IsValid() bool {
3541
return s == AlertStateOK || s == AlertStateNoData || s == AlertStatePaused || s == AlertStatePending
3642
}
@@ -138,6 +144,11 @@ type PauseAlertCommand struct {
138144
Paused bool
139145
}
140146

147+
type PauseAllAlertCommand struct {
148+
ResultCount int64
149+
Paused bool
150+
}
151+
141152
type SetAlertStateCommand struct {
142153
AlertId int64
143154
OrgId int64

pkg/services/alerting/conditions/query.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ type AlertQuery struct {
3434
To string
3535
}
3636

37-
func (c *QueryCondition) GetDatasourceId() (datasourceId *int64, exist bool) {
38-
return &c.Query.DatasourceId, true
39-
}
40-
4137
func (c *QueryCondition) Eval(context *alerting.EvalContext) (*alerting.ConditionResult, error) {
4238
timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To)
4339

pkg/services/alerting/interfaces.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ type ConditionResult struct {
3030

3131
type Condition interface {
3232
Eval(result *EvalContext) (*ConditionResult, error)
33-
GetDatasourceId() (datasourceId *int64, exist bool)
3433
}

pkg/services/alerting/result_handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
8686
}
8787

8888
if err := bus.Dispatch(cmd); err != nil {
89+
if err == m.ErrCannotChangeStateOnPausedAlert {
90+
handler.log.Error("Cannot change state on alert thats pause", "error", err)
91+
return err
92+
}
8993
handler.log.Error("Failed to save state", "error", err)
9094
}
9195

pkg/services/alerting/rule_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ func (f *FakeCondition) Eval(context *EvalContext) (*ConditionResult, error) {
1414
return &ConditionResult{}, nil
1515
}
1616

17-
func (c *FakeCondition) GetDatasourceId() (datasourceId *int64, exist bool) {
18-
return nil, false
19-
}
20-
2117
func TestAlertRuleModel(t *testing.T) {
2218
Convey("Testing alert rule", t, func() {
2319

pkg/services/sqlstore/alert.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package sqlstore
33
import (
44
"bytes"
55
"fmt"
6-
"time"
7-
86
"strings"
7+
"time"
98

109
"github.com/go-xorm/xorm"
1110
"github.com/grafana/grafana/pkg/bus"
@@ -21,6 +20,7 @@ func init() {
2120
bus.AddHandler("sql", SetAlertState)
2221
bus.AddHandler("sql", GetAlertStatesForDashboard)
2322
bus.AddHandler("sql", PauseAlertRule)
23+
bus.AddHandler("sql", PauseAllAlertRule)
2424
}
2525

2626
func GetAlertById(query *m.GetAlertByIdQuery) error {
@@ -230,6 +230,10 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error {
230230
return fmt.Errorf("Could not find alert")
231231
}
232232

233+
if alert.State == m.AlertStatePaused {
234+
return m.ErrCannotChangeStateOnPausedAlert
235+
}
236+
233237
alert.State = cmd.State
234238
alert.StateChanges += 1
235239
alert.NewStateDate = time.Now()
@@ -248,6 +252,10 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error {
248252

249253
func PauseAlertRule(cmd *m.PauseAlertCommand) error {
250254
return inTransaction(func(sess *xorm.Session) error {
255+
if len(cmd.AlertIds) == 0 {
256+
return fmt.Errorf("command contains no alertids")
257+
}
258+
251259
var buffer bytes.Buffer
252260
params := make([]interface{}, 0)
253261

@@ -258,11 +266,9 @@ func PauseAlertRule(cmd *m.PauseAlertCommand) error {
258266
params = append(params, string(m.AlertStatePending))
259267
}
260268

261-
if len(cmd.AlertIds) > 0 {
262-
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
263-
for _, v := range cmd.AlertIds {
264-
params = append(params, v)
265-
}
269+
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
270+
for _, v := range cmd.AlertIds {
271+
params = append(params, v)
266272
}
267273

268274
res, err := sess.Exec(buffer.String(), params...)
@@ -274,6 +280,24 @@ func PauseAlertRule(cmd *m.PauseAlertCommand) error {
274280
})
275281
}
276282

283+
func PauseAllAlertRule(cmd *m.PauseAllAlertCommand) error {
284+
return inTransaction(func(sess *xorm.Session) error {
285+
var newState string
286+
if cmd.Paused {
287+
newState = string(m.AlertStatePaused)
288+
} else {
289+
newState = string(m.AlertStatePending)
290+
}
291+
292+
res, err := sess.Exec(`UPDATE alert SET state = ?`, newState)
293+
if err != nil {
294+
return err
295+
}
296+
cmd.ResultCount, _ = res.RowsAffected()
297+
return nil
298+
})
299+
}
300+
277301
func GetAlertStatesForDashboard(query *m.GetAlertStatesForDashboardQuery) error {
278302
var rawSql = `SELECT
279303
id,

pkg/services/sqlstore/alert_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ func TestAlertingDataAccess(t *testing.T) {
3939
So(err, ShouldBeNil)
4040
})
4141

42+
Convey("Can set new states", func() {
43+
Convey("new state ok", func() {
44+
cmd := &m.SetAlertStateCommand{
45+
AlertId: 1,
46+
State: m.AlertStateOK,
47+
}
48+
49+
err = SetAlertState(cmd)
50+
So(err, ShouldBeNil)
51+
})
52+
53+
Convey("can pause alert", func() {
54+
cmd := &m.PauseAllAlertCommand{
55+
Paused: true,
56+
}
57+
58+
err = PauseAllAlertRule(cmd)
59+
So(err, ShouldBeNil)
60+
61+
Convey("cannot updated paused alert", func() {
62+
cmd := &m.SetAlertStateCommand{
63+
AlertId: 1,
64+
State: m.AlertStateOK,
65+
}
66+
67+
err = SetAlertState(cmd)
68+
So(err, ShouldNotBeNil)
69+
})
70+
})
71+
})
72+
4273
Convey("Can read properties", func() {
4374
alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1}
4475
err2 := HandleAlertsQuery(&alertQuery)

0 commit comments

Comments
 (0)