浏览代码

feat(alerting): renamed AlertRuleModel to just Alert, think making a distinction between an Alert and an Alert Rule is just confusing and was a mistake on my part

Torkel Ödegaard 9 年之前
父节点
当前提交
a362984c57

+ 1 - 1
pkg/api/alerting.go

@@ -43,7 +43,7 @@ func GetAlertChanges(c *middleware.Context) Response {
 	return Json(200, query.Result)
 }
 
-// GET /api/alerts
+// GET /api/alerts/rules/
 func GetAlerts(c *middleware.Context) Response {
 	query := models.GetAlertsQuery{
 		OrgId:       c.OrgId,

+ 11 - 19
pkg/models/alerts.go → pkg/models/alert.go

@@ -6,7 +6,7 @@ import (
 	"github.com/grafana/grafana/pkg/components/simplejson"
 )
 
-type AlertRuleModel struct {
+type Alert struct {
 	Id          int64
 	OrgId       int64
 	DashboardId int64
@@ -24,17 +24,11 @@ type AlertRuleModel struct {
 	Expression *simplejson.Json
 }
 
-type AlertRules []*AlertRuleModel
-
-func (this AlertRuleModel) TableName() string {
-	return "alert_rule"
-}
-
-func (alertRule *AlertRuleModel) ValidToSave() bool {
-	return alertRule.DashboardId != 0
+func (alert *Alert) ValidToSave() bool {
+	return alert.DashboardId != 0
 }
 
-func (this *AlertRuleModel) ContainsUpdates(other *AlertRuleModel) bool {
+func (this *Alert) ContainsUpdates(other *Alert) bool {
 	result := false
 	result = result || this.Name != other.Name
 	result = result || this.Description != other.Description
@@ -51,7 +45,6 @@ func (this *AlertRuleModel) ContainsUpdates(other *AlertRuleModel) bool {
 	}
 
 	//don't compare .State! That would be insane.
-
 	return result
 }
 
@@ -70,11 +63,10 @@ type HeartBeat struct {
 
 type HeartBeatCommand struct {
 	ServerId string
-
-	Result AlertingClusterInfo
+	Result   AlertingClusterInfo
 }
 
-type AlertRuleChange struct {
+type AlertChange struct {
 	Id      int64     `json:"id"`
 	OrgId   int64     `json:"-"`
 	AlertId int64     `json:"alertId"`
@@ -88,7 +80,7 @@ type SaveAlertsCommand struct {
 	UserId      int64
 	OrgId       int64
 
-	Alerts AlertRules
+	Alerts []*Alert
 }
 
 type DeleteAlertCommand struct {
@@ -102,17 +94,17 @@ type GetAlertsQuery struct {
 	DashboardId int64
 	PanelId     int64
 
-	Result []*AlertRuleModel
+	Result []*Alert
 }
 
 type GetAllAlertsQuery struct {
-	Result []*AlertRuleModel
+	Result []*Alert
 }
 
 type GetAlertByIdQuery struct {
 	Id int64
 
-	Result *AlertRuleModel
+	Result *Alert
 }
 
 type GetAlertChangesQuery struct {
@@ -120,5 +112,5 @@ type GetAlertChangesQuery struct {
 	Limit   int64
 	SinceId int64
 
-	Result []*AlertRuleChange
+	Result []*AlertChange
 }

+ 1 - 1
pkg/models/alerts_state.go → pkg/models/alert_state.go

@@ -31,7 +31,7 @@ type UpdateAlertStateCommand struct {
 	NewState string `json:"newState" binding:"Required"`
 	Info     string `json:"info"`
 
-	Result *AlertRuleModel
+	Result *Alert
 }
 
 // Queries

+ 2 - 2
pkg/models/alerts_test.go → pkg/models/alert_test.go

@@ -13,13 +13,13 @@ func TestAlertingModelTest(t *testing.T) {
 		json1, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
 		json2, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
 
-		rule1 := &AlertRuleModel{
+		rule1 := &Alert{
 			Expression:  json1,
 			Name:        "Namn",
 			Description: "Description",
 		}
 
-		rule2 := &AlertRuleModel{
+		rule2 := &Alert{
 			Expression:  json2,
 			Name:        "Namn",
 			Description: "Description",

+ 1 - 1
pkg/services/alerting/alert_rule.go

@@ -25,7 +25,7 @@ type AlertRule struct {
 	Transformer     Transformer
 }
 
-func NewAlertRuleFromDBModel(ruleDef *m.AlertRuleModel) (*AlertRule, error) {
+func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) {
 	model := &AlertRule{}
 	model.Id = ruleDef.Id
 	model.OrgId = ruleDef.OrgId

+ 1 - 1
pkg/services/alerting/commands.go

@@ -38,7 +38,7 @@ func updateDashboardAlerts(cmd *UpdateDashboardAlertsCommand) error {
 	return nil
 }
 
-func ConvetAlertModelToAlertRule(ruleDef *m.AlertRuleModel) (*AlertRule, error) {
+func ConvetAlertModelToAlertRule(ruleDef *m.Alert) (*AlertRule, error) {
 	model := &AlertRule{}
 	model.Id = ruleDef.Id
 	model.OrgId = ruleDef.OrgId

+ 1 - 1
pkg/services/alerting/extractor.go

@@ -65,7 +65,7 @@ func (e *AlertRuleExtractor) GetRuleModels() (m.AlertRules, error) {
 				continue
 			}
 
-			ruleModel := &m.AlertRuleModel{
+			ruleModel := &m.Alert{
 				DashboardId: e.Dash.Id,
 				OrgId:       e.OrgId,
 				PanelId:     panel.Get("id").MustInt64(),

+ 10 - 10
pkg/services/sqlstore/alert_rule.go → pkg/services/sqlstore/alert.go

@@ -64,7 +64,7 @@ func HeartBeat(query *m.HeartBeatCommand) error {
 */
 
 func GetAlertById(query *m.GetAlertByIdQuery) error {
-	alert := m.AlertRuleModel{}
+	alert := m.Alert{}
 	has, err := x.Id(query.Id).Get(&alert)
 	if !has {
 		return fmt.Errorf("could not find alert")
@@ -78,7 +78,7 @@ func GetAlertById(query *m.GetAlertByIdQuery) error {
 }
 
 func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
-	var alerts []*m.AlertRuleModel
+	var alerts []*m.Alert
 	err := x.Sql("select * from alert_rule").Find(&alerts)
 	if err != nil {
 		return err
@@ -131,7 +131,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error {
 		sql.WriteString(")")
 	}
 
-	alerts := make([]*m.AlertRuleModel, 0)
+	alerts := make([]*m.Alert, 0)
 	if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil {
 		return err
 	}
@@ -141,7 +141,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error {
 }
 
 func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
-	alerts := make([]*m.AlertRuleModel, 0)
+	alerts := make(m.Alerts, 0)
 	sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
 
 	for _, alert := range alerts {
@@ -172,10 +172,10 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
 	})
 }
 
-func upsertAlerts(alerts []*m.AlertRuleModel, posted []*m.AlertRuleModel, sess *xorm.Session) error {
+func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) error {
 	for _, alert := range posted {
 		update := false
-		var alertToUpdate *m.AlertRuleModel
+		var alertToUpdate *m.Alert
 
 		for _, k := range alerts {
 			if alert.PanelId == k.PanelId {
@@ -212,7 +212,7 @@ func upsertAlerts(alerts []*m.AlertRuleModel, posted []*m.AlertRuleModel, sess *
 	return nil
 }
 
-func deleteMissingAlerts(alerts []*m.AlertRuleModel, posted []*m.AlertRuleModel, sess *xorm.Session) error {
+func deleteMissingAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) error {
 	for _, missingAlert := range alerts {
 		missing := true
 
@@ -238,12 +238,12 @@ func deleteMissingAlerts(alerts []*m.AlertRuleModel, posted []*m.AlertRuleModel,
 	return nil
 }
 
-func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.AlertRuleModel, error) {
-	alerts := make([]*m.AlertRuleModel, 0)
+func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.Alert, error) {
+	alerts := make([]*m.Alert, 0)
 	err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
 
 	if err != nil {
-		return []*m.AlertRuleModel{}, err
+		return []*m.Alert{}, err
 	}
 
 	return alerts, nil

+ 1 - 1
pkg/services/sqlstore/alert_rule_changes.go

@@ -48,7 +48,7 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
 	return nil
 }
 
-func SaveAlertChange(change string, alert *m.AlertRuleModel, sess *xorm.Session) error {
+func SaveAlertChange(change string, alert *m.Alert, sess *xorm.Session) error {
 	_, err := sess.Insert(&m.AlertRuleChange{
 		OrgId:   alert.OrgId,
 		Type:    change,

+ 1 - 1
pkg/services/sqlstore/alert_rule_changes_test.go

@@ -20,7 +20,7 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
 		var err error
 
 		Convey("When dashboard is removed", func() {
-			items := []*m.AlertRuleModel{
+			items := []*m.Alert{
 				{
 					PanelId:     1,
 					DashboardId: testDash.Id,

+ 1 - 1
pkg/services/sqlstore/alert_rule_parser_test.go

@@ -12,7 +12,7 @@ import (
 func TestAlertRuleModelParsing(t *testing.T) {
 
 	Convey("Parsing alertRule from expression", t, func() {
-		alertRuleDAO := &m.AlertRuleModel{}
+		alertRuleDAO := &m.Alert{}
 		json, _ := simplejson.NewJson([]byte(`
       {
         "frequency": 10,

+ 3 - 3
pkg/services/sqlstore/alert_rule_test.go

@@ -14,7 +14,7 @@ func TestAlertingDataAccess(t *testing.T) {
 
 		testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
 
-		items := []*m.AlertRuleModel{
+		items := []*m.Alert{
 			{
 				PanelId:     1,
 				DashboardId: testDash.Id,
@@ -96,7 +96,7 @@ func TestAlertingDataAccess(t *testing.T) {
 		})
 
 		Convey("Multiple alerts per dashboard", func() {
-			multipleItems := []*m.AlertRuleModel{
+			multipleItems := []*m.Alert{
 				{
 					DashboardId: testDash.Id,
 					PanelId:     1,
@@ -161,7 +161,7 @@ func TestAlertingDataAccess(t *testing.T) {
 		})
 
 		Convey("When dashboard is removed", func() {
-			items := []*m.AlertRuleModel{
+			items := []*m.Alert{
 				{
 					PanelId:     1,
 					DashboardId: testDash.Id,

+ 3 - 2
pkg/services/sqlstore/alert_state.go

@@ -2,10 +2,11 @@ package sqlstore
 
 import (
 	"fmt"
+	"time"
+
 	"github.com/go-xorm/xorm"
 	"github.com/grafana/grafana/pkg/bus"
 	m "github.com/grafana/grafana/pkg/models"
-	"time"
 )
 
 func init() {
@@ -19,7 +20,7 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
 			return fmt.Errorf("new state is invalid")
 		}
 
-		alert := m.AlertRuleModel{}
+		alert := m.Alert{}
 		has, err := sess.Id(cmd.AlertId).Get(&alert)
 		if !has {
 			return fmt.Errorf("Could not find alert")

+ 1 - 1
pkg/services/sqlstore/alert_state_test.go

@@ -13,7 +13,7 @@ func TestAlertingStateAccess(t *testing.T) {
 
 		testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
 
-		items := []*m.AlertRuleModel{
+		items := []*m.Alert{
 			{
 				PanelId:     1,
 				DashboardId: testDash.Id,