mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): cleanup, removed alert changes table and code
This commit is contained in:
parent
d9096110f8
commit
624cd6fc0a
@ -9,4 +9,3 @@ collectd:
|
||||
COLLECT_INTERVAL: 10
|
||||
links:
|
||||
- graphite
|
||||
# - memcached
|
||||
|
@ -22,27 +22,6 @@ func ValidateOrgAlert(c *middleware.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/alerting/changes
|
||||
func GetAlertChanges(c *middleware.Context) Response {
|
||||
query := models.GetAlertChangesQuery{
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
limit := c.QueryInt64("limit")
|
||||
if limit == 0 {
|
||||
limit = 50
|
||||
}
|
||||
|
||||
query.Limit = limit
|
||||
query.SinceId = c.QueryInt64("sinceId")
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "List alerts failed", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// GET /api/alerts/rules/
|
||||
func GetAlerts(c *middleware.Context) Response {
|
||||
query := models.GetAlertsQuery{
|
||||
|
@ -245,7 +245,7 @@ func Register(r *macaron.Macaron) {
|
||||
// metrics
|
||||
r.Get("/metrics", wrap(GetInternalMetrics))
|
||||
|
||||
r.Group("/alerts", func() {
|
||||
r.Group("/alerting", func() {
|
||||
r.Group("/rules", func() {
|
||||
r.Get("/:alertId/states", wrap(GetAlertStates))
|
||||
//r.Put("/:alertId/state", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState))
|
||||
@ -262,8 +262,6 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/:notificationId", wrap(GetAlertNotificationById))
|
||||
r.Delete("/:notificationId", wrap(DeleteAlertNotification))
|
||||
}, reqOrgAdmin)
|
||||
|
||||
//r.Get("/changes", wrap(GetAlertChanges))
|
||||
})
|
||||
|
||||
// error test
|
||||
|
@ -151,17 +151,6 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
|
||||
}
|
||||
|
||||
sqlog.Debug("Alert deleted (due to dashboard deletion)", "name", alert.Name, "id", alert.Id)
|
||||
|
||||
cmd := &m.CreateAlertChangeCommand{
|
||||
Type: "DELETED",
|
||||
UpdatedBy: 1,
|
||||
AlertId: alert.Id,
|
||||
OrgId: alert.OrgId,
|
||||
NewAlertSettings: alert.Settings,
|
||||
}
|
||||
if err := SaveAlertChange(cmd, sess); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -205,13 +194,6 @@ func upsertAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Sessio
|
||||
}
|
||||
|
||||
sqlog.Debug("Alert updated", "name", alert.Name, "id", alert.Id)
|
||||
SaveAlertChange(&m.CreateAlertChangeCommand{
|
||||
OrgId: alert.OrgId,
|
||||
AlertId: alert.Id,
|
||||
NewAlertSettings: alert.Settings,
|
||||
UpdatedBy: cmd.UserId,
|
||||
Type: "UPDATED",
|
||||
}, sess)
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -224,13 +206,6 @@ func upsertAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Sessio
|
||||
}
|
||||
|
||||
sqlog.Debug("Alert inserted", "name", alert.Name, "id", alert.Id)
|
||||
SaveAlertChange(&m.CreateAlertChangeCommand{
|
||||
OrgId: alert.OrgId,
|
||||
AlertId: alert.Id,
|
||||
NewAlertSettings: alert.Settings,
|
||||
UpdatedBy: cmd.UserId,
|
||||
Type: "CREATED",
|
||||
}, sess)
|
||||
}
|
||||
}
|
||||
|
||||
@ -255,17 +230,6 @@ func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm
|
||||
}
|
||||
|
||||
sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id)
|
||||
|
||||
SaveAlertChange(&m.CreateAlertChangeCommand{
|
||||
OrgId: missingAlert.OrgId,
|
||||
AlertId: missingAlert.Id,
|
||||
NewAlertSettings: missingAlert.Settings,
|
||||
UpdatedBy: cmd.UserId,
|
||||
Type: "DELETED",
|
||||
}, sess)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,68 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetAlertRuleChanges)
|
||||
}
|
||||
|
||||
func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
|
||||
var sql bytes.Buffer
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
sql.WriteString(`SELECT
|
||||
alert_change.id,
|
||||
alert_change.org_id,
|
||||
alert_change.alert_id,
|
||||
alert_change.type,
|
||||
alert_change.created,
|
||||
alert_change.new_alert_settings,
|
||||
alert_change.updated_by
|
||||
FROM alert_change
|
||||
`)
|
||||
|
||||
sql.WriteString(`WHERE alert_change.org_id = ?`)
|
||||
params = append(params, query.OrgId)
|
||||
|
||||
if query.SinceId != 0 {
|
||||
sql.WriteString(`AND alert_change.id >= ?`)
|
||||
params = append(params, query.SinceId)
|
||||
}
|
||||
|
||||
if query.Limit != 0 {
|
||||
sql.WriteString(` ORDER BY alert_change.id DESC LIMIT ?`)
|
||||
params = append(params, query.Limit)
|
||||
}
|
||||
|
||||
alertChanges := make([]*m.AlertChange, 0)
|
||||
if err := x.Sql(sql.String(), params...).Find(&alertChanges); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
query.Result = alertChanges
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveAlertChange(cmd *m.CreateAlertChangeCommand, sess *xorm.Session) error {
|
||||
_, err := sess.Insert(&m.AlertChange{
|
||||
OrgId: cmd.OrgId,
|
||||
Type: cmd.Type,
|
||||
Created: time.Now(),
|
||||
AlertId: cmd.AlertId,
|
||||
NewAlertSettings: cmd.NewAlertSettings,
|
||||
UpdatedBy: cmd.UpdatedBy,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
var (
|
||||
FakeOrgId int64 = 2
|
||||
)
|
||||
|
||||
func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
|
||||
Convey("Testing Alert rule changes data access", t, func() {
|
||||
InitTestDB(t)
|
||||
|
||||
testDash := insertTestDashboard("dashboard with alerts", 2, "alert")
|
||||
var err error
|
||||
|
||||
Convey("When dashboard is removed", func() {
|
||||
items := []*m.Alert{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
OrgId: FakeOrgId,
|
||||
},
|
||||
}
|
||||
|
||||
cmd := m.SaveAlertsCommand{
|
||||
Alerts: items,
|
||||
DashboardId: testDash.Id,
|
||||
OrgId: FakeOrgId,
|
||||
UserId: 2,
|
||||
}
|
||||
|
||||
SaveAlerts(&cmd)
|
||||
|
||||
query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
|
||||
err = DeleteDashboard(&m.DeleteDashboardCommand{
|
||||
OrgId: FakeOrgId,
|
||||
Slug: testDash.Slug,
|
||||
})
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Alerts should be removed", func() {
|
||||
query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
|
||||
err2 := HandleAlertsQuery(&query)
|
||||
|
||||
So(testDash.Id, ShouldEqual, 1)
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("should add one more alert_rule_change", func() {
|
||||
query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
|
||||
Convey("add 4 updates", func() {
|
||||
sess := x.NewSession()
|
||||
updateCmd := m.CreateAlertChangeCommand{
|
||||
AlertId: items[0].Id,
|
||||
OrgId: items[0].OrgId,
|
||||
UpdatedBy: 1,
|
||||
}
|
||||
|
||||
SaveAlertChange(&updateCmd, sess)
|
||||
SaveAlertChange(&updateCmd, sess)
|
||||
SaveAlertChange(&updateCmd, sess)
|
||||
SaveAlertChange(&updateCmd, sess)
|
||||
sess.Commit()
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
@ -14,7 +14,7 @@ func addAlertMigrations(mg *Migrator) {
|
||||
{Name: "panel_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "description", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "description", Type: DB_Text, Nullable: false},
|
||||
{Name: "state", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "settings", Type: DB_Text, Nullable: false},
|
||||
{Name: "frequency", Type: DB_BigInt, Nullable: false},
|
||||
@ -22,34 +22,21 @@ func addAlertMigrations(mg *Migrator) {
|
||||
{Name: "enabled", Type: DB_Bool, Nullable: false},
|
||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||
{Name: "updated_by", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "created_by", Type: DB_BigInt, Nullable: false},
|
||||
},
|
||||
}
|
||||
|
||||
// create table
|
||||
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
|
||||
|
||||
alert_changes := Table{
|
||||
Name: "alert_change",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "alert_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "type", Type: DB_NVarchar, Length: 50, Nullable: false},
|
||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||
{Name: "updated_by", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "new_alert_settings", Type: DB_Text, Nullable: false},
|
||||
},
|
||||
}
|
||||
|
||||
mg.AddMigration("create alert_change table v1", NewAddTableMigration(alert_changes))
|
||||
|
||||
alert_state_log := Table{
|
||||
Name: "alert_state",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "alert_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "new_state", Type: DB_NVarchar, Length: 50, Nullable: false},
|
||||
{Name: "state", Type: DB_NVarchar, Length: 50, Nullable: false},
|
||||
{Name: "info", Type: DB_Text, Nullable: true},
|
||||
{Name: "triggered_alerts", Type: DB_Text, Nullable: true},
|
||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||
|
Loading…
Reference in New Issue
Block a user