2016-10-06 07:16:26 -05:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-11-15 04:22:00 -06:00
|
|
|
"time"
|
2016-10-06 07:16:26 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-12-07 04:10:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-10-06 07:16:26 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInfluxdbQueryParser(t *testing.T) {
|
|
|
|
Convey("Influxdb query parser", t, func() {
|
|
|
|
|
|
|
|
parser := &InfluxdbQueryParser{}
|
2016-12-07 04:10:42 -06:00
|
|
|
dsInfo := &models.DataSource{
|
2016-10-13 04:42:51 -05:00
|
|
|
JsonData: simplejson.New(),
|
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
|
|
|
|
Convey("can parse influxdb json model", func() {
|
|
|
|
json := `
|
|
|
|
{
|
|
|
|
"groupBy": [
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"$interval"
|
|
|
|
],
|
|
|
|
"type": "time"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"datacenter"
|
|
|
|
],
|
|
|
|
"type": "tag"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"none"
|
|
|
|
],
|
|
|
|
"type": "fill"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"measurement": "logins.count",
|
|
|
|
"policy": "default",
|
|
|
|
"refId": "B",
|
|
|
|
"resultFormat": "time_series",
|
|
|
|
"select": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"params": [
|
|
|
|
"value"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "count",
|
|
|
|
"params": []
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"params": [
|
|
|
|
"value"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "bottom",
|
|
|
|
"params": [
|
|
|
|
3
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"params": [
|
|
|
|
"value"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "mean",
|
|
|
|
"params": []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "math",
|
|
|
|
"params": [
|
|
|
|
" / 100"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
2016-11-08 12:22:59 -06:00
|
|
|
"alias": "serie alias",
|
2016-10-06 07:16:26 -05:00
|
|
|
"tags": [
|
|
|
|
{
|
|
|
|
"key": "datacenter",
|
|
|
|
"operator": "=",
|
|
|
|
"value": "America"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "OR",
|
|
|
|
"key": "hostname",
|
|
|
|
"operator": "=",
|
|
|
|
"value": "server1"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2016-10-13 04:42:51 -05:00
|
|
|
dsInfo.JsonData.Set("timeInterval", ">20s")
|
2016-10-06 07:16:26 -05:00
|
|
|
modelJson, err := simplejson.NewJson([]byte(json))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2016-10-13 04:42:51 -05:00
|
|
|
res, err := parser.Parse(modelJson, dsInfo)
|
2016-10-06 07:16:26 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(res.GroupBy), ShouldEqual, 3)
|
|
|
|
So(len(res.Selects), ShouldEqual, 3)
|
|
|
|
So(len(res.Tags), ShouldEqual, 2)
|
2017-11-15 04:22:00 -06:00
|
|
|
So(res.Interval, ShouldEqual, time.Second*20)
|
2016-11-08 12:22:59 -06:00
|
|
|
So(res.Alias, ShouldEqual, "serie alias")
|
2016-10-06 07:16:26 -05:00
|
|
|
})
|
2016-10-11 11:29:09 -05:00
|
|
|
|
|
|
|
Convey("can part raw query json model", func() {
|
|
|
|
json := `
|
|
|
|
{
|
|
|
|
"groupBy": [
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"$interval"
|
|
|
|
],
|
|
|
|
"type": "time"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"null"
|
|
|
|
],
|
|
|
|
"type": "fill"
|
|
|
|
}
|
|
|
|
],
|
2016-10-13 04:42:51 -05:00
|
|
|
"interval": ">10s",
|
2016-10-11 11:29:09 -05:00
|
|
|
"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)
|
|
|
|
|
2016-10-13 04:42:51 -05:00
|
|
|
res, err := parser.Parse(modelJson, dsInfo)
|
2016-10-11 11:29:09 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(res.RawQuery, ShouldEqual, "RawDummieQuery")
|
|
|
|
So(len(res.GroupBy), ShouldEqual, 2)
|
2016-10-12 00:43:30 -05:00
|
|
|
So(len(res.Selects), ShouldEqual, 1)
|
2016-10-11 11:29:09 -05:00
|
|
|
So(len(res.Tags), ShouldEqual, 0)
|
2017-11-15 04:22:00 -06:00
|
|
|
So(res.Interval, ShouldEqual, time.Second*10)
|
2016-10-11 11:29:09 -05:00
|
|
|
})
|
2016-10-06 07:16:26 -05:00
|
|
|
})
|
|
|
|
}
|