mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add api endpoints for listing alerts
This commit is contained in:
parent
996eec3ce2
commit
9b50313f11
47
pkg/api/alerting.go
Normal file
47
pkg/api/alerting.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
|
"github.com/grafana/grafana/pkg/middleware"
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ValidateOrgAlert(c *middleware.Context) {
|
||||||
|
id := c.ParamsInt64(":id")
|
||||||
|
query := models.GetAlertById{Id: id}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
c.JsonApiErr(404, "Alert not found", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.OrgId != query.Result.OrgId {
|
||||||
|
c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /api/alert_rule
|
||||||
|
func GetAlerts(c *middleware.Context) Response {
|
||||||
|
query := models.GetAlertsQuery{
|
||||||
|
OrgId: c.OrgId,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
return ApiError(500, "List alerts failed", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Json(200, query.Result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /api/alert_rule/:id
|
||||||
|
func GetAlert(c *middleware.Context) Response {
|
||||||
|
id := c.ParamsInt64(":id")
|
||||||
|
query := models.GetAlertById{Id: id}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
return ApiError(500, "List alerts failed", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Json(200, &query.Result)
|
||||||
|
}
|
@ -236,6 +236,11 @@ func Register(r *macaron.Macaron) {
|
|||||||
// metrics
|
// metrics
|
||||||
r.Get("/metrics/test", GetTestMetrics)
|
r.Get("/metrics/test", GetTestMetrics)
|
||||||
|
|
||||||
|
r.Group("/alert_rule", func() {
|
||||||
|
r.Get("/", wrap(GetAlerts))
|
||||||
|
r.Get("/:id", ValidateOrgAlert, wrap(GetAlert))
|
||||||
|
})
|
||||||
|
|
||||||
}, reqSignedIn)
|
}, reqSignedIn)
|
||||||
|
|
||||||
// admin api
|
// admin api
|
||||||
|
@ -82,3 +82,16 @@ type SaveAlertsCommand struct {
|
|||||||
|
|
||||||
Alerts *[]AlertRule
|
Alerts *[]AlertRule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Queries
|
||||||
|
type GetAlertsQuery struct {
|
||||||
|
OrgId int64
|
||||||
|
|
||||||
|
Result []AlertRule
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAlertById struct {
|
||||||
|
Id int64
|
||||||
|
|
||||||
|
Result AlertRule
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package sqlstore
|
package sqlstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
@ -9,6 +10,33 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
bus.AddHandler("sql", SaveAlerts)
|
bus.AddHandler("sql", SaveAlerts)
|
||||||
|
bus.AddHandler("sql", GetAllAlertsForOrg)
|
||||||
|
bus.AddHandler("sql", GetAlertById)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAlertById(query *m.GetAlertById) error {
|
||||||
|
alert := m.AlertRule{}
|
||||||
|
has, err := x.Id(query.Id).Get(&alert)
|
||||||
|
|
||||||
|
if !has {
|
||||||
|
return fmt.Errorf("could not find alert")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n%v\n\n", query)
|
||||||
|
query.Result = alert
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllAlertsForOrg(query *m.GetAlertsQuery) error {
|
||||||
|
alerts := make([]m.AlertRule, 0)
|
||||||
|
if err := x.Where("org_id = ?", query.OrgId).Find(&alerts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Result = alerts
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
|
func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user