feat(alerting): more model changes

This commit is contained in:
Torkel Ödegaard
2016-06-11 10:54:24 +02:00
parent a362984c57
commit 382f396247
9 changed files with 97 additions and 98 deletions

View File

@@ -79,7 +79,7 @@ func GetAlertById(query *m.GetAlertByIdQuery) error {
func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
var alerts []*m.Alert
err := x.Sql("select * from alert_rule").Find(&alerts)
err := x.Sql("select * from alert").Find(&alerts)
if err != nil {
return err
}
@@ -90,7 +90,7 @@ func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
func DeleteAlertById(cmd *m.DeleteAlertCommand) error {
return inTransaction(func(sess *xorm.Session) error {
if _, err := sess.Exec("DELETE FROM alert_rule WHERE id = ?", cmd.AlertId); err != nil {
if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", cmd.AlertId); err != nil {
return err
}
@@ -103,7 +103,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error {
params := make([]interface{}, 0)
sql.WriteString(`SELECT *
from alert_rule
from alert
`)
sql.WriteString(`WHERE org_id = ?`)
@@ -141,15 +141,17 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error {
}
func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
alerts := make(m.Alerts, 0)
alerts := make([]*m.Alert, 0)
sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
for _, alert := range alerts {
_, err := sess.Exec("DELETE FROM alert_rule WHERE id = ? ", alert.Id)
_, err := sess.Exec("DELETE FROM alert WHERE id = ? ", alert.Id)
if err != nil {
return err
}
sqlog.Debug("Alert deleted (due to dashboard deletion)", "name", alert.Name, "id", alert.Id)
if err := SaveAlertChange("DELETED", alert, sess); err != nil {
return err
}
@@ -194,6 +196,7 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro
return err
}
sqlog.Debug("Alert updated", "name", alert.Name, "id", alert.Id)
SaveAlertChange("UPDATED", alert, sess)
}
@@ -205,6 +208,8 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro
if err != nil {
return err
}
sqlog.Debug("Alert inserted", "name", alert.Name, "id", alert.Id)
SaveAlertChange("CREATED", alert, sess)
}
}
@@ -223,11 +228,13 @@ func deleteMissingAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Sessio
}
if missing {
_, err := sess.Exec("DELETE FROM alert_rule WHERE id = ?", missingAlert.Id)
_, err := sess.Exec("DELETE FROM alert WHERE id = ?", missingAlert.Id)
if err != nil {
return err
}
sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id)
err = SaveAlertChange("DELETED", missingAlert, sess)
if err != nil {
return err

View File

@@ -39,7 +39,7 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
params = append(params, query.Limit)
}
alertChanges := make([]*m.AlertRuleChange, 0)
alertChanges := make([]*m.AlertChange, 0)
if err := x.Sql(sql.String(), params...).Find(&alertChanges); err != nil {
return err
}
@@ -49,7 +49,7 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
}
func SaveAlertChange(change string, alert *m.Alert, sess *xorm.Session) error {
_, err := sess.Insert(&m.AlertRuleChange{
_, err := sess.Insert(&m.AlertChange{
OrgId: alert.OrgId,
Type: change,
Created: time.Now(),

View File

@@ -7,7 +7,7 @@ import (
func addAlertMigrations(mg *Migrator) {
alertV1 := Table{
Name: "alert_rule",
Name: "alert",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
@@ -26,10 +26,10 @@ func addAlertMigrations(mg *Migrator) {
}
// create table
mg.AddMigration("create alert_rule table v2", NewAddTableMigration(alertV1))
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
alert_changes := Table{
Name: "alert_rule_change",
Name: "alert_change",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "alert_id", Type: DB_BigInt, Nullable: false},
@@ -39,7 +39,7 @@ func addAlertMigrations(mg *Migrator) {
},
}
mg.AddMigration("create alert_rules_updates table v1", NewAddTableMigration(alert_changes))
mg.AddMigration("create alert_change table v1", NewAddTableMigration(alert_changes))
alert_state_log := Table{
Name: "alert_state",

View File

@@ -107,7 +107,7 @@ func (mg *Migrator) Start() error {
}
func (mg *Migrator) exec(m Migration) error {
log.Info("Executing migration", "id", m.Id())
mg.Logger.Info("Executing migration", "id", m.Id())
err := mg.inTransaction(func(sess *xorm.Session) error {