InfluxDB: Support cardinality queries with backend mode (#87264)

support cardinality queries
This commit is contained in:
ismail simsek
2024-05-03 12:50:00 +02:00
committed by GitHub
parent 253ab5efd3
commit 3317691615
7 changed files with 540 additions and 65 deletions
@@ -1,13 +1,14 @@
package querydata
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/stretchr/testify/require"
@@ -19,6 +20,24 @@ const (
testPath = "../testdata"
)
func readJsonFile(filePath string) io.ReadCloser {
bytes, err := os.ReadFile(filepath.Join(testPath, filepath.Clean(filePath)+".json"))
if err != nil {
panic(fmt.Sprintf("cannot read the file: %s", filePath))
}
return io.NopCloser(strings.NewReader(string(bytes)))
}
func generateQuery(query, resFormat, alias string) *models.Query {
return &models.Query{
RawQuery: query,
UseRawQuery: true,
Alias: alias,
ResultFormat: resFormat,
}
}
var testFiles = []string{
"all_values_are_null",
"influx_select_all_from_cpu",
@@ -55,26 +74,34 @@ func TestReadInfluxAsTable(t *testing.T) {
func runScenario(tf string, resultFormat string) func(t *testing.T) {
return func(t *testing.T) {
f, err := os.Open(path.Join(testPath, filepath.Clean(tf+".json")))
require.NoError(t, err)
f := readJsonFile(tf)
var rsp *backend.DataResponse
query := generateQuery("Test raw query", resultFormat, "")
query := &models.Query{
RawQuery: "Test raw query",
UseRawQuery: true,
ResultFormat: resultFormat,
}
rsp = ResponseParse(f, 200, query)
if strings.Contains(tf, "error") {
require.Error(t, rsp.Error)
return
}
require.NoError(t, rsp.Error)
fname := tf + "." + resultFormat + ".golden"
experimental.CheckGoldenJSONResponse(t, testPath, fname, rsp, shouldUpdate)
runQuery(t, f, tf, resultFormat, query)
}
}
func runQuery(t *testing.T, f io.ReadCloser, tf string, rf string, query *models.Query) {
rsp := ResponseParse(f, 200, query)
if strings.Contains(tf, "error") {
require.Error(t, rsp.Error)
return
}
require.NoError(t, rsp.Error)
fname := tf + "." + rf + ".golden"
experimental.CheckGoldenJSONResponse(t, testPath, fname, rsp, shouldUpdate)
}
func TestParsingAsTimeSeriesWithoutTimeColumn(t *testing.T) {
t.Run("cardinality", func(t *testing.T) {
f, err := os.Open(path.Join(testPath, filepath.Clean("cardinality.json")))
require.NoError(t, err)
query := generateQuery(`SHOW TAG VALUES CARDINALITY with key = "host"`, "time_series", "")
runQuery(t, f, "cardinality", "time_series", query)
})
}