feat(api): adds endpoint for mass pausing alerts

ref #6589
This commit is contained in:
bergquist
2016-12-15 17:01:45 +01:00
parent 830bf5a91c
commit fb6aa0e026
9 changed files with 117 additions and 39 deletions

View File

@@ -5,6 +5,8 @@ import (
"fmt"
"time"
"strings"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
@@ -246,25 +248,32 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error {
func PauseAlertRule(cmd *m.PauseAlertCommand) error {
return inTransaction(func(sess *xorm.Session) error {
alert := m.Alert{}
var buffer bytes.Buffer
params := make([]interface{}, 0)
buffer.WriteString(`UPDATE alert SET state = ?`)
has, err := x.Where("id = ? AND org_id=?", cmd.AlertId, cmd.OrgId).Get(&alert)
alertIdCount := len(cmd.AlertIds)
if alertIdCount == 1 {
buffer.WriteString(` WHERE id = ?`)
} else if alertIdCount > 1 {
buffer.WriteString(` WHERE id IN (?` + strings.Repeat(",?", len(cmd.AlertIds)-1) + `)`)
}
if cmd.Paused {
params = append(params, string(m.AlertStatePaused))
} else {
params = append(params, string(m.AlertStatePending))
}
for _, v := range cmd.AlertIds {
params = append(params, v)
}
res, err := sess.Exec(buffer.String(), params...)
if err != nil {
return err
} else if !has {
return fmt.Errorf("Could not find alert")
}
var newState m.AlertStateType
if cmd.Paused {
newState = m.AlertStatePaused
} else {
newState = m.AlertStatePending
}
alert.State = newState
sess.Id(alert.Id).Update(&alert)
cmd.ResultCount, _ = res.RowsAffected()
return nil
})
}

View File

@@ -1,18 +0,0 @@
package sqlstore
import (
"testing"
// m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestAlertingHeartbeatDataAccess(t *testing.T) {
Convey("Testing Alerting data access", t, func() {
InitTestDB(t)
//send heartbeat from server 1
//send heartbeat from server 2
})
}