feat(alerting): add limit and since id options for alert logs

This commit is contained in:
bergquist
2016-05-04 09:57:53 +02:00
parent 7757d6d636
commit 26941284da
4 changed files with 63 additions and 4 deletions

View File

@@ -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
}

View File

@@ -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)
})
})
})
})
}