Commit 4fc16c36 by bergquist

feat(alerting): saves new state when alert updates

parent 5a65dc5d
......@@ -71,14 +71,24 @@ type HeartBeatCommand struct {
}
type AlertChange struct {
Id int64 `json:"id"`
OrgId int64 `json:"-"`
AlertId int64 `json:"alertId"`
Type string `json:"type"`
Created time.Time `json:"created"`
Id int64 `json:"id"`
OrgId int64 `json:"-"`
AlertId int64 `json:"alertId"`
UpdatedBy int64 `json:"updatedBy"`
NewAlertSettings *simplejson.Json `json:"newAlertSettings"`
Type string `json:"type"`
Created time.Time `json:"created"`
}
// Commands
type CreateAlertChangeCommand struct {
OrgId int64
AlertId int64
UpdatedBy int64
NewAlertSettings *simplejson.Json
Type string
}
type SaveAlertsCommand struct {
DashboardId int64
UserId int64
......
......@@ -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)
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
}
}
......@@ -167,15 +174,15 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
return err
}
upsertAlerts(alerts, cmd.Alerts, sess)
deleteMissingAlerts(alerts, cmd.Alerts, sess)
upsertAlerts(alerts, cmd, sess)
deleteMissingAlerts(alerts, cmd, sess)
return nil
})
}
func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) error {
for _, alert := range posted {
func upsertAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error {
for _, alert := range cmd.Alerts {
update := false
var alertToUpdate *m.Alert
......@@ -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)
SaveAlertChange("UPDATED", alert, sess)
SaveAlertChange(&m.CreateAlertChangeCommand{
OrgId: alert.OrgId,
AlertId: alert.Id,
NewAlertSettings: alert.Settings,
UpdatedBy: cmd.UserId,
Type: "UPDATED",
}, sess)
}
} else {
......@@ -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)
SaveAlertChange("CREATED", alert, sess)
SaveAlertChange(&m.CreateAlertChangeCommand{
OrgId: alert.OrgId,
AlertId: alert.Id,
NewAlertSettings: alert.Settings,
UpdatedBy: cmd.UserId,
Type: "CREATED",
}, sess)
}
}
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 {
missing := true
for _, k := range posted {
for _, k := range cmd.Alerts {
if missingAlert.PanelId == k.PanelId {
missing = false
break
......@@ -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)
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 {
return err
}
......
......@@ -22,7 +22,9 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
alert_change.org_id,
alert_change.alert_id,
alert_change.type,
alert_change.created
alert_change.created,
alert_change.new_alert_settings,
alert_change.updated_by
FROM alert_change
`)
......@@ -48,12 +50,14 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
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{
OrgId: alert.OrgId,
Type: change,
Created: time.Now(),
AlertId: alert.Id,
OrgId: cmd.OrgId,
Type: cmd.Type,
Created: time.Now(),
AlertId: cmd.AlertId,
NewAlertSettings: cmd.NewAlertSettings,
UpdatedBy: cmd.UpdatedBy,
})
if err != nil {
......
......@@ -69,10 +69,16 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
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)
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() {
......
......@@ -36,6 +36,8 @@ func addAlertMigrations(mg *Migrator) {
{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},
},
}
......
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