feat(alerting): add api endpoint for alert state

This commit is contained in:
bergquist
2016-04-28 08:23:50 +02:00
parent 6a5ecb3fca
commit e7be7d2835
11 changed files with 230 additions and 56 deletions

View File

@@ -13,7 +13,7 @@ func init() {
bus.AddHandler("sql", GetAlertById)
}
func GetAlertById(query *m.GetAlertById) error {
func GetAlertById(query *m.GetAlertByIdQuery) error {
alert := m.AlertRule{}
has, err := x.Id(query.Id).Get(&alert)
@@ -68,7 +68,7 @@ func alertIsDifferent(rule1, rule2 m.AlertRule) bool {
result = result || rule1.Title != rule2.Title
result = result || rule1.Description != rule2.Description
result = result || rule1.QueryRange != rule2.QueryRange
result = result || rule1.DatasourceName != rule2.DatasourceName
//don't compare .State! That would be insane.
return result
}
@@ -81,7 +81,6 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
}
upsertAlerts(alerts, cmd.Alerts, sess)
deleteMissingAlerts(alerts, cmd.Alerts, sess)
return nil

View File

@@ -16,19 +16,18 @@ func TestAlertingDataAccess(t *testing.T) {
items := []m.AlertRule{
{
PanelId: 1,
DashboardId: testDash.Id,
OrgId: testDash.OrgId,
Query: "Query",
QueryRefId: "A",
WarnLevel: "> 30",
CritLevel: "> 50",
Interval: "10",
Title: "Alerting title",
Description: "Alerting description",
QueryRange: "5m",
Aggregator: "avg",
DatasourceName: "graphite",
PanelId: 1,
DashboardId: testDash.Id,
OrgId: testDash.OrgId,
Query: "Query",
QueryRefId: "A",
WarnLevel: "> 30",
CritLevel: "> 50",
Interval: "10",
Title: "Alerting title",
Description: "Alerting description",
QueryRange: "5m",
Aggregator: "avg",
},
}
@@ -63,7 +62,6 @@ func TestAlertingDataAccess(t *testing.T) {
So(alert.Description, ShouldEqual, "Alerting description")
So(alert.QueryRange, ShouldEqual, "5m")
So(alert.Aggregator, ShouldEqual, "avg")
So(alert.DatasourceName, ShouldEqual, "graphite")
})
Convey("Alerts with same dashboard id and panel id should update", func() {

View File

@@ -0,0 +1,39 @@
package sqlstore
import (
"fmt"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", SetNewAlertState)
}
func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
return inTransaction(func(sess *xorm.Session) error {
if !cmd.IsValidState() {
return fmt.Errorf("new state is invalid")
}
alert := m.AlertRule{}
has, err := sess.Id(cmd.AlertId).Get(&alert)
if !has {
return fmt.Errorf("Could not find alert")
}
if err != nil {
return err
}
alert.State = cmd.NewState
sess.Id(alert.Id).Update(&alert)
//update alert
//insert alert state log
cmd.Result = &alert
return nil
})
}

View File

@@ -0,0 +1,85 @@
package sqlstore
import (
"testing"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestAlertingStateAccess(t *testing.T) {
Convey("Test alerting state changes", t, func() {
InitTestDB(t)
//setup alert
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
items := []m.AlertRule{
{
PanelId: 1,
DashboardId: testDash.Id,
OrgId: testDash.OrgId,
Query: "Query",
QueryRefId: "A",
WarnLevel: "> 30",
CritLevel: "> 50",
Interval: "10",
Title: "Alerting title",
Description: "Alerting description",
QueryRange: "5m",
Aggregator: "avg",
},
}
cmd := m.SaveAlertsCommand{
Alerts: &items,
DashboardId: testDash.Id,
OrgId: 1,
UserId: 1,
}
err := SaveAlerts(&cmd)
So(err, ShouldBeNil)
Convey("Cannot insert invalid states", func() {
err = SetNewAlertState(&m.UpdateAlertStateCommand{
AlertId: 1,
NewState: "maybe ok",
Info: "Shit just hit the fan",
})
So(err, ShouldNotBeNil)
})
Convey("Changes state to alert", func() {
err = SetNewAlertState(&m.UpdateAlertStateCommand{
AlertId: 1,
NewState: "ALERT",
Info: "Shit just hit the fan",
})
Convey("can get new state for alert", func() {
query := &m.GetAlertByIdQuery{Id: 1}
err := GetAlertById(query)
So(err, ShouldBeNil)
So(query.Result.State, ShouldEqual, "ALERT")
})
Convey("Changes state to ok", func() {
err = SetNewAlertState(&m.UpdateAlertStateCommand{
AlertId: 1,
NewState: "OK",
Info: "Shit just hit the fan",
})
Convey("get ok state for alert", func() {
query := &m.GetAlertByIdQuery{Id: 1}
err := GetAlertById(query)
So(err, ShouldBeNil)
So(query.Result.State, ShouldEqual, "OK")
})
})
})
})
}

View File

@@ -19,7 +19,7 @@ func addAlertMigrations(mg *Migrator) {
{Name: "description", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "query_range", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "aggregator", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "datasource_name", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "state", Type: DB_NVarchar, Length: 255, Nullable: false},
},
}