feat(alerting): adds endpoint for getting alert states log
This commit is contained in:
@@ -23,7 +23,7 @@ func GetAlertById(query *m.GetAlertByIdQuery) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("\n\n%v\n\n", query)
|
||||
|
||||
query.Result = alert
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ import (
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", SetNewAlertState)
|
||||
bus.AddHandler("sql", GetAlertStateLogByAlertId)
|
||||
}
|
||||
|
||||
func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
|
||||
@@ -29,11 +31,29 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
|
||||
|
||||
alert.State = cmd.NewState
|
||||
sess.Id(alert.Id).Update(&alert)
|
||||
//update alert
|
||||
|
||||
//insert alert state log
|
||||
log := m.AlertStateLog{
|
||||
AlertId: cmd.AlertId,
|
||||
OrgId: cmd.AlertId,
|
||||
NewState: cmd.NewState,
|
||||
Info: cmd.Info,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
sess.Insert(&log)
|
||||
|
||||
cmd.Result = &alert
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func GetAlertStateLogByAlertId(cmd *m.GetAlertsStateLogCommand) error {
|
||||
alertLogs := make([]m.AlertStateLog, 0)
|
||||
|
||||
if err := x.Where("alert_id = ?", cmd.AlertId).Find(&alertLogs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Result = &alertLogs
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ 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{
|
||||
@@ -79,6 +78,18 @@ func TestAlertingStateAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result.State, ShouldEqual, "OK")
|
||||
})
|
||||
|
||||
Convey("should have two event state logs", func() {
|
||||
query := &m.GetAlertsStateLogCommand{
|
||||
AlertId: 1,
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetAlertStateLogByAlertId(query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(*query.Result), ShouldEqual, 2)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -38,4 +38,18 @@ func addAlertMigrations(mg *Migrator) {
|
||||
}
|
||||
|
||||
mg.AddMigration("create alert_rules_updates table v1", NewAddTableMigration(alert_changes))
|
||||
|
||||
alert_state_log := Table{
|
||||
Name: "alert_state_log",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "alert_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "new_state", Type: DB_NVarchar, Length: 50, Nullable: false},
|
||||
{Name: "info", Type: DB_Text, Nullable: true},
|
||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||
},
|
||||
}
|
||||
|
||||
mg.AddMigration("create alert_state_log table v1", NewAddTableMigration(alert_state_log))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user