mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add limit and since id options for alert logs
This commit is contained in:
parent
7757d6d636
commit
26941284da
@ -28,6 +28,14 @@ func GetAlertChanges(c *middleware.Context) Response {
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
limit := c.QueryInt64("limit")
|
||||
if limit == 0 {
|
||||
limit = 10
|
||||
}
|
||||
|
||||
query.Limit = limit
|
||||
query.SinceId = c.QueryInt64("sinceId")
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "List alerts failed", err)
|
||||
}
|
||||
@ -116,7 +124,7 @@ func DelAlert(c *middleware.Context) Response {
|
||||
return Json(200, resp)
|
||||
}
|
||||
|
||||
// GET /api/alerts/state/:id
|
||||
// GET /api/alerts/events/:id
|
||||
func GetAlertState(c *middleware.Context) Response {
|
||||
alertId := c.ParamsInt64(":alertId")
|
||||
|
||||
@ -131,7 +139,7 @@ func GetAlertState(c *middleware.Context) Response {
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// PUT /api/alerts/state/:id
|
||||
// PUT /api/alerts/events/:id
|
||||
func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
|
||||
alertId := c.ParamsInt64(":alertId")
|
||||
|
||||
|
@ -25,6 +25,7 @@ type AlertRule struct {
|
||||
}
|
||||
|
||||
type AlertRuleChange struct {
|
||||
Id int64 `json:"id"`
|
||||
OrgId int64 `json:"-"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
Type string `json:"type"`
|
||||
@ -106,7 +107,9 @@ type GetAlertByIdQuery struct {
|
||||
}
|
||||
|
||||
type GetAlertChangesQuery struct {
|
||||
OrgId int64
|
||||
OrgId int64
|
||||
Limit int64
|
||||
SinceId int64
|
||||
|
||||
Result []AlertRuleChange
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
@ -12,8 +13,33 @@ func init() {
|
||||
}
|
||||
|
||||
func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
|
||||
var sql bytes.Buffer
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
sql.WriteString(`SELECT
|
||||
alert_rule_change.id,
|
||||
alert_rule_change.org_id,
|
||||
alert_rule_change.alert_id,
|
||||
alert_rule_change.type,
|
||||
alert_rule_change.created
|
||||
FROM alert_rule_change
|
||||
`)
|
||||
|
||||
sql.WriteString(`WHERE alert_rule_change.org_id = ?`)
|
||||
params = append(params, query.OrgId)
|
||||
|
||||
if query.SinceId != 0 {
|
||||
sql.WriteString(`AND alert_rule_change.id >= ?`)
|
||||
params = append(params, query.SinceId)
|
||||
}
|
||||
|
||||
if query.Limit != 0 {
|
||||
sql.WriteString(` ORDER BY alert_rule_change.id DESC LIMIT ?`)
|
||||
params = append(params, query.Limit)
|
||||
}
|
||||
|
||||
alertChanges := make([]m.AlertRuleChange, 0)
|
||||
if err := x.Where("org_id = ?", query.OrgId).Find(&alertChanges); err != nil {
|
||||
if err := x.Sql(sql.String(), params...).Find(&alertChanges); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -75,6 +75,28 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
So(er, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
|
||||
Convey("add 4 updates", func() {
|
||||
sess := x.NewSession()
|
||||
SaveAlertChange("UPDATED", items[0], sess)
|
||||
SaveAlertChange("UPDATED", items[0], sess)
|
||||
SaveAlertChange("UPDATED", items[0], sess)
|
||||
SaveAlertChange("UPDATED", items[0], sess)
|
||||
|
||||
Convey("query for max one change", func() {
|
||||
query := &m.GetAlertChangesQuery{OrgId: FakeOrgId, Limit: 1}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("query for all since id 5", func() {
|
||||
query := &m.GetAlertChangesQuery{OrgId: FakeOrgId, SinceId: 5}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user