Commit 8cd1d179 by bergquist

feat(alerting): new alerting model\

parent 0c69c5af
...@@ -30,8 +30,8 @@ func ParseAlertsFromDashboard(cmd *m.SaveDashboardCommand) []*m.AlertRuleDAO { ...@@ -30,8 +30,8 @@ func ParseAlertsFromDashboard(cmd *m.SaveDashboardCommand) []*m.AlertRuleDAO {
log.Info("Alertrule: %v", alert.Name) log.Info("Alertrule: %v", alert.Name)
valueQuery := alerting.Get("valueQuery") valueQuery := alerting.Get("query")
valueQueryRef := valueQuery.Get("queryRefId").MustString() valueQueryRef := valueQuery.Get("refId").MustString()
for _, targetsObj := range panel.Get("targets").MustArray() { for _, targetsObj := range panel.Get("targets").MustArray() {
target := simplejson.NewFromAny(targetsObj) target := simplejson.NewFromAny(targetsObj)
...@@ -48,7 +48,7 @@ func ParseAlertsFromDashboard(cmd *m.SaveDashboardCommand) []*m.AlertRuleDAO { ...@@ -48,7 +48,7 @@ func ParseAlertsFromDashboard(cmd *m.SaveDashboardCommand) []*m.AlertRuleDAO {
if err := bus.Dispatch(query); err == nil { if err := bus.Dispatch(query); err == nil {
for _, ds := range query.Result { for _, ds := range query.Result {
if ds.IsDefault { if ds.IsDefault {
alerting.SetPath([]string{"valueQuery", "datasourceId"}, ds.Id) alerting.SetPath([]string{"query", "datasourceId"}, ds.Id)
} }
} }
} }
...@@ -58,12 +58,12 @@ func ParseAlertsFromDashboard(cmd *m.SaveDashboardCommand) []*m.AlertRuleDAO { ...@@ -58,12 +58,12 @@ func ParseAlertsFromDashboard(cmd *m.SaveDashboardCommand) []*m.AlertRuleDAO {
OrgId: cmd.OrgId, OrgId: cmd.OrgId,
} }
bus.Dispatch(query) bus.Dispatch(query)
alerting.SetPath([]string{"valueQuery", "datasourceId"}, query.Result.Id) alerting.SetPath([]string{"query", "datasourceId"}, query.Result.Id)
} }
targetQuery := target.Get("target").MustString() targetQuery := target.Get("target").MustString()
if targetQuery != "" { if targetQuery != "" {
alerting.SetPath([]string{"valueQuery", "query"}, targetQuery) alerting.SetPath([]string{"query", "query"}, targetQuery)
} }
} }
} }
...@@ -105,23 +105,24 @@ func ParseAlertRulesFromAlertModel(ruleDef *m.AlertRuleDAO) (*AlertRule, error) ...@@ -105,23 +105,24 @@ func ParseAlertRulesFromAlertModel(ruleDef *m.AlertRuleDAO) (*AlertRule, error)
} }
model.Frequency = ruleDef.Expression.Get("frequency").MustInt64() model.Frequency = ruleDef.Expression.Get("frequency").MustInt64()
model.Transform = ruleDef.Expression.Get("transform").Get("type").MustString()
valueQuery := ruleDef.Expression.Get("valueQuery") model.TransformParams = *ruleDef.Expression.Get("transform")
model.ValueQuery = AlertQuery{ query := ruleDef.Expression.Get("query")
Query: valueQuery.Get("query").MustString(), model.Query = AlertQuery{
DatasourceId: valueQuery.Get("datasourceId").MustInt64(), Query: query.Get("query").MustString(),
From: valueQuery.Get("from").MustString(), DatasourceId: query.Get("datasourceId").MustInt64(),
To: valueQuery.Get("to").MustString(), From: query.Get("from").MustString(),
Aggregator: valueQuery.Get("agg").MustString(), To: query.Get("to").MustString(),
Aggregator: query.Get("agg").MustString(),
} }
if model.ValueQuery.Query == "" { if model.Query.Query == "" {
return nil, fmt.Errorf("missing valueQuery query") return nil, fmt.Errorf("missing query.query")
} }
if model.ValueQuery.DatasourceId == 0 { if model.Query.DatasourceId == 0 {
return nil, fmt.Errorf("missing valueQuery datasourceId") return nil, fmt.Errorf("missing query.datasourceId")
} }
return model, nil return model, nil
......
...@@ -100,7 +100,7 @@ func (e *ExecutorImpl) Execute(job *AlertJob, resultQueue chan *AlertResult) { ...@@ -100,7 +100,7 @@ func (e *ExecutorImpl) Execute(job *AlertJob, resultQueue chan *AlertResult) {
func (e *ExecutorImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error) { func (e *ExecutorImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error) {
getDsInfo := &m.GetDataSourceByIdQuery{ getDsInfo := &m.GetDataSourceByIdQuery{
Id: job.Rule.ValueQuery.DatasourceId, Id: job.Rule.Query.DatasourceId,
OrgId: job.Rule.OrgId, OrgId: job.Rule.OrgId,
} }
...@@ -128,16 +128,16 @@ func (e *ExecutorImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error) ...@@ -128,16 +128,16 @@ func (e *ExecutorImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error)
} }
func (e *ExecutorImpl) GetRequestForAlertRule(rule *AlertRule, datasource *m.DataSource) *tsdb.Request { func (e *ExecutorImpl) GetRequestForAlertRule(rule *AlertRule, datasource *m.DataSource) *tsdb.Request {
log.Debug2("GetRequest", "query", rule.ValueQuery.Query, "from", rule.ValueQuery.From, "datasourceId", datasource.Id) log.Debug2("GetRequest", "query", rule.Query.Query, "from", rule.Query.From, "datasourceId", datasource.Id)
req := &tsdb.Request{ req := &tsdb.Request{
TimeRange: tsdb.TimeRange{ TimeRange: tsdb.TimeRange{
From: "-" + rule.ValueQuery.From, From: "-" + rule.Query.From,
To: rule.ValueQuery.To, To: rule.Query.To,
}, },
Queries: []*tsdb.Query{ Queries: []*tsdb.Query{
{ {
RefId: "A", RefId: "A",
Query: rule.ValueQuery.Query, Query: rule.Query.Query,
DataSource: &tsdb.DataSourceInfo{ DataSource: &tsdb.DataSourceInfo{
Id: datasource.Id, Id: datasource.Id,
Name: datasource.Name, Name: datasource.Name,
......
package alerting package alerting
import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb"
)
type AlertJob struct { type AlertJob struct {
Offset int64 Offset int64
Delay bool Delay bool
...@@ -18,26 +23,23 @@ type AlertResult struct { ...@@ -18,26 +23,23 @@ type AlertResult struct {
} }
type AlertRule struct { type AlertRule struct {
Id int64 Id int64
OrgId int64 OrgId int64
DashboardId int64 DashboardId int64
PanelId int64 PanelId int64
//WarnLevel float64 Frequency int64
//CritLevel float64 Name string
//WarnOperator string Description string
//CritOperator string State string
Frequency int64 Warning Level
Name string Critical Level
Description string Query AlertQuery
State string Transform string
TransformParams simplejson.Json
Warning Level }
Critical Level
ValueQuery AlertQuery type Transformer interface {
EvalFunc string Transform(tsdb tsdb.TimeSeriesSlice) float64
EvalQuery AlertQuery
EvalParam string
} }
type Level struct { type Level struct {
......
...@@ -15,58 +15,37 @@ func TestAlertRuleModelParsing(t *testing.T) { ...@@ -15,58 +15,37 @@ func TestAlertRuleModelParsing(t *testing.T) {
alertRuleDAO := &m.AlertRuleDAO{} alertRuleDAO := &m.AlertRuleDAO{}
json, _ := simplejson.NewJson([]byte(` json, _ := simplejson.NewJson([]byte(`
{ {
"frequency": 10,
"warning": {
"op": ">",
"level": 10
},
"critical": { "critical": {
"level": 20, "op": ">",
"op": ">" "level": 20
}, },
"description": "Alerting Panel Title alert", "query": {
"evalQuery": {
"agg": "avg",
"from": "5m",
"params": [
"#A",
"5m",
"now",
"avg"
],
"queryRefId": "A", "queryRefId": "A",
"to": "now"
},
"evalStringParam1": "",
"frequency": 10,
"function": "static",
"name": "Alerting Panel Title alert",
"queryRef": "- select query -",
"valueQuery": {
"agg": "avg",
"datasourceId": 1,
"from": "5m", "from": "5m",
"params": [ "to": "now",
"#A", "datasourceId": 1,
"5m", "query": "aliasByNode(statsd.fakesite.counters.session_start.*.count, 4)"
"now",
"avg"
],
"query": "aliasByNode(statsd.fakesite.counters.session_start.*.count, 4)",
"queryRefId": "A",
"to": "now"
}, },
"warning": { "transform": {
"level": 10, "name": "aggregation",
"op": ">" "method": "avg"
} }`))
}`))
alertRuleDAO.Name = "Test" alertRuleDAO.Name = "Test"
alertRuleDAO.Expression = json alertRuleDAO.Expression = json
rule, _ := alerting.ParseAlertRulesFromAlertModel(alertRuleDAO) rule, _ := alerting.ParseAlertRulesFromAlertModel(alertRuleDAO)
Convey("Confirm that all properties are set", func() { Convey("Confirm that all properties are set", func() {
So(rule.ValueQuery.Query, ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.*.count, 4)") So(rule.Query.Query, ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.*.count, 4)")
So(rule.ValueQuery.From, ShouldEqual, "5m") So(rule.Query.From, ShouldEqual, "5m")
So(rule.ValueQuery.To, ShouldEqual, "now") So(rule.Query.To, ShouldEqual, "now")
So(rule.ValueQuery.DatasourceId, ShouldEqual, 1) So(rule.Query.DatasourceId, ShouldEqual, 1)
So(rule.ValueQuery.Aggregator, ShouldEqual, "avg") //So(rule.ValueQuery.Aggregator, ShouldEqual, "avg")
So(rule.Warning.Level, ShouldEqual, 10) So(rule.Warning.Level, ShouldEqual, 10)
So(rule.Warning.Operator, ShouldEqual, ">") So(rule.Warning.Operator, ShouldEqual, ">")
So(rule.Critical.Level, ShouldEqual, 20) So(rule.Critical.Level, ShouldEqual, 20)
......
...@@ -51,20 +51,15 @@ export class AlertTabCtrl { ...@@ -51,20 +51,15 @@ export class AlertTabCtrl {
frequency: 10, frequency: 10,
warning: { op: '>', level: 10 }, warning: { op: '>', level: 10 },
critical: { op: '>', level: 20 }, critical: { op: '>', level: 20 },
function: 'static', query: {
valueQuery: { refId: 'A',
queryRefId: 'A', from: '5m',
from: '600s',
to: 'now', to: 'now',
agg: 'avg',
}, },
evalQuery: { transform: {
queryRefId: 'A', type: 'aggregation',
from: '600s', method: 'avg'
to: 'now', }
agg: 'avg',
},
evalStringParam1: '',
}; };
/** @ngInject */ /** @ngInject */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment