|
|
@@ -2,14 +2,49 @@ package sqlstore
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
+ "github.com/go-xorm/xorm"
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
func init() {
|
|
|
bus.AddHandler("sql", SaveAlerts)
|
|
|
}
|
|
|
|
|
|
+func SaveAlertChange(change string, alert m.AlertRule) error {
|
|
|
+ return inTransaction(func(sess *xorm.Session) error {
|
|
|
+ _, err := sess.Insert(&m.AlertRuleChange{
|
|
|
+ OrgId: alert.OrgId,
|
|
|
+ Type: change,
|
|
|
+ Created: time.Now(),
|
|
|
+ AlertId: alert.Id,
|
|
|
+ })
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func alertIsDifferent(rule1, rule2 m.AlertRule) bool {
|
|
|
+ result := false
|
|
|
+
|
|
|
+ result = result || rule1.Aggregator != rule2.Aggregator
|
|
|
+ result = result || rule1.CritLevel != rule2.CritLevel
|
|
|
+ result = result || rule1.WarnLevel != rule2.WarnLevel
|
|
|
+ result = result || rule1.Query != rule2.Query
|
|
|
+ result = result || rule1.QueryRefId != rule2.QueryRefId
|
|
|
+ result = result || rule1.Interval != rule2.Interval
|
|
|
+ result = result || rule1.Title != rule2.Title
|
|
|
+ result = result || rule1.Description != rule2.Description
|
|
|
+ result = result || rule1.QueryRange != rule2.QueryRange
|
|
|
+
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
|
|
//this function should be refactored
|
|
|
|
|
|
@@ -22,24 +57,33 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
|
|
|
|
|
for _, alert := range *cmd.Alerts {
|
|
|
update := false
|
|
|
+ var alertToUpdate m.AlertRule
|
|
|
|
|
|
for _, k := range alerts {
|
|
|
if alert.PanelId == k.PanelId {
|
|
|
update = true
|
|
|
alert.Id = k.Id
|
|
|
+ alertToUpdate = k
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if update {
|
|
|
- _, err = x.Id(alert.Id).Update(&alert)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+
|
|
|
+ if alertIsDifferent(alertToUpdate, alert) {
|
|
|
+ _, err = x.Id(alert.Id).Update(&alert)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ SaveAlertChange("UPDATED", alert)
|
|
|
}
|
|
|
+
|
|
|
} else {
|
|
|
_, err = x.Insert(&alert)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+ SaveAlertChange("CREATED", alert)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -58,6 +102,7 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ err = SaveAlertChange("DELETED", missingAlert)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -95,3 +140,14 @@ func GetAlertsByDashboardAndPanelId(dashboardId, panelId int64) (m.AlertRule, er
|
|
|
|
|
|
return alerts[0], nil
|
|
|
}
|
|
|
+
|
|
|
+func GetAlertRuleChanges(orgid int64) ([]m.AlertRuleChange, error) {
|
|
|
+ alertChanges := make([]m.AlertRuleChange, 0)
|
|
|
+ err := x.Where("org_id = ?", orgid).Find(&alertChanges)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return []m.AlertRuleChange{}, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return alertChanges, nil
|
|
|
+}
|