mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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
This commit is contained in:
@@ -43,7 +43,7 @@ func GetAlertChanges(c *middleware.Context) Response {
|
|||||||
return Json(200, query.Result)
|
return Json(200, query.Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/alerts
|
// GET /api/alerts/rules/
|
||||||
func GetAlerts(c *middleware.Context) Response {
|
func GetAlerts(c *middleware.Context) Response {
|
||||||
query := models.GetAlertsQuery{
|
query := models.GetAlertsQuery{
|
||||||
OrgId: c.OrgId,
|
OrgId: c.OrgId,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AlertRuleModel struct {
|
type Alert struct {
|
||||||
Id int64
|
Id int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
DashboardId int64
|
DashboardId int64
|
||||||
@@ -24,17 +24,11 @@ type AlertRuleModel struct {
|
|||||||
Expression *simplejson.Json
|
Expression *simplejson.Json
|
||||||
}
|
}
|
||||||
|
|
||||||
type AlertRules []*AlertRuleModel
|
func (alert *Alert) ValidToSave() bool {
|
||||||
|
return alert.DashboardId != 0
|
||||||
func (this AlertRuleModel) TableName() string {
|
|
||||||
return "alert_rule"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (alertRule *AlertRuleModel) ValidToSave() bool {
|
func (this *Alert) ContainsUpdates(other *Alert) bool {
|
||||||
return alertRule.DashboardId != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *AlertRuleModel) ContainsUpdates(other *AlertRuleModel) bool {
|
|
||||||
result := false
|
result := false
|
||||||
result = result || this.Name != other.Name
|
result = result || this.Name != other.Name
|
||||||
result = result || this.Description != other.Description
|
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.
|
//don't compare .State! That would be insane.
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,11 +63,10 @@ type HeartBeat struct {
|
|||||||
|
|
||||||
type HeartBeatCommand struct {
|
type HeartBeatCommand struct {
|
||||||
ServerId string
|
ServerId string
|
||||||
|
Result AlertingClusterInfo
|
||||||
Result AlertingClusterInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AlertRuleChange struct {
|
type AlertChange struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
OrgId int64 `json:"-"`
|
OrgId int64 `json:"-"`
|
||||||
AlertId int64 `json:"alertId"`
|
AlertId int64 `json:"alertId"`
|
||||||
@@ -88,7 +80,7 @@ type SaveAlertsCommand struct {
|
|||||||
UserId int64
|
UserId int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
|
|
||||||
Alerts AlertRules
|
Alerts []*Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteAlertCommand struct {
|
type DeleteAlertCommand struct {
|
||||||
@@ -102,17 +94,17 @@ type GetAlertsQuery struct {
|
|||||||
DashboardId int64
|
DashboardId int64
|
||||||
PanelId int64
|
PanelId int64
|
||||||
|
|
||||||
Result []*AlertRuleModel
|
Result []*Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetAllAlertsQuery struct {
|
type GetAllAlertsQuery struct {
|
||||||
Result []*AlertRuleModel
|
Result []*Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetAlertByIdQuery struct {
|
type GetAlertByIdQuery struct {
|
||||||
Id int64
|
Id int64
|
||||||
|
|
||||||
Result *AlertRuleModel
|
Result *Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetAlertChangesQuery struct {
|
type GetAlertChangesQuery struct {
|
||||||
@@ -120,5 +112,5 @@ type GetAlertChangesQuery struct {
|
|||||||
Limit int64
|
Limit int64
|
||||||
SinceId int64
|
SinceId int64
|
||||||
|
|
||||||
Result []*AlertRuleChange
|
Result []*AlertChange
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ type UpdateAlertStateCommand struct {
|
|||||||
NewState string `json:"newState" binding:"Required"`
|
NewState string `json:"newState" binding:"Required"`
|
||||||
Info string `json:"info"`
|
Info string `json:"info"`
|
||||||
|
|
||||||
Result *AlertRuleModel
|
Result *Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queries
|
// Queries
|
||||||
@@ -13,13 +13,13 @@ func TestAlertingModelTest(t *testing.T) {
|
|||||||
json1, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
|
json1, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
|
||||||
json2, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
|
json2, _ := simplejson.NewJson([]byte(`{ "field": "value" }`))
|
||||||
|
|
||||||
rule1 := &AlertRuleModel{
|
rule1 := &Alert{
|
||||||
Expression: json1,
|
Expression: json1,
|
||||||
Name: "Namn",
|
Name: "Namn",
|
||||||
Description: "Description",
|
Description: "Description",
|
||||||
}
|
}
|
||||||
|
|
||||||
rule2 := &AlertRuleModel{
|
rule2 := &Alert{
|
||||||
Expression: json2,
|
Expression: json2,
|
||||||
Name: "Namn",
|
Name: "Namn",
|
||||||
Description: "Description",
|
Description: "Description",
|
||||||
@@ -25,7 +25,7 @@ type AlertRule struct {
|
|||||||
Transformer Transformer
|
Transformer Transformer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAlertRuleFromDBModel(ruleDef *m.AlertRuleModel) (*AlertRule, error) {
|
func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) {
|
||||||
model := &AlertRule{}
|
model := &AlertRule{}
|
||||||
model.Id = ruleDef.Id
|
model.Id = ruleDef.Id
|
||||||
model.OrgId = ruleDef.OrgId
|
model.OrgId = ruleDef.OrgId
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func updateDashboardAlerts(cmd *UpdateDashboardAlertsCommand) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConvetAlertModelToAlertRule(ruleDef *m.AlertRuleModel) (*AlertRule, error) {
|
func ConvetAlertModelToAlertRule(ruleDef *m.Alert) (*AlertRule, error) {
|
||||||
model := &AlertRule{}
|
model := &AlertRule{}
|
||||||
model.Id = ruleDef.Id
|
model.Id = ruleDef.Id
|
||||||
model.OrgId = ruleDef.OrgId
|
model.OrgId = ruleDef.OrgId
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func (e *AlertRuleExtractor) GetRuleModels() (m.AlertRules, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleModel := &m.AlertRuleModel{
|
ruleModel := &m.Alert{
|
||||||
DashboardId: e.Dash.Id,
|
DashboardId: e.Dash.Id,
|
||||||
OrgId: e.OrgId,
|
OrgId: e.OrgId,
|
||||||
PanelId: panel.Get("id").MustInt64(),
|
PanelId: panel.Get("id").MustInt64(),
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func HeartBeat(query *m.HeartBeatCommand) error {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
||||||
alert := m.AlertRuleModel{}
|
alert := m.Alert{}
|
||||||
has, err := x.Id(query.Id).Get(&alert)
|
has, err := x.Id(query.Id).Get(&alert)
|
||||||
if !has {
|
if !has {
|
||||||
return fmt.Errorf("could not find alert")
|
return fmt.Errorf("could not find alert")
|
||||||
@@ -78,7 +78,7 @@ func GetAlertById(query *m.GetAlertByIdQuery) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
|
func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
|
||||||
var alerts []*m.AlertRuleModel
|
var alerts []*m.Alert
|
||||||
err := x.Sql("select * from alert_rule").Find(&alerts)
|
err := x.Sql("select * from alert_rule").Find(&alerts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -131,7 +131,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error {
|
|||||||
sql.WriteString(")")
|
sql.WriteString(")")
|
||||||
}
|
}
|
||||||
|
|
||||||
alerts := make([]*m.AlertRuleModel, 0)
|
alerts := make([]*m.Alert, 0)
|
||||||
if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil {
|
if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -141,7 +141,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) 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)
|
sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
||||||
|
|
||||||
for _, alert := range 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 {
|
for _, alert := range posted {
|
||||||
update := false
|
update := false
|
||||||
var alertToUpdate *m.AlertRuleModel
|
var alertToUpdate *m.Alert
|
||||||
|
|
||||||
for _, k := range alerts {
|
for _, k := range alerts {
|
||||||
if alert.PanelId == k.PanelId {
|
if alert.PanelId == k.PanelId {
|
||||||
@@ -212,7 +212,7 @@ func upsertAlerts(alerts []*m.AlertRuleModel, posted []*m.AlertRuleModel, sess *
|
|||||||
return nil
|
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 {
|
for _, missingAlert := range alerts {
|
||||||
missing := true
|
missing := true
|
||||||
|
|
||||||
@@ -238,12 +238,12 @@ func deleteMissingAlerts(alerts []*m.AlertRuleModel, posted []*m.AlertRuleModel,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.AlertRuleModel, error) {
|
func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.Alert, error) {
|
||||||
alerts := make([]*m.AlertRuleModel, 0)
|
alerts := make([]*m.Alert, 0)
|
||||||
err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []*m.AlertRuleModel{}, err
|
return []*m.Alert{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return alerts, nil
|
return alerts, nil
|
||||||
@@ -48,7 +48,7 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
|
|||||||
return nil
|
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{
|
_, err := sess.Insert(&m.AlertRuleChange{
|
||||||
OrgId: alert.OrgId,
|
OrgId: alert.OrgId,
|
||||||
Type: change,
|
Type: change,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
Convey("When dashboard is removed", func() {
|
Convey("When dashboard is removed", func() {
|
||||||
items := []*m.AlertRuleModel{
|
items := []*m.Alert{
|
||||||
{
|
{
|
||||||
PanelId: 1,
|
PanelId: 1,
|
||||||
DashboardId: testDash.Id,
|
DashboardId: testDash.Id,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
func TestAlertRuleModelParsing(t *testing.T) {
|
func TestAlertRuleModelParsing(t *testing.T) {
|
||||||
|
|
||||||
Convey("Parsing alertRule from expression", t, func() {
|
Convey("Parsing alertRule from expression", t, func() {
|
||||||
alertRuleDAO := &m.AlertRuleModel{}
|
alertRuleDAO := &m.Alert{}
|
||||||
json, _ := simplejson.NewJson([]byte(`
|
json, _ := simplejson.NewJson([]byte(`
|
||||||
{
|
{
|
||||||
"frequency": 10,
|
"frequency": 10,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
|||||||
|
|
||||||
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
|
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
|
||||||
|
|
||||||
items := []*m.AlertRuleModel{
|
items := []*m.Alert{
|
||||||
{
|
{
|
||||||
PanelId: 1,
|
PanelId: 1,
|
||||||
DashboardId: testDash.Id,
|
DashboardId: testDash.Id,
|
||||||
@@ -96,7 +96,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("Multiple alerts per dashboard", func() {
|
Convey("Multiple alerts per dashboard", func() {
|
||||||
multipleItems := []*m.AlertRuleModel{
|
multipleItems := []*m.Alert{
|
||||||
{
|
{
|
||||||
DashboardId: testDash.Id,
|
DashboardId: testDash.Id,
|
||||||
PanelId: 1,
|
PanelId: 1,
|
||||||
@@ -161,7 +161,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("When dashboard is removed", func() {
|
Convey("When dashboard is removed", func() {
|
||||||
items := []*m.AlertRuleModel{
|
items := []*m.Alert{
|
||||||
{
|
{
|
||||||
PanelId: 1,
|
PanelId: 1,
|
||||||
DashboardId: testDash.Id,
|
DashboardId: testDash.Id,
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ package sqlstore
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -19,7 +20,7 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
|
|||||||
return fmt.Errorf("new state is invalid")
|
return fmt.Errorf("new state is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
alert := m.AlertRuleModel{}
|
alert := m.Alert{}
|
||||||
has, err := sess.Id(cmd.AlertId).Get(&alert)
|
has, err := sess.Id(cmd.AlertId).Get(&alert)
|
||||||
if !has {
|
if !has {
|
||||||
return fmt.Errorf("Could not find alert")
|
return fmt.Errorf("Could not find alert")
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func TestAlertingStateAccess(t *testing.T) {
|
|||||||
|
|
||||||
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
|
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
|
||||||
|
|
||||||
items := []*m.AlertRuleModel{
|
items := []*m.Alert{
|
||||||
{
|
{
|
||||||
PanelId: 1,
|
PanelId: 1,
|
||||||
DashboardId: testDash.Id,
|
DashboardId: testDash.Id,
|
||||||
|
|||||||
Reference in New Issue
Block a user