feat(alerting): save execution error upon state changed

This commit is contained in:
bergquist
2016-08-17 09:27:29 +02:00
parent 9915f15ed0
commit 6497b307c4
5 changed files with 11 additions and 63 deletions

View File

@@ -18,24 +18,6 @@ func init() {
bus.AddHandler("sql", DeleteAlertById)
bus.AddHandler("sql", GetAllAlertQueryHandler)
bus.AddHandler("sql", SetAlertState)
bus.AddHandler("sql", SaveExecutionErrorForAlert)
}
func SaveExecutionErrorForAlert(cmd *m.SaveExecutionErrorCommand) error {
return inTransaction(func(sess *xorm.Session) error {
alert := m.Alert{}
if has, err := sess.Id(cmd.AlertId).Get(&alert); err != nil {
return err
} else if !has {
return fmt.Errorf("Could not find alert")
}
alert.ExecutionError = cmd.ExecutionError
sess.Id(alert.Id).Update(&alert)
return nil
})
}
func GetAlertById(query *m.GetAlertByIdQuery) error {
@@ -241,6 +223,7 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error {
alert.State = cmd.State
alert.StateChanges += 1
alert.NewStateDate = time.Now()
alert.ExecutionError = cmd.Error
sess.Id(alert.Id).Update(&alert)
return nil

View File

@@ -175,39 +175,5 @@ func TestAlertingDataAccess(t *testing.T) {
So(len(query.Result), ShouldEqual, 0)
})
})
Convey("Can set new execution error", func() {
items := []*m.Alert{
{
PanelId: 1,
DashboardId: testDash.Id,
Name: "Alerting title",
Message: "Alerting message",
},
}
cmd := m.SaveAlertsCommand{
Alerts: items,
DashboardId: testDash.Id,
OrgId: 1,
UserId: 1,
}
SaveAlerts(&cmd)
So(SaveExecutionErrorForAlert(&m.SaveExecutionErrorCommand{
AlertId: 1,
ExecutionError: "the slacker is broken",
}), ShouldBeNil)
Convey("Alerts should be removed", func() {
query := &m.GetAlertByIdQuery{Id: 1}
err2 := GetAlertById(query)
So(testDash.Id, ShouldEqual, 1)
So(err2, ShouldBeNil)
So(query.Result.ExecutionError, ShouldEqual, "the slacker is broken")
})
})
})
}

View File

@@ -21,7 +21,6 @@ func addAlertMigrations(mg *Migrator) {
{Name: "frequency", Type: DB_BigInt, Nullable: false},
{Name: "handler", Type: DB_BigInt, Nullable: false},
{Name: "severity", Type: DB_Text, Nullable: false},
{Name: "paused", Type: DB_Bool, Nullable: false},
{Name: "silenced", Type: DB_Bool, Nullable: false},
{Name: "execution_error", Type: DB_Text, Nullable: false},
{Name: "eval_data", Type: DB_Text, Nullable: true},