Revert changes post code review and move them to notification page

This commit is contained in:
John Baublitz
2018-05-19 16:21:00 -04:00
committed by bergquist
parent e068be4c26
commit 3cb0e27e1c
34 changed files with 215 additions and 107 deletions

View File

@@ -22,7 +22,6 @@ func init() {
bus.AddHandler("sql", GetAlertStatesForDashboard)
bus.AddHandler("sql", PauseAlert)
bus.AddHandler("sql", PauseAllAlerts)
bus.AddHandler("sql", IncAlertEval)
}
func GetAlertById(query *m.GetAlertByIdQuery) error {
@@ -189,7 +188,7 @@ func updateAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *DBS
if alertToUpdate.ContainsUpdates(alert) {
alert.Updated = timeNow()
alert.State = alertToUpdate.State
sess.MustCols("message", "notify_freq", "notify_once")
sess.MustCols("message")
_, err := sess.Id(alert.Id).Update(alert)
if err != nil {
return err
@@ -344,22 +343,3 @@ func GetAlertStatesForDashboard(query *m.GetAlertStatesForDashboardQuery) error
return err
}
func IncAlertEval(cmd *m.IncAlertEvalCommand) error {
return inTransaction(func(sess *DBSession) error {
alert := m.Alert{}
if _, err := sess.Id(cmd.AlertId).Get(&alert); err != nil {
return err
}
alert.NotifyEval = (alert.NotifyEval + 1) % alert.NotifyFreq
sess.MustCols("notify_eval")
if _, err := sess.Id(cmd.AlertId).Update(alert); err != nil {
return err
}
return nil
})
}

View File

@@ -17,6 +17,9 @@ func init() {
bus.AddHandler("sql", DeleteAlertNotification)
bus.AddHandler("sql", GetAlertNotificationsToSend)
bus.AddHandler("sql", GetAllAlertNotifications)
bus.AddHandler("sql", RecordNotificationJournal)
bus.AddHandler("sql", GetLatestNotification)
bus.AddHandler("sql", CleanNotificationJournal)
}
func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
@@ -138,13 +141,15 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error
}
alertNotification := &m.AlertNotification{
OrgId: cmd.OrgId,
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,
Created: time.Now(),
Updated: time.Now(),
IsDefault: cmd.IsDefault,
OrgId: cmd.OrgId,
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,
NotifyOnce: cmd.NotifyOnce,
Frequency: cmd.Frequency,
Created: time.Now(),
Updated: time.Now(),
IsDefault: cmd.IsDefault,
}
if _, err = sess.Insert(alertNotification); err != nil {
@@ -192,3 +197,42 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
return nil
})
}
func RecordNotificationJournal(cmd *m.RecordNotificationJournalCommand) error {
return inTransaction(func(sess *DBSession) error {
journalEntry := &m.NotificationJournal{
OrgId: cmd.OrgId,
AlertId: cmd.AlertId,
NotifierId: cmd.NotifierId,
SentAt: cmd.SentAt,
Success: cmd.Success,
}
if _, err := sess.Insert(journalEntry); err != nil {
return err
}
return nil
})
}
func GetLatestNotification(cmd *m.GetLatestNotificationQuery) error {
return inTransaction(func(sess *DBSession) error {
notificationJournal := &m.NotificationJournal{}
_, err := sess.OrderBy("notification_journal.sent_at").Desc().Where("notification_journal.org_id = ? AND notification_journal.alert_id = ? AND notification_journal.notifier_id = ?", cmd.OrgId, cmd.AlertId, cmd.NotifierId).Get(notificationJournal)
if err != nil {
return err
}
cmd.Result = notificationJournal
return nil
})
}
func CleanNotificationJournal(cmd *m.CleanNotificationJournalCommand) error {
return inTransaction(func(sess *DBSession) error {
sql := "DELETE FROM notification_journal WHERE notification_journal.org_id = ? AND notification_journal.alert_id = ? AND notification_journal.notifier_id = ?"
_, err := sess.Exec(sql, cmd.OrgId, cmd.AlertId, cmd.NotifierId)
return err
})
}

View File

@@ -29,9 +29,6 @@ func addAlertMigrations(mg *Migrator) {
{Name: "state_changes", Type: DB_Int, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
{Name: "notify_once", Type: DB_Bool, Nullable: false},
{Name: "notify_freq", Type: DB_Int, Nullable: false},
{Name: "notify_eval", Type: DB_Int, Nullable: false},
},
Indices: []*Index{
{Cols: []string{"org_id", "id"}, Type: IndexType},
@@ -68,8 +65,32 @@ func addAlertMigrations(mg *Migrator) {
mg.AddMigration("Add column is_default", NewAddColumnMigration(alert_notification, &Column{
Name: "is_default", Type: DB_Bool, Nullable: false, Default: "0",
}))
mg.AddMigration("Add column frequency", NewAddColumnMigration(alert_notification, &Column{
Name: "frequency", Type: DB_BigInt, Nullable: true,
}))
mg.AddMigration("Add column notify_once", NewAddColumnMigration(alert_notification, &Column{
Name: "notify_once", Type: DB_Bool, Nullable: false, Default: "1",
}))
mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
notification_journal := Table{
Name: "notification_journal",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "alert_id", Type: DB_BigInt, Nullable: false},
{Name: "notifier_id", Type: DB_BigInt, Nullable: false},
{Name: "sent_at", Type: DB_DateTime, Nullable: false},
{Name: "success", Type: DB_Bool, Nullable: false},
},
Indices: []*Index{
{Cols: []string{"org_id", "alert_id", "notifier_id"}, Type: IndexType},
},
}
mg.AddMigration("create notification_journal table v1", NewAddTableMigration(notification_journal))
mg.AddMigration("add index notification_journal org_id & alert_id & notifier_id", NewAddIndexMigration(notification_journal, notification_journal.Indices[0]))
mg.AddMigration("Update alert table charset", NewTableCharsetMigration("alert", []*Column{
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "message", Type: DB_Text, Nullable: false},