From 650a87dc05b4a69da8b42b7c79d820e206e40197 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 30 Aug 2016 09:32:56 +0200 Subject: [PATCH] feat(alerting): add alert history api endpoint ref #5850 --- pkg/api/alerting.go | 32 +++++++++++++++++++++ pkg/api/api.go | 3 +- pkg/api/dtos/alerting.go | 11 ++++++++ pkg/services/annotations/annotations.go | 9 ++++++ pkg/services/sqlstore/annotation.go | 37 +++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index f20e08a5cad..63a080a74d2 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -8,6 +8,7 @@ import ( "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" + "github.com/grafana/grafana/pkg/services/annotations" ) func ValidateOrgAlert(c *middleware.Context) { @@ -212,3 +213,34 @@ func DeleteAlertNotification(c *middleware.Context) Response { return ApiSuccess("Notification deleted") } + +func GetAlertHistory(c *middleware.Context) Response { + query := &annotations.ItemQuery{ + AlertId: c.ParamsInt64("alertId"), + Type: annotations.AlertType, + OrgId: c.OrgId, + Limit: c.QueryInt64("limit"), + } + + repo := annotations.GetRepository() + + items, err := repo.Find(query) + if err != nil { + return ApiError(500, "Failed to get history for alert", err) + } + + var result []dtos.AlertHistory + for _, item := range items { + result = append(result, dtos.AlertHistory{ + AlertId: item.AlertId, + Timestamp: item.Timestamp, + Data: item.Data, + NewState: item.NewState, + Text: item.Text, + Metric: item.Metric, + Title: item.Title, + }) + } + + return Json(200, result) +} diff --git a/pkg/api/api.go b/pkg/api/api.go index 25158586822..966f950ee53 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -250,11 +250,12 @@ 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", ValidateOrgAlert, wrap(GetAlert)) r.Get("/", wrap(GetAlerts)) }) + r.Get("/alert-history/:alertId", ValidateOrgAlert, wrap(GetAlertHistory)) + r.Get("/alert-notifications", wrap(GetAlertNotifications)) r.Group("/alert-notifications", func() { diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go index 0ec0174a949..c6cafddead0 100644 --- a/pkg/api/dtos/alerting.go +++ b/pkg/api/dtos/alerting.go @@ -52,3 +52,14 @@ type EvalMatch struct { Metric string `json:"metric"` Value float64 `json:"value"` } + +type AlertHistory struct { + AlertId int64 `json:"alertId"` + NewState string `json:"netState"` + Timestamp time.Time `json:"timestamp"` + Title string `json:"title"` + Text string `json:"text"` + Metric string `json:"metric"` + + Data *simplejson.Json `json:"data"` +} diff --git a/pkg/services/annotations/annotations.go b/pkg/services/annotations/annotations.go index fd8c8ebdb9a..06be152ec31 100644 --- a/pkg/services/annotations/annotations.go +++ b/pkg/services/annotations/annotations.go @@ -8,6 +8,15 @@ import ( type Repository interface { Save(item *Item) error + Find(query *ItemQuery) ([]*Item, error) +} + +type ItemQuery struct { + OrgId int64 `json:"orgId"` + Type ItemType `json:"type"` + AlertId int64 `json:"alertId"` + + Limit int64 `json:"alertId"` } var repositoryInstance Repository diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index 0530952144e..5cf4461b370 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -1,6 +1,9 @@ package sqlstore import ( + "bytes" + "fmt" + "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/services/annotations" ) @@ -17,5 +20,39 @@ func (r *SqlAnnotationRepo) Save(item *annotations.Item) error { return nil }) +} +func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.Item, error) { + var sql bytes.Buffer + params := make([]interface{}, 0) + + sql.WriteString(`SELECT * + from annotation + `) + + sql.WriteString(`WHERE org_id = ?`) + params = append(params, query.OrgId) + + if query.AlertId != 0 { + sql.WriteString(` AND alert_id = ?`) + params = append(params, query.AlertId) + } + + if query.Type != "" { + sql.WriteString(` AND type = ?`) + params = append(params, string(query.Type)) + } + + if query.Limit == 0 { + query.Limit = 10 + } + + sql.WriteString(fmt.Sprintf("ORDER BY timestamp DESC LIMIT %v", query.Limit)) + + items := make([]*annotations.Item, 0) + if err := x.Sql(sql.String(), params...).Find(&items); err != nil { + return nil, err + } + + return items, nil }