feat(alerting): more model changes
This commit is contained in:
@@ -18,20 +18,20 @@ func init() {
|
||||
}
|
||||
|
||||
func updateDashboardAlerts(cmd *UpdateDashboardAlertsCommand) error {
|
||||
saveRulesCmd := m.SaveAlertsCommand{
|
||||
saveAlerts := m.SaveAlertsCommand{
|
||||
OrgId: cmd.OrgId,
|
||||
UserId: cmd.UserId,
|
||||
}
|
||||
|
||||
extractor := NewAlertRuleExtractor(cmd.Dashboard, cmd.OrgId)
|
||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId)
|
||||
|
||||
rules, err := extractor.GetRuleModels()
|
||||
alerts, err := extractor.GetRuleModels()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
saveRulesCmd.Alerts = rules
|
||||
if bus.Dispatch(&saveRulesCmd); err != nil {
|
||||
saveAlerts.Alerts = alerts
|
||||
if bus.Dispatch(&saveAlerts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -9,21 +9,21 @@ import (
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type AlertRuleExtractor struct {
|
||||
type DashAlertExtractor struct {
|
||||
Dash *m.Dashboard
|
||||
OrgId int64
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func NewAlertRuleExtractor(dash *m.Dashboard, orgId int64) *AlertRuleExtractor {
|
||||
return &AlertRuleExtractor{
|
||||
func NewDashAlertExtractor(dash *m.Dashboard, orgId int64) *DashAlertExtractor {
|
||||
return &DashAlertExtractor{
|
||||
Dash: dash,
|
||||
OrgId: orgId,
|
||||
log: log.New("alerting.extractor"),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *AlertRuleExtractor) lookupDatasourceId(dsName string) (int64, error) {
|
||||
func (e *DashAlertExtractor) lookupDatasourceId(dsName string) (int64, error) {
|
||||
if dsName == "" {
|
||||
query := &m.GetDataSourcesQuery{OrgId: e.OrgId}
|
||||
if err := bus.Dispatch(query); err != nil {
|
||||
@@ -47,36 +47,36 @@ func (e *AlertRuleExtractor) lookupDatasourceId(dsName string) (int64, error) {
|
||||
return 0, errors.New("Could not find datasource id for " + dsName)
|
||||
}
|
||||
|
||||
func (e *AlertRuleExtractor) GetRuleModels() (m.AlertRules, error) {
|
||||
func (e *DashAlertExtractor) GetRuleModels() ([]*m.Alert, error) {
|
||||
|
||||
rules := make(m.AlertRules, 0)
|
||||
alerts := make([]*m.Alert, 0)
|
||||
|
||||
for _, rowObj := range e.Dash.Data.Get("rows").MustArray() {
|
||||
row := simplejson.NewFromAny(rowObj)
|
||||
|
||||
for _, panelObj := range row.Get("panels").MustArray() {
|
||||
panel := simplejson.NewFromAny(panelObj)
|
||||
jsonRule := panel.Get("alerting")
|
||||
jsonAlert := panel.Get("alert")
|
||||
|
||||
// check if marked for deletion
|
||||
deleted := jsonRule.Get("deleted").MustBool()
|
||||
deleted := jsonAlert.Get("deleted").MustBool()
|
||||
if deleted {
|
||||
e.log.Info("Deleted alert rule found")
|
||||
continue
|
||||
}
|
||||
|
||||
ruleModel := &m.Alert{
|
||||
alert := &m.Alert{
|
||||
DashboardId: e.Dash.Id,
|
||||
OrgId: e.OrgId,
|
||||
PanelId: panel.Get("id").MustInt64(),
|
||||
Id: jsonRule.Get("id").MustInt64(),
|
||||
Name: jsonRule.Get("name").MustString(),
|
||||
Scheduler: jsonRule.Get("scheduler").MustInt64(),
|
||||
Enabled: jsonRule.Get("enabled").MustBool(),
|
||||
Description: jsonRule.Get("description").MustString(),
|
||||
Id: jsonAlert.Get("id").MustInt64(),
|
||||
Name: jsonAlert.Get("name").MustString(),
|
||||
Scheduler: jsonAlert.Get("scheduler").MustInt64(),
|
||||
Enabled: jsonAlert.Get("enabled").MustBool(),
|
||||
Description: jsonAlert.Get("description").MustString(),
|
||||
}
|
||||
|
||||
valueQuery := jsonRule.Get("query")
|
||||
valueQuery := jsonAlert.Get("query")
|
||||
valueQueryRef := valueQuery.Get("refId").MustString()
|
||||
for _, targetsObj := range panel.Get("targets").MustArray() {
|
||||
target := simplejson.NewFromAny(targetsObj)
|
||||
@@ -97,24 +97,24 @@ func (e *AlertRuleExtractor) GetRuleModels() (m.AlertRules, error) {
|
||||
|
||||
targetQuery := target.Get("target").MustString()
|
||||
if targetQuery != "" {
|
||||
jsonRule.SetPath([]string{"query", "query"}, targetQuery)
|
||||
jsonAlert.SetPath([]string{"query", "query"}, targetQuery)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ruleModel.Expression = jsonRule
|
||||
alert.Expression = jsonAlert
|
||||
|
||||
// validate
|
||||
_, err := NewAlertRuleFromDBModel(ruleModel)
|
||||
if err == nil && ruleModel.ValidToSave() {
|
||||
rules = append(rules, ruleModel)
|
||||
_, err := NewAlertRuleFromDBModel(alert)
|
||||
if err == nil && alert.ValidToSave() {
|
||||
alerts = append(alerts, alert)
|
||||
} else {
|
||||
e.log.Error("Failed to extract alert rules from dashboard", "error", err)
|
||||
return nil, errors.New("Failed to extract alert rules from dashboard")
|
||||
e.log.Error("Failed to extract alerts from dashboard", "error", err)
|
||||
return nil, errors.New("Failed to extract alerts from dashboard")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return rules, nil
|
||||
return alerts, nil
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
}
|
||||
],
|
||||
"datasource": null,
|
||||
"alerting": {
|
||||
"alert": {
|
||||
"name": "name1",
|
||||
"description": "desc1",
|
||||
"scheduler": 1,
|
||||
@@ -71,7 +71,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
}
|
||||
],
|
||||
"datasource": "graphite2",
|
||||
"alerting": {
|
||||
"alert": {
|
||||
"name": "name2",
|
||||
"description": "desc2",
|
||||
"scheduler": 0,
|
||||
@@ -150,7 +150,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
"title": "Broken influxdb panel",
|
||||
"transform": "table",
|
||||
"type": "table",
|
||||
"alerting": {
|
||||
"alert": {
|
||||
"deleted": true
|
||||
}
|
||||
}
|
||||
@@ -164,7 +164,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewAlertRuleExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
|
||||
// mock data
|
||||
defaultDs := &m.DataSource{Id: 12, OrgId: 2, Name: "I am default", IsDefault: true}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user