Commit 624cd6fc by Torkel Ödegaard

feat(alerting): cleanup, removed alert changes table and code

parent d9096110
...@@ -9,4 +9,3 @@ collectd: ...@@ -9,4 +9,3 @@ collectd:
COLLECT_INTERVAL: 10 COLLECT_INTERVAL: 10
links: links:
- graphite - graphite
# - memcached
...@@ -22,27 +22,6 @@ func ValidateOrgAlert(c *middleware.Context) { ...@@ -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/ // GET /api/alerts/rules/
func GetAlerts(c *middleware.Context) Response { func GetAlerts(c *middleware.Context) Response {
query := models.GetAlertsQuery{ query := models.GetAlertsQuery{
......
...@@ -245,7 +245,7 @@ func Register(r *macaron.Macaron) { ...@@ -245,7 +245,7 @@ func Register(r *macaron.Macaron) {
// metrics // metrics
r.Get("/metrics", wrap(GetInternalMetrics)) r.Get("/metrics", wrap(GetInternalMetrics))
r.Group("/alerts", func() { r.Group("/alerting", func() {
r.Group("/rules", func() { r.Group("/rules", func() {
r.Get("/:alertId/states", wrap(GetAlertStates)) r.Get("/:alertId/states", wrap(GetAlertStates))
//r.Put("/:alertId/state", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState)) //r.Put("/:alertId/state", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState))
...@@ -262,8 +262,6 @@ func Register(r *macaron.Macaron) { ...@@ -262,8 +262,6 @@ func Register(r *macaron.Macaron) {
r.Get("/:notificationId", wrap(GetAlertNotificationById)) r.Get("/:notificationId", wrap(GetAlertNotificationById))
r.Delete("/:notificationId", wrap(DeleteAlertNotification)) r.Delete("/:notificationId", wrap(DeleteAlertNotification))
}, reqOrgAdmin) }, reqOrgAdmin)
//r.Get("/changes", wrap(GetAlertChanges))
}) })
// error test // error test
......
...@@ -151,17 +151,6 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error { ...@@ -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) 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 return nil
...@@ -205,13 +194,6 @@ func upsertAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Sessio ...@@ -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) 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 { } else {
...@@ -224,13 +206,6 @@ func upsertAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Sessio ...@@ -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) 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 ...@@ -255,17 +230,6 @@ func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm
} }
sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id) 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
}
} }
} }
......
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
}
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) { ...@@ -14,7 +14,7 @@ func addAlertMigrations(mg *Migrator) {
{Name: "panel_id", Type: DB_BigInt, Nullable: false}, {Name: "panel_id", Type: DB_BigInt, Nullable: false},
{Name: "org_id", Type: DB_BigInt, Nullable: false}, {Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "name", Type: DB_NVarchar, Length: 255, 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: "state", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "settings", Type: DB_Text, Nullable: false}, {Name: "settings", Type: DB_Text, Nullable: false},
{Name: "frequency", Type: DB_BigInt, Nullable: false}, {Name: "frequency", Type: DB_BigInt, Nullable: false},
...@@ -22,34 +22,21 @@ func addAlertMigrations(mg *Migrator) { ...@@ -22,34 +22,21 @@ func addAlertMigrations(mg *Migrator) {
{Name: "enabled", Type: DB_Bool, Nullable: false}, {Name: "enabled", Type: DB_Bool, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false}, {Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", 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 // create table
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1)) 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{ alert_state_log := Table{
Name: "alert_state", Name: "alert_state",
Columns: []*Column{ Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "alert_id", Type: DB_BigInt, Nullable: false}, {Name: "alert_id", Type: DB_BigInt, Nullable: false},
{Name: "org_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: "info", Type: DB_Text, Nullable: true},
{Name: "triggered_alerts", Type: DB_Text, Nullable: true}, {Name: "triggered_alerts", Type: DB_Text, Nullable: true},
{Name: "created", Type: DB_DateTime, Nullable: false}, {Name: "created", Type: DB_DateTime, Nullable: false},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment