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
|
|
|
|
2021-09-06 02:33:07 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2020-12-21 00:51:46 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-06 07:16:26 -05:00
|
|
|
)
|
|
|
|
|
2020-12-21 00:51:46 -06:00
|
|
|
func TestInfluxdbQueryParser_Parse(t *testing.T) {
|
|
|
|
parser := &InfluxdbQueryParser{}
|
2016-10-06 07:16:26 -05:00
|
|
|
|
2020-12-21 00:51:46 -06:00
|
|
|
t.Run("can parse influxdb json model", func(t *testing.T) {
|
|
|
|
json := `
|
2016-10-06 07:16:26 -05:00
|
|
|
{
|
|
|
|
"groupBy": [
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"$interval"
|
|
|
|
],
|
|
|
|
"type": "time"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"datacenter"
|
|
|
|
],
|
|
|
|
"type": "tag"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"none"
|
|
|
|
],
|
|
|
|
"type": "fill"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"measurement": "logins.count",
|
2018-12-21 09:38:53 -06:00
|
|
|
"tz": "Europe/Paris",
|
2022-02-09 12:26:16 -06:00
|
|
|
"limit": "1",
|
|
|
|
"slimit": "1",
|
|
|
|
"orderByTime": "ASC",
|
2016-10-06 07:16:26 -05:00
|
|
|
"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"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
2020-06-01 10:11:25 -05:00
|
|
|
"alias": "series alias",
|
2016-10-06 07:16:26 -05:00
|
|
|
"tags": [
|
|
|
|
{
|
|
|
|
"key": "datacenter",
|
|
|
|
"operator": "=",
|
|
|
|
"value": "America"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"condition": "OR",
|
|
|
|
"key": "hostname",
|
|
|
|
"operator": "=",
|
|
|
|
"value": "server1"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-09-06 02:33:07 -05:00
|
|
|
query := backend.DataQuery{
|
|
|
|
JSON: []byte(json),
|
|
|
|
Interval: time.Second * 20,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := parser.Parse(query)
|
2020-12-21 00:51:46 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, res.GroupBy, 3)
|
|
|
|
require.Len(t, res.Selects, 3)
|
|
|
|
require.Len(t, res.Tags, 2)
|
|
|
|
require.Equal(t, "Europe/Paris", res.Tz)
|
2022-02-09 12:26:16 -06:00
|
|
|
require.Equal(t, "1", res.Limit)
|
|
|
|
require.Equal(t, "1", res.Slimit)
|
|
|
|
require.Equal(t, "ASC", res.OrderByTime)
|
2020-12-21 00:51:46 -06:00
|
|
|
require.Equal(t, time.Second*20, res.Interval)
|
|
|
|
require.Equal(t, "series alias", res.Alias)
|
|
|
|
})
|
2016-10-11 11:29:09 -05:00
|
|
|
|
2020-12-21 00:51:46 -06:00
|
|
|
t.Run("can parse raw query json model", func(t *testing.T) {
|
|
|
|
json := `
|
2016-10-11 11:29:09 -05:00
|
|
|
{
|
|
|
|
"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",
|
2020-06-01 10:11:25 -05:00
|
|
|
"query": "RawDummyQuery",
|
2016-10-11 11:29:09 -05:00
|
|
|
"rawQuery": true,
|
|
|
|
"refId": "A",
|
|
|
|
"resultFormat": "time_series",
|
|
|
|
"select": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"value"
|
|
|
|
],
|
|
|
|
"type": "field"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
|
|
|
|
],
|
|
|
|
"type": "mean"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"tags": [
|
|
|
|
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-09-06 02:33:07 -05:00
|
|
|
query := backend.DataQuery{
|
|
|
|
JSON: []byte(json),
|
|
|
|
Interval: time.Second * 10,
|
|
|
|
}
|
2016-10-11 11:29:09 -05:00
|
|
|
|
2021-09-06 02:33:07 -05:00
|
|
|
res, err := parser.Parse(query)
|
2020-12-21 00:51:46 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "RawDummyQuery", res.RawQuery)
|
|
|
|
require.Len(t, res.GroupBy, 2)
|
|
|
|
require.Len(t, res.Selects, 1)
|
|
|
|
require.Empty(t, res.Tags)
|
|
|
|
require.Equal(t, time.Second*10, res.Interval)
|
2016-10-06 07:16:26 -05:00
|
|
|
})
|
2021-09-07 02:49:34 -05:00
|
|
|
|
|
|
|
t.Run("will enforce a minInterval of 1 millisecond", func(t *testing.T) {
|
|
|
|
json := `
|
|
|
|
{
|
|
|
|
"query": "RawDummyQuery",
|
|
|
|
"rawQuery": true,
|
|
|
|
"resultFormat": "time_series"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
query := backend.DataQuery{
|
|
|
|
JSON: []byte(json),
|
|
|
|
Interval: time.Millisecond * 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := parser.Parse(query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, time.Millisecond*1, res.Interval)
|
|
|
|
})
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|