mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 04:22:13 +08:00
Expressions: expose ConvertDataFramesToResults (#83805)
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
@ -12,11 +11,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||
"github.com/grafana/grafana/pkg/infra/log/logtest"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type expectedError struct{}
|
||||
@ -169,106 +164,3 @@ func TestCheckIfSeriesNeedToBeFixed(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertDataFramesToResults(t *testing.T) {
|
||||
s := &Service{
|
||||
cfg: setting.NewCfg(),
|
||||
features: &featuremgmt.FeatureManager{},
|
||||
tracer: tracing.InitializeTracerForTest(),
|
||||
metrics: newMetrics(nil),
|
||||
}
|
||||
|
||||
t.Run("should add name label if no labels and specific data source", func(t *testing.T) {
|
||||
supported := []string{datasources.DS_GRAPHITE, datasources.DS_TESTDATA}
|
||||
t.Run("when only field name is specified", func(t *testing.T) {
|
||||
t.Run("use value field names if one frame - many series", func(t *testing.T) {
|
||||
supported := []string{datasources.DS_GRAPHITE, datasources.DS_TESTDATA}
|
||||
|
||||
frames := []*data.Frame{
|
||||
data.NewFrame("test",
|
||||
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
||||
data.NewField("test-value1", nil, []*float64{fp(2)}),
|
||||
data.NewField("test-value2", nil, []*float64{fp(2)})),
|
||||
}
|
||||
|
||||
for _, dtype := range supported {
|
||||
t.Run(dtype, func(t *testing.T) {
|
||||
resultType, res, err := convertDataFramesToResults(context.Background(), frames, dtype, s, &logtest.Fake{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "single frame series", resultType)
|
||||
require.Len(t, res.Values, 2)
|
||||
|
||||
var names []string
|
||||
for _, value := range res.Values {
|
||||
require.IsType(t, mathexp.Series{}, value)
|
||||
lbls := value.GetLabels()
|
||||
require.Contains(t, lbls, nameLabelName)
|
||||
names = append(names, lbls[nameLabelName])
|
||||
}
|
||||
require.EqualValues(t, []string{"test-value1", "test-value2"}, names)
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("should use frame name if one frame - one series", func(t *testing.T) {
|
||||
frames := []*data.Frame{
|
||||
data.NewFrame("test-frame1",
|
||||
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
||||
data.NewField("test-value1", nil, []*float64{fp(2)})),
|
||||
data.NewFrame("test-frame2",
|
||||
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
||||
data.NewField("test-value2", nil, []*float64{fp(2)})),
|
||||
}
|
||||
|
||||
for _, dtype := range supported {
|
||||
t.Run(dtype, func(t *testing.T) {
|
||||
resultType, res, err := convertDataFramesToResults(context.Background(), frames, dtype, s, &logtest.Fake{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "multi frame series", resultType)
|
||||
require.Len(t, res.Values, 2)
|
||||
|
||||
var names []string
|
||||
for _, value := range res.Values {
|
||||
require.IsType(t, mathexp.Series{}, value)
|
||||
lbls := value.GetLabels()
|
||||
require.Contains(t, lbls, nameLabelName)
|
||||
names = append(names, lbls[nameLabelName])
|
||||
}
|
||||
require.EqualValues(t, []string{"test-frame1", "test-frame2"}, names)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
t.Run("should use fields DisplayNameFromDS when it is unique", func(t *testing.T) {
|
||||
f1 := data.NewField("test-value1", nil, []*float64{fp(2)})
|
||||
f1.Config = &data.FieldConfig{DisplayNameFromDS: "test-value1"}
|
||||
f2 := data.NewField("test-value2", nil, []*float64{fp(2)})
|
||||
f2.Config = &data.FieldConfig{DisplayNameFromDS: "test-value2"}
|
||||
frames := []*data.Frame{
|
||||
data.NewFrame("test-frame1",
|
||||
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
||||
f1),
|
||||
data.NewFrame("test-frame2",
|
||||
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
||||
f2),
|
||||
}
|
||||
|
||||
for _, dtype := range supported {
|
||||
t.Run(dtype, func(t *testing.T) {
|
||||
resultType, res, err := convertDataFramesToResults(context.Background(), frames, dtype, s, &logtest.Fake{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "multi frame series", resultType)
|
||||
require.Len(t, res.Values, 2)
|
||||
|
||||
var names []string
|
||||
for _, value := range res.Values {
|
||||
require.IsType(t, mathexp.Series{}, value)
|
||||
lbls := value.GetLabels()
|
||||
require.Contains(t, lbls, nameLabelName)
|
||||
names = append(names, lbls[nameLabelName])
|
||||
}
|
||||
require.EqualValues(t, []string{"test-value1", "test-value2"}, names)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user