mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): rename alertrule model to alertruleDAO
This commit is contained in:
@@ -64,7 +64,7 @@ func HeartBeat(query *m.HeartBeatCommand) error {
|
||||
*/
|
||||
|
||||
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
||||
alert := m.AlertRule{}
|
||||
alert := m.AlertRuleDAO{}
|
||||
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.AlertRule
|
||||
var alerts []*m.AlertRuleDAO
|
||||
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.AlertRule, 0)
|
||||
alerts := make([]*m.AlertRuleDAO, 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.AlertRule, 0)
|
||||
alerts := make([]*m.AlertRuleDAO, 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.AlertRule, posted []*m.AlertRule, sess *xorm.Session) error {
|
||||
func upsertAlerts(alerts []*m.AlertRuleDAO, posted []*m.AlertRuleDAO, sess *xorm.Session) error {
|
||||
for _, alert := range posted {
|
||||
update := false
|
||||
var alertToUpdate *m.AlertRule
|
||||
var alertToUpdate *m.AlertRuleDAO
|
||||
|
||||
for _, k := range alerts {
|
||||
if alert.PanelId == k.PanelId {
|
||||
@@ -212,7 +212,7 @@ func upsertAlerts(alerts []*m.AlertRule, posted []*m.AlertRule, sess *xorm.Sessi
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteMissingAlerts(alerts []*m.AlertRule, posted []*m.AlertRule, sess *xorm.Session) error {
|
||||
func deleteMissingAlerts(alerts []*m.AlertRuleDAO, posted []*m.AlertRuleDAO, sess *xorm.Session) error {
|
||||
for _, missingAlert := range alerts {
|
||||
missing := true
|
||||
|
||||
@@ -238,12 +238,12 @@ func deleteMissingAlerts(alerts []*m.AlertRule, posted []*m.AlertRule, sess *xor
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.AlertRule, error) {
|
||||
alerts := make([]*m.AlertRule, 0)
|
||||
func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.AlertRuleDAO, error) {
|
||||
alerts := make([]*m.AlertRuleDAO, 0)
|
||||
err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
||||
|
||||
if err != nil {
|
||||
return []*m.AlertRule{}, err
|
||||
return []*m.AlertRuleDAO{}, err
|
||||
}
|
||||
|
||||
return alerts, nil
|
||||
|
||||
@@ -48,7 +48,7 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveAlertChange(change string, alert *m.AlertRule, sess *xorm.Session) error {
|
||||
func SaveAlertChange(change string, alert *m.AlertRuleDAO, sess *xorm.Session) error {
|
||||
_, err := sess.Insert(&m.AlertRuleChange{
|
||||
OrgId: alert.OrgId,
|
||||
Type: change,
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
var err error
|
||||
|
||||
Convey("When dashboard is removed", func() {
|
||||
items := []*m.AlertRule{
|
||||
items := []*m.AlertRuleDAO{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
|
||||
76
pkg/services/sqlstore/alert_rule_parser_test.go
Normal file
76
pkg/services/sqlstore/alert_rule_parser_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestAlertRuleModelParsing(t *testing.T) {
|
||||
|
||||
Convey("Parsing alertRule from expression", t, func() {
|
||||
alertRuleDAO := &m.AlertRuleDAO{}
|
||||
json, _ := simplejson.NewJson([]byte(`
|
||||
{
|
||||
"critical": {
|
||||
"level": 20,
|
||||
"op": ">"
|
||||
},
|
||||
"description": "Alerting Panel Title alert",
|
||||
"evalQuery": {
|
||||
"agg": "avg",
|
||||
"from": "5m",
|
||||
"params": [
|
||||
"#A",
|
||||
"5m",
|
||||
"now",
|
||||
"avg"
|
||||
],
|
||||
"queryRefId": "A",
|
||||
"to": "now"
|
||||
},
|
||||
"evalStringParam1": "",
|
||||
"frequency": 10,
|
||||
"function": "static",
|
||||
"name": "Alerting Panel Title alert",
|
||||
"queryRef": "- select query -",
|
||||
"valueQuery": {
|
||||
"agg": "avg",
|
||||
"datasourceId": 1,
|
||||
"from": "5m",
|
||||
"params": [
|
||||
"#A",
|
||||
"5m",
|
||||
"now",
|
||||
"avg"
|
||||
],
|
||||
"query": "aliasByNode(statsd.fakesite.counters.session_start.*.count, 4)",
|
||||
"queryRefId": "A",
|
||||
"to": "now"
|
||||
},
|
||||
"warning": {
|
||||
"level": 10,
|
||||
"op": ">"
|
||||
}
|
||||
}`))
|
||||
|
||||
alertRuleDAO.Name = "Test"
|
||||
alertRuleDAO.Expression = json
|
||||
rule, _ := alerting.ParseAlertRulesFromAlertModel(alertRuleDAO)
|
||||
|
||||
Convey("Confirm that all properties are set", func() {
|
||||
So(rule.ValueQuery.Query, ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.*.count, 4)")
|
||||
So(rule.ValueQuery.From, ShouldEqual, "5m")
|
||||
So(rule.ValueQuery.To, ShouldEqual, "now")
|
||||
So(rule.ValueQuery.DatasourceId, ShouldEqual, 1)
|
||||
So(rule.ValueQuery.Aggregator, ShouldEqual, "avg")
|
||||
So(rule.Warning.Level, ShouldEqual, 10)
|
||||
So(rule.Warning.Operator, ShouldEqual, ">")
|
||||
So(rule.Critical.Level, ShouldEqual, 20)
|
||||
So(rule.Critical.Operator, ShouldEqual, ">")
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -13,7 +13,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
|
||||
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
|
||||
|
||||
items := []*m.AlertRule{
|
||||
items := []*m.AlertRuleDAO{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
@@ -95,7 +95,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Multiple alerts per dashboard", func() {
|
||||
multipleItems := []*m.AlertRule{
|
||||
multipleItems := []*m.AlertRuleDAO{
|
||||
{
|
||||
DashboardId: testDash.Id,
|
||||
PanelId: 1,
|
||||
@@ -157,7 +157,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("When dashboard is removed", func() {
|
||||
items := []*m.AlertRule{
|
||||
items := []*m.AlertRuleDAO{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
|
||||
@@ -19,7 +19,7 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
|
||||
return fmt.Errorf("new state is invalid")
|
||||
}
|
||||
|
||||
alert := m.AlertRule{}
|
||||
alert := m.AlertRuleDAO{}
|
||||
has, err := sess.Id(cmd.AlertId).Get(&alert)
|
||||
if !has {
|
||||
return fmt.Errorf("Could not find alert")
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestAlertingStateAccess(t *testing.T) {
|
||||
|
||||
testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
|
||||
|
||||
items := []*m.AlertRule{
|
||||
items := []*m.AlertRuleDAO{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
|
||||
@@ -9,34 +9,25 @@ import (
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestAlertModel(t *testing.T) {
|
||||
func TestAlertModelParsing(t *testing.T) {
|
||||
|
||||
Convey("Parsing alerts from dashboard", t, func() {
|
||||
json := `{
|
||||
Convey("Parsing alert info from json", t, func() {
|
||||
Convey("Parsing and validating alerts from dashboards", func() {
|
||||
json := `{
|
||||
"id": 57,
|
||||
"title": "Graphite 4",
|
||||
"originalTitle": "Graphite 4",
|
||||
"tags": [
|
||||
"graphite"
|
||||
],
|
||||
"style": "dark",
|
||||
"timezone": "browser",
|
||||
"editable": true,
|
||||
"hideControls": false,
|
||||
"sharedCrosshair": false,
|
||||
"rows": [
|
||||
{
|
||||
"collapse": false,
|
||||
"editable": true,
|
||||
"height": "250px",
|
||||
|
||||
"panels": [
|
||||
{
|
||||
"title": "Active desktop users",
|
||||
"error": false,
|
||||
"span": 6,
|
||||
"editable": true,
|
||||
"type": "graph",
|
||||
"isNew": true,
|
||||
"id": 3,
|
||||
"targets": [
|
||||
{
|
||||
@@ -45,65 +36,9 @@ func TestAlertModel(t *testing.T) {
|
||||
}
|
||||
],
|
||||
"datasource": null,
|
||||
"renderer": "flot",
|
||||
"yaxes": [
|
||||
{
|
||||
"label": null,
|
||||
"show": true,
|
||||
"logBase": 1,
|
||||
"min": null,
|
||||
"max": null,
|
||||
"format": "short"
|
||||
},
|
||||
{
|
||||
"label": null,
|
||||
"show": true,
|
||||
"logBase": 1,
|
||||
"min": null,
|
||||
"max": null,
|
||||
"format": "short"
|
||||
}
|
||||
],
|
||||
"xaxis": {
|
||||
"show": true
|
||||
},
|
||||
"grid": {
|
||||
"threshold1": null,
|
||||
"threshold2": null,
|
||||
"threshold1Color": "rgba(216, 200, 27, 0.27)",
|
||||
"threshold2Color": "rgba(234, 112, 112, 0.22)"
|
||||
},
|
||||
"lines": true,
|
||||
"fill": 1,
|
||||
"linewidth": 2,
|
||||
"points": false,
|
||||
"pointradius": 5,
|
||||
"bars": false,
|
||||
"stack": false,
|
||||
"percentage": false,
|
||||
"legend": {
|
||||
"show": true,
|
||||
"values": false,
|
||||
"min": false,
|
||||
"max": false,
|
||||
"current": false,
|
||||
"total": false,
|
||||
"avg": false
|
||||
},
|
||||
"nullPointMode": "connected",
|
||||
"steppedLine": false,
|
||||
"tooltip": {
|
||||
"value_type": "cumulative",
|
||||
"shared": true,
|
||||
"msResolution": false
|
||||
},
|
||||
"timeFrom": null,
|
||||
"timeShift": null,
|
||||
"aliasColors": {},
|
||||
"seriesOverrides": [],
|
||||
|
||||
|
||||
"alerting": {
|
||||
"name": "alert name",
|
||||
"description": "description",
|
||||
"frequency": 10,
|
||||
"warning": {
|
||||
"op": ">",
|
||||
@@ -140,16 +75,10 @@ func TestAlertModel(t *testing.T) {
|
||||
},
|
||||
"evalStringParam1": "",
|
||||
"name": "Alerting Panel Title alert"
|
||||
},
|
||||
"links": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Active mobile users",
|
||||
"error": false,
|
||||
"span": 6,
|
||||
"editable": true,
|
||||
"type": "graph",
|
||||
"isNew": true,
|
||||
"id": 4,
|
||||
"targets": [
|
||||
{
|
||||
@@ -158,65 +87,9 @@ func TestAlertModel(t *testing.T) {
|
||||
}
|
||||
],
|
||||
"datasource": "graphite2",
|
||||
"renderer": "flot",
|
||||
"yaxes": [
|
||||
{
|
||||
"label": null,
|
||||
"show": true,
|
||||
"logBase": 1,
|
||||
"min": null,
|
||||
"max": null,
|
||||
"format": "short"
|
||||
},
|
||||
{
|
||||
"label": null,
|
||||
"show": true,
|
||||
"logBase": 1,
|
||||
"min": null,
|
||||
"max": null,
|
||||
"format": "short"
|
||||
}
|
||||
],
|
||||
"xaxis": {
|
||||
"show": true
|
||||
},
|
||||
"grid": {
|
||||
"threshold1": null,
|
||||
"threshold2": null,
|
||||
"threshold1Color": "rgba(216, 200, 27, 0.27)",
|
||||
"threshold2Color": "rgba(234, 112, 112, 0.22)"
|
||||
},
|
||||
"lines": true,
|
||||
"fill": 1,
|
||||
"linewidth": 2,
|
||||
"points": false,
|
||||
"pointradius": 5,
|
||||
"bars": false,
|
||||
"stack": false,
|
||||
"percentage": false,
|
||||
"legend": {
|
||||
"show": true,
|
||||
"values": false,
|
||||
"min": false,
|
||||
"max": false,
|
||||
"current": false,
|
||||
"total": false,
|
||||
"avg": false
|
||||
},
|
||||
"nullPointMode": "connected",
|
||||
"steppedLine": false,
|
||||
"tooltip": {
|
||||
"value_type": "cumulative",
|
||||
"shared": true,
|
||||
"msResolution": false
|
||||
},
|
||||
"timeFrom": null,
|
||||
"timeShift": null,
|
||||
"aliasColors": {
|
||||
"mobile": "#EAB839"
|
||||
},
|
||||
"seriesOverrides": [],
|
||||
"alerting": {
|
||||
"name": "alert name",
|
||||
"description": "description",
|
||||
"frequency": 10,
|
||||
"warning": {
|
||||
"op": ">",
|
||||
@@ -253,8 +126,7 @@ func TestAlertModel(t *testing.T) {
|
||||
},
|
||||
"evalStringParam1": "",
|
||||
"name": "Alerting Panel Title alert"
|
||||
},
|
||||
"links": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"title": "Row"
|
||||
@@ -265,41 +137,8 @@ func TestAlertModel(t *testing.T) {
|
||||
"height": "250px",
|
||||
"panels": [
|
||||
{
|
||||
"columns": [],
|
||||
"datasource": "InfluxDB",
|
||||
"editable": true,
|
||||
"error": false,
|
||||
"fontSize": "100%",
|
||||
"id": 2,
|
||||
"isNew": true,
|
||||
"pageSize": null,
|
||||
"scroll": true,
|
||||
"showHeader": true,
|
||||
"sort": {
|
||||
"col": 0,
|
||||
"desc": true
|
||||
},
|
||||
"span": 6,
|
||||
"styles": [
|
||||
{
|
||||
"dateFormat": "YYYY-MM-DD HH:mm:ss",
|
||||
"pattern": "Time",
|
||||
"type": "date"
|
||||
},
|
||||
{
|
||||
"colorMode": null,
|
||||
"colors": [
|
||||
"rgba(245, 54, 54, 0.9)",
|
||||
"rgba(237, 129, 40, 0.89)",
|
||||
"rgba(50, 172, 45, 0.97)"
|
||||
],
|
||||
"decimals": 2,
|
||||
"pattern": "/.*/",
|
||||
"thresholds": [],
|
||||
"type": "number",
|
||||
"unit": "short"
|
||||
}
|
||||
],
|
||||
"targets": [
|
||||
{
|
||||
"dsType": "influxdb",
|
||||
@@ -342,104 +181,60 @@ func TestAlertModel(t *testing.T) {
|
||||
],
|
||||
"title": "Broken influxdb panel",
|
||||
"transform": "table",
|
||||
"type": "table",
|
||||
"links": []
|
||||
"type": "table"
|
||||
}
|
||||
],
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"time": {
|
||||
"from": "now-1h",
|
||||
"to": "now"
|
||||
},
|
||||
"timepicker": {
|
||||
"now": true,
|
||||
"nowDelay": "5m",
|
||||
"refresh_intervals": [
|
||||
"5s",
|
||||
"10s",
|
||||
"30s",
|
||||
"1m",
|
||||
"5m",
|
||||
"15m",
|
||||
"30m",
|
||||
"1h",
|
||||
"2h",
|
||||
"1d",
|
||||
"7d"
|
||||
],
|
||||
"time_options": [
|
||||
"5m",
|
||||
"15m",
|
||||
"1h",
|
||||
"6h",
|
||||
"12h",
|
||||
"24h",
|
||||
"2d",
|
||||
"7d",
|
||||
"30d"
|
||||
]
|
||||
},
|
||||
"templating": {
|
||||
"list": []
|
||||
},
|
||||
"annotations": {
|
||||
"list": []
|
||||
},
|
||||
"schemaVersion": 12,
|
||||
"version": 16,
|
||||
"links": []
|
||||
]
|
||||
|
||||
}`
|
||||
dashboardJson, _ := simplejson.NewJson([]byte(json))
|
||||
cmd := &m.SaveDashboardCommand{
|
||||
Dashboard: dashboardJson,
|
||||
UserId: 1,
|
||||
OrgId: 1,
|
||||
Overwrite: true,
|
||||
Result: &m.Dashboard{
|
||||
Id: 1,
|
||||
},
|
||||
}
|
||||
|
||||
InitTestDB(t)
|
||||
|
||||
AddDataSource(&m.AddDataSourceCommand{
|
||||
Name: "graphite2",
|
||||
OrgId: 1,
|
||||
Type: m.DS_INFLUXDB,
|
||||
Access: m.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
IsDefault: false,
|
||||
Database: "site",
|
||||
})
|
||||
|
||||
AddDataSource(&m.AddDataSourceCommand{
|
||||
Name: "InfluxDB",
|
||||
OrgId: 1,
|
||||
Type: m.DS_GRAPHITE,
|
||||
Access: m.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
IsDefault: true,
|
||||
})
|
||||
|
||||
alerts := alerting.ParseAlertsFromDashboard(cmd)
|
||||
|
||||
Convey("all properties have been set", func() {
|
||||
So(alerts, ShouldNotBeEmpty)
|
||||
So(len(alerts), ShouldEqual, 2)
|
||||
|
||||
for _, v := range alerts {
|
||||
So(v.DashboardId, ShouldEqual, 1)
|
||||
So(v.PanelId, ShouldNotEqual, 0)
|
||||
|
||||
So(v.Name, ShouldNotBeEmpty)
|
||||
So(v.Description, ShouldNotBeEmpty)
|
||||
|
||||
expr := simplejson.NewFromAny(v.Expression)
|
||||
So(expr.Get("valueQuery").Get("query").MustString(), ShouldNotEqual, "")
|
||||
So(expr.Get("valueQuery").Get("datsourceId").MustInt64(), ShouldNotEqual, 0)
|
||||
dashboardJSON, _ := simplejson.NewJson([]byte(json))
|
||||
cmd := &m.SaveDashboardCommand{
|
||||
Dashboard: dashboardJSON,
|
||||
UserId: 1,
|
||||
OrgId: 1,
|
||||
Overwrite: true,
|
||||
Result: &m.Dashboard{
|
||||
Id: 1,
|
||||
},
|
||||
}
|
||||
|
||||
InitTestDB(t)
|
||||
|
||||
AddDataSource(&m.AddDataSourceCommand{
|
||||
Name: "graphite2",
|
||||
OrgId: 1,
|
||||
Type: m.DS_INFLUXDB,
|
||||
Access: m.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
IsDefault: false,
|
||||
Database: "site",
|
||||
})
|
||||
|
||||
AddDataSource(&m.AddDataSourceCommand{
|
||||
Name: "InfluxDB",
|
||||
OrgId: 1,
|
||||
Type: m.DS_GRAPHITE,
|
||||
Access: m.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
IsDefault: true,
|
||||
})
|
||||
|
||||
alerts := alerting.ParseAlertsFromDashboard(cmd)
|
||||
|
||||
Convey("all properties have been set", func() {
|
||||
So(alerts, ShouldNotBeEmpty)
|
||||
So(len(alerts), ShouldEqual, 2)
|
||||
|
||||
for _, v := range alerts {
|
||||
So(v.DashboardId, ShouldEqual, 1)
|
||||
So(v.PanelId, ShouldNotEqual, 0)
|
||||
|
||||
So(v.Name, ShouldNotBeEmpty)
|
||||
So(v.Description, ShouldNotBeEmpty)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user