feat(alerting): add support for alert page filter

This commit is contained in:
bergquist
2016-05-09 14:44:44 +02:00
parent 3e462f2914
commit 1be513fabd
9 changed files with 85 additions and 35 deletions
+30 -3
View File
@@ -1,15 +1,17 @@
package sqlstore
import (
"bytes"
"fmt"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"strings"
)
func init() {
bus.AddHandler("sql", SaveAlerts)
bus.AddHandler("sql", GetAllAlertsForOrg)
bus.AddHandler("sql", HandleAlertsQuery)
bus.AddHandler("sql", GetAlertById)
bus.AddHandler("sql", GetAlertsByDashboardId)
bus.AddHandler("sql", GetAlertsByDashboardAndPanelId)
@@ -41,9 +43,33 @@ func DeleteAlertById(cmd *m.DeleteAlertCommand) error {
})
}
func GetAllAlertsForOrg(query *m.GetAlertsQuery) error {
func HandleAlertsQuery(query *m.GetAlertsQuery) error {
var sql bytes.Buffer
params := make([]interface{}, 0)
sql.WriteString(`SELECT *
from alert_rule
`)
sql.WriteString(`WHERE org_id = ?`)
params = append(params, query.OrgId)
if len(query.State) > 0 {
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(")")
}
alerts := make([]m.AlertRule, 0)
if err := x.Where("org_id = ?", query.OrgId).Find(&alerts); err != nil {
if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil {
return err
}
@@ -127,6 +153,7 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
}
} else {
alert.State = "OK"
_, err := sess.Insert(&alert)
if err != nil {
return err
-1
View File
@@ -30,7 +30,6 @@ func TestAlertingDataAccess(t *testing.T) {
Description: "Alerting description",
QueryRange: "5m",
Aggregator: "avg",
State: "OK",
},
}
+1 -1
View File
@@ -47,7 +47,7 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
})
}
func GetAlertStateLogByAlertId(cmd *m.GetAlertsStateCommand) error {
func GetAlertStateLogByAlertId(cmd *m.GetAlertsStateQuery) error {
alertLogs := make([]m.AlertState, 0)
if err := x.Where("alert_id = ?", cmd.AlertId).Desc("created").Find(&alertLogs); err != nil {
+12 -1
View File
@@ -82,7 +82,7 @@ func TestAlertingStateAccess(t *testing.T) {
})
Convey("should have two event state logs", func() {
query := &m.GetAlertsStateCommand{
query := &m.GetAlertsStateQuery{
AlertId: 1,
OrgId: 1,
}
@@ -92,6 +92,17 @@ func TestAlertingStateAccess(t *testing.T) {
So(len(*query.Result), ShouldEqual, 2)
})
Convey("should not get any alerts with critical state", func() {
query := &m.GetAlertsQuery{
OrgId: 1,
State: []string{"Critical"},
}
err := HandleAlertsQuery(query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 0)
})
})
})
})