| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package influxdb
- import (
- "testing"
- "github.com/grafana/grafana/pkg/components/simplejson"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestInfluxdbQueryParser(t *testing.T) {
- Convey("Influxdb query parser", t, func() {
- parser := &InfluxdbQueryParser{}
- Convey("can parse influxdb json model", func() {
- json := `
- {
- "dsType": "influxdb",
- "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"
- ]
- }
- ]
- ],
- "tags": [
- {
- "key": "datacenter",
- "operator": "=",
- "value": "America"
- },
- {
- "condition": "OR",
- "key": "hostname",
- "operator": "=",
- "value": "server1"
- }
- ]
- }
- `
- modelJson, err := simplejson.NewJson([]byte(json))
- So(err, ShouldBeNil)
- res, err := parser.Parse(modelJson)
- So(err, ShouldBeNil)
- So(len(res.GroupBy), ShouldEqual, 3)
- 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)
- })
- })
- }
|