Commit 885e0366 by bergquist

feat(influxdb): support raw queries in alerting

ref #6231
parent ccee1b3f
......@@ -10,6 +10,7 @@ type InfluxdbQueryParser struct{}
func (qp *InfluxdbQueryParser) Parse(model *simplejson.Json) (*Query, error) {
policy := model.Get("policy").MustString("default")
rawQuery := model.Get("query").MustString("")
measurement, err := model.Get("measurement").String()
if err != nil {
......@@ -43,6 +44,7 @@ func (qp *InfluxdbQueryParser) Parse(model *simplejson.Json) (*Query, error) {
GroupBy: groupBys,
Tags: tags,
Selects: selects,
RawQuery: rawQuery,
}, nil
}
......
......@@ -111,5 +111,61 @@ func TestInfluxdbQueryParser(t *testing.T) {
So(len(res.Selects), ShouldEqual, 3)
So(len(res.Tags), ShouldEqual, 2)
})
Convey("can part raw query json model", func() {
json := `
{
"dsType": "influxdb",
"groupBy": [
{
"params": [
"$interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"policy": "default",
"query": "RawDummieQuery",
"rawQuery": true,
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"value"
],
"type": "field"
},
{
"params": [
],
"type": "mean"
}
]
],
"tags": [
]
}
`
modelJson, err := simplejson.NewJson([]byte(json))
So(err, ShouldBeNil)
res, err := parser.Parse(modelJson)
So(err, ShouldBeNil)
So(res.RawQuery, ShouldEqual, "RawDummieQuery")
So(len(res.GroupBy), ShouldEqual, 2)
So(len(res.Selects), ShouldEqual, 2)
So(len(res.Tags), ShouldEqual, 0)
})
})
}
......@@ -7,6 +7,7 @@ type Query struct {
Tags []*Tag
GroupBy []*QueryPart
Selects []*Select
RawQuery string
Interval string
}
......
......@@ -10,6 +10,15 @@ import (
type QueryBuilder struct{}
func (qb *QueryBuilder) Build(query *Query, queryContext *tsdb.QueryContext) (string, error) {
if query.RawQuery != "" {
q := query.RawQuery
q = strings.Replace(q, "$timeFilter", qb.renderTimeFilter(query, queryContext), 1)
q = strings.Replace(q, "$interval", tsdb.CalculateInterval(queryContext.TimeRange), 1)
return q, nil
}
res := qb.renderSelectors(query, queryContext)
res += qb.renderMeasurement(query)
res += qb.renderWhereClause(query)
......
......@@ -68,5 +68,20 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
So(builder.renderTimeFilter(&query, queryContext), ShouldEqual, "time > now() - 10m")
})
})
Convey("can build query from raw query", func() {
query := &Query{
Selects: []*Select{{*qp1, *qp2}},
Measurement: "cpu",
Policy: "policy",
GroupBy: []*QueryPart{groupBy1, groupBy3},
Interval: "10s",
RawQuery: "Raw query",
}
rawQuery, err := builder.Build(query, queryContext)
So(err, ShouldBeNil)
So(rawQuery, ShouldEqual, `Raw query`)
})
})
}
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