mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): adds endpoint for getting alert states log
This commit is contained in:
parent
3ecc13506c
commit
0f0fa0c257
@ -98,6 +98,21 @@ func GetAlert(c *middleware.Context) Response {
|
||||
return Json(200, &query.Result)
|
||||
}
|
||||
|
||||
// GET /api/alerts/state/:id
|
||||
func GetAlertState(c *middleware.Context) Response {
|
||||
alertId := c.ParamsInt64(":alertId")
|
||||
|
||||
query := models.GetAlertsStateLogCommand{
|
||||
AlertId: alertId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed get alert state log", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// PUT /api/alerts/state/:id
|
||||
func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
|
||||
alertId := c.ParamsInt64(":alertId")
|
||||
|
@ -239,6 +239,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/metrics/test", GetTestMetrics)
|
||||
|
||||
r.Group("/alerts", func() {
|
||||
r.Get("/state/:alertId", wrap(GetAlertState))
|
||||
r.Put("/state/:alertId", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState))
|
||||
r.Get("/changes", wrap(GetAlertChanges))
|
||||
r.Get("/", wrap(GetAlerts))
|
||||
|
@ -1,15 +1,16 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type AlertStateLog struct {
|
||||
Id int64 `json:"id"`
|
||||
OrgId int64 `json:"-"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
State string `json:"type"`
|
||||
Created time.Time `json:"created"`
|
||||
Acknowledged time.Time `json:"acknowledged"`
|
||||
Deleted time.Time `json:"deleted"`
|
||||
Id int64 `json:"-"`
|
||||
OrgId int64 `json:"-"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
NewState string `json:"newState"`
|
||||
Created time.Time `json:"created"`
|
||||
Info string `json:"info"`
|
||||
}
|
||||
|
||||
var (
|
||||
@ -23,6 +24,8 @@ func (this *UpdateAlertStateCommand) IsValidState() bool {
|
||||
return this.NewState == ALERT_STATE_OK || this.NewState == ALERT_STATE_WARN || this.NewState == ALERT_STATE_ALERT || this.NewState == ALERT_STATE_ACKNOWLEDGED
|
||||
}
|
||||
|
||||
// Commands
|
||||
|
||||
type UpdateAlertStateCommand struct {
|
||||
AlertId int64 `json:"alertId" binding:"Required"`
|
||||
NewState string `json:"newState" binding:"Required"`
|
||||
@ -30,3 +33,12 @@ type UpdateAlertStateCommand struct {
|
||||
|
||||
Result *AlertRule
|
||||
}
|
||||
|
||||
// Queries
|
||||
|
||||
type GetAlertsStateLogCommand struct {
|
||||
OrgId int64 `json:"orgId" binding:"Required"`
|
||||
AlertId int64 `json:"alertId" binding:"Required"`
|
||||
|
||||
Result *[]AlertStateLog
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user