feat(alerting): add basic UI for history in alert tab

ref #5850
This commit is contained in:
bergquist
2016-08-30 13:22:59 +02:00
parent 650a87dc05
commit 11a4ff0f8a
7 changed files with 80 additions and 4 deletions

View File

@@ -215,8 +215,13 @@ func DeleteAlertNotification(c *middleware.Context) Response {
}
func GetAlertHistory(c *middleware.Context) Response {
alertId, err := getAlertIdForRequest(c)
if err != nil {
return ApiError(400, "Invalid request", err)
}
query := &annotations.ItemQuery{
AlertId: c.ParamsInt64("alertId"),
AlertId: alertId,
Type: annotations.AlertType,
OrgId: c.OrgId,
Limit: c.QueryInt64("limit"),
@@ -244,3 +249,34 @@ func GetAlertHistory(c *middleware.Context) Response {
return Json(200, result)
}
func getAlertIdForRequest(c *middleware.Context) (int64, error) {
alertId := c.QueryInt64("alertId")
panelId := c.QueryInt64("panelId")
dashboardId := c.QueryInt64("dashboardId")
if alertId == 0 && dashboardId == 0 && panelId == 0 {
return 0, fmt.Errorf("Missing alertId or dashboardId and panelId")
}
if alertId == 0 {
//fetch alertId
query := models.GetAlertsQuery{
OrgId: c.OrgId,
DashboardId: dashboardId,
PanelId: panelId,
}
if err := bus.Dispatch(&query); err != nil {
return 0, err
}
if len(query.Result) != 1 {
return 0, fmt.Errorf("PanelId is not unique on dashboard")
}
alertId = query.Result[0].Id
}
return alertId, nil
}

View File

@@ -254,7 +254,7 @@ func Register(r *macaron.Macaron) {
r.Get("/", wrap(GetAlerts))
})
r.Get("/alert-history/:alertId", ValidateOrgAlert, wrap(GetAlertHistory))
r.Get("/alert-history", wrap(GetAlertHistory))
r.Get("/alert-notifications", wrap(GetAlertNotifications))

View File

@@ -55,7 +55,7 @@ type EvalMatch struct {
type AlertHistory struct {
AlertId int64 `json:"alertId"`
NewState string `json:"netState"`
NewState string `json:"newState"`
Timestamp time.Time `json:"timestamp"`
Title string `json:"title"`
Text string `json:"text"`

View File

@@ -4,6 +4,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
m "github.com/grafana/grafana/pkg/models"
@@ -65,6 +66,7 @@ func (handler *DefaultResultHandler) Handle(ctx *EvalContext) {
NewState: string(ctx.Rule.State),
PrevState: string(oldState),
Timestamp: time.Now(),
Data: simplejson.NewFromAny(ctx.EvalMatches),
}
annotationRepo := annotations.GetRepository()