Commit 4fc16c36 by bergquist

feat(alerting): saves new state when alert updates

parent 5a65dc5d
...@@ -71,14 +71,24 @@ type HeartBeatCommand struct { ...@@ -71,14 +71,24 @@ type HeartBeatCommand struct {
} }
type AlertChange struct { type AlertChange struct {
Id int64 `json:"id"` Id int64 `json:"id"`
OrgId int64 `json:"-"` OrgId int64 `json:"-"`
AlertId int64 `json:"alertId"` AlertId int64 `json:"alertId"`
Type string `json:"type"` UpdatedBy int64 `json:"updatedBy"`
Created time.Time `json:"created"` NewAlertSettings *simplejson.Json `json:"newAlertSettings"`
Type string `json:"type"`
Created time.Time `json:"created"`
} }
// Commands // Commands
type CreateAlertChangeCommand struct {
OrgId int64
AlertId int64
UpdatedBy int64
NewAlertSettings *simplejson.Json
Type string
}
type SaveAlertsCommand struct { type SaveAlertsCommand struct {
DashboardId int64 DashboardId int64
UserId int64 UserId int64
......
...@@ -152,7 +152,14 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error { ...@@ -152,7 +152,14 @@ 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)
if err := SaveAlertChange("DELETED", alert, sess); err != nil { 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 err
} }
} }
...@@ -167,15 +174,15 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error { ...@@ -167,15 +174,15 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
return err return err
} }
upsertAlerts(alerts, cmd.Alerts, sess) upsertAlerts(alerts, cmd, sess)
deleteMissingAlerts(alerts, cmd.Alerts, sess) deleteMissingAlerts(alerts, cmd, sess)
return nil return nil
}) })
} }
func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) error { func upsertAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error {
for _, alert := range posted { for _, alert := range cmd.Alerts {
update := false update := false
var alertToUpdate *m.Alert var alertToUpdate *m.Alert
...@@ -198,7 +205,13 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro ...@@ -198,7 +205,13 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro
} }
sqlog.Debug("Alert updated", "name", alert.Name, "id", alert.Id) sqlog.Debug("Alert updated", "name", alert.Name, "id", alert.Id)
SaveAlertChange("UPDATED", alert, sess) SaveAlertChange(&m.CreateAlertChangeCommand{
OrgId: alert.OrgId,
AlertId: alert.Id,
NewAlertSettings: alert.Settings,
UpdatedBy: cmd.UserId,
Type: "UPDATED",
}, sess)
} }
} else { } else {
...@@ -211,18 +224,24 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro ...@@ -211,18 +224,24 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro
} }
sqlog.Debug("Alert inserted", "name", alert.Name, "id", alert.Id) sqlog.Debug("Alert inserted", "name", alert.Name, "id", alert.Id)
SaveAlertChange("CREATED", alert, sess) SaveAlertChange(&m.CreateAlertChangeCommand{
OrgId: alert.OrgId,
AlertId: alert.Id,
NewAlertSettings: alert.Settings,
UpdatedBy: cmd.UserId,
Type: "CREATED",
}, sess)
} }
} }
return nil return nil
} }
func deleteMissingAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) error { func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error {
for _, missingAlert := range alerts { for _, missingAlert := range alerts {
missing := true missing := true
for _, k := range posted { for _, k := range cmd.Alerts {
if missingAlert.PanelId == k.PanelId { if missingAlert.PanelId == k.PanelId {
missing = false missing = false
break break
...@@ -237,7 +256,13 @@ func deleteMissingAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Sessio ...@@ -237,7 +256,13 @@ func deleteMissingAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Sessio
sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id) sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id)
err = SaveAlertChange("DELETED", missingAlert, sess) SaveAlertChange(&m.CreateAlertChangeCommand{
OrgId: missingAlert.OrgId,
AlertId: missingAlert.Id,
NewAlertSettings: missingAlert.Settings,
UpdatedBy: cmd.UserId,
Type: "DELETED",
}, sess)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -22,7 +22,9 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error { ...@@ -22,7 +22,9 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
alert_change.org_id, alert_change.org_id,
alert_change.alert_id, alert_change.alert_id,
alert_change.type, alert_change.type,
alert_change.created alert_change.created,
alert_change.new_alert_settings,
alert_change.updated_by
FROM alert_change FROM alert_change
`) `)
...@@ -48,12 +50,14 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error { ...@@ -48,12 +50,14 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
return nil return nil
} }
func SaveAlertChange(change string, alert *m.Alert, sess *xorm.Session) error { func SaveAlertChange(cmd *m.CreateAlertChangeCommand, sess *xorm.Session) error {
_, err := sess.Insert(&m.AlertChange{ _, err := sess.Insert(&m.AlertChange{
OrgId: alert.OrgId, OrgId: cmd.OrgId,
Type: change, Type: cmd.Type,
Created: time.Now(), Created: time.Now(),
AlertId: alert.Id, AlertId: cmd.AlertId,
NewAlertSettings: cmd.NewAlertSettings,
UpdatedBy: cmd.UpdatedBy,
}) })
if err != nil { if err != nil {
......
...@@ -69,10 +69,16 @@ func TestAlertRuleChangesDataAccess(t *testing.T) { ...@@ -69,10 +69,16 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
Convey("add 4 updates", func() { Convey("add 4 updates", func() {
sess := x.NewSession() sess := x.NewSession()
SaveAlertChange("UPDATED", items[0], sess) updateCmd := m.CreateAlertChangeCommand{
SaveAlertChange("UPDATED", items[0], sess) AlertId: items[0].Id,
SaveAlertChange("UPDATED", items[0], sess) OrgId: items[0].OrgId,
SaveAlertChange("UPDATED", items[0], sess) UpdatedBy: 1,
}
SaveAlertChange(&updateCmd, sess)
SaveAlertChange(&updateCmd, sess)
SaveAlertChange(&updateCmd, sess)
SaveAlertChange(&updateCmd, sess)
sess.Commit() sess.Commit()
Convey("query for max one change", func() { Convey("query for max one change", func() {
......
...@@ -36,6 +36,8 @@ func addAlertMigrations(mg *Migrator) { ...@@ -36,6 +36,8 @@ func addAlertMigrations(mg *Migrator) {
{Name: "org_id", Type: DB_BigInt, Nullable: false}, {Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "type", Type: DB_NVarchar, Length: 50, Nullable: false}, {Name: "type", Type: DB_NVarchar, Length: 50, Nullable: false},
{Name: "created", Type: DB_DateTime, 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},
}, },
} }
......
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