feat(alerting): working on state management

This commit is contained in:
Torkel Ödegaard
2016-07-22 13:14:09 +02:00
parent 783d697529
commit 7eb2d2cf47
23 changed files with 380 additions and 450 deletions
+37 -36
View File
@@ -49,6 +49,7 @@ func GetAlerts(c *middleware.Context) Response {
Name: alert.Name,
Description: alert.Description,
State: alert.State,
Severity: alert.Severity,
})
}
@@ -92,7 +93,7 @@ func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
res := backendCmd.Result
dtoRes := &dtos.AlertTestResult{
Triggered: res.Triggered,
Firing: res.Firing,
}
if res.Error != nil {
@@ -138,41 +139,41 @@ func DelAlert(c *middleware.Context) Response {
return Json(200, resp)
}
// GET /api/alerts/events/:id
func GetAlertStates(c *middleware.Context) Response {
alertId := c.ParamsInt64(":alertId")
query := models.GetAlertsStateQuery{
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/events/:id
func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
cmd.AlertId = c.ParamsInt64(":alertId")
cmd.OrgId = c.OrgId
query := models.GetAlertByIdQuery{Id: cmd.AlertId}
if err := bus.Dispatch(&query); err != nil {
return ApiError(500, "Failed to get alertstate", err)
}
if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
return ApiError(500, "Alert not found", nil)
}
if err := bus.Dispatch(&cmd); err != nil {
return ApiError(500, "Failed to set new state", err)
}
return Json(200, cmd.Result)
}
// // GET /api/alerts/events/:id
// func GetAlertStates(c *middleware.Context) Response {
// alertId := c.ParamsInt64(":alertId")
//
// query := models.GetAlertsStateQuery{
// 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/events/:id
// func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
// cmd.AlertId = c.ParamsInt64(":alertId")
// cmd.OrgId = c.OrgId
//
// query := models.GetAlertByIdQuery{Id: cmd.AlertId}
// if err := bus.Dispatch(&query); err != nil {
// return ApiError(500, "Failed to get alertstate", err)
// }
//
// if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
// return ApiError(500, "Alert not found", nil)
// }
//
// if err := bus.Dispatch(&cmd); err != nil {
// return ApiError(500, "Failed to set new state", err)
// }
//
// return Json(200, cmd.Result)
// }
func GetAlertNotifications(c *middleware.Context) Response {
query := &models.GetAlertNotificationQuery{
+1 -1
View File
@@ -247,7 +247,7 @@ func Register(r *macaron.Macaron) {
r.Group("/alerts", func() {
r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest))
r.Get("/:alertId/states", wrap(GetAlertStates))
//r.Get("/:alertId/states", wrap(GetAlertStates))
//r.Put("/:alertId/state", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState))
r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
//r.Delete("/:alertId", ValidateOrgAlert, wrap(DelAlert)) disabled until we know how to handle it dashboard updates
+12 -19
View File
@@ -4,24 +4,17 @@ import (
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
)
type AlertRuleDTO struct {
Id int64 `json:"id"`
DashboardId int64 `json:"dashboardId"`
PanelId int64 `json:"panelId"`
Query string `json:"query"`
QueryRefId string `json:"queryRefId"`
WarnLevel float64 `json:"warnLevel"`
CritLevel float64 `json:"critLevel"`
WarnOperator string `json:"warnOperator"`
CritOperator string `json:"critOperator"`
Frequency int64 `json:"frequency"`
Name string `json:"name"`
Description string `json:"description"`
QueryRange int `json:"queryRange"`
Aggregator string `json:"aggregator"`
State string `json:"state"`
Id int64 `json:"id"`
DashboardId int64 `json:"dashboardId"`
PanelId int64 `json:"panelId"`
Name string `json:"name"`
Description string `json:"description"`
State m.AlertStateType `json:"state"`
Severity m.AlertSeverityType `json:"severity"`
DashbboardUri string `json:"dashboardUri"`
}
@@ -40,10 +33,10 @@ type AlertTestCommand struct {
}
type AlertTestResult struct {
Triggered bool `json:"triggerd"`
Timing string `json:"timing"`
Error string `json:"error,omitempty"`
Logs []*AlertTestResultLog `json:"logs,omitempty"`
Firing bool `json:"firing"`
Timing string `json:"timing"`
Error string `json:"error,omitempty"`
Logs []*AlertTestResultLog `json:"logs,omitempty"`
}
type AlertTestResultLog struct {