Peakq: use generic query function (#82121)

Co-authored-by: Kyle Brandt <kyle@grafana.com>
pull/82255/head
Ryan McKinley 2 years ago committed by GitHub
parent 685ba7d287
commit 4884194879
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      pkg/apis/peakq/v0alpha1/types.go
  2. 8
      pkg/apis/peakq/v0alpha1/zz_generated.openapi.go
  3. 17
      pkg/apis/query/v0alpha1/query.go
  4. 4
      pkg/registry/apis/peakq/render.go
  5. 50
      pkg/registry/apis/peakq/render_examples.go
  6. 4
      pkg/registry/apis/peakq/render_examples_test.go
  7. 45
      pkg/registry/apis/peakq/render_test.go

@ -6,6 +6,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
common "github.com/grafana/grafana/pkg/apis/common/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@ -40,8 +41,8 @@ type Target struct {
// Variables that will be replaced in the query
Variables map[string][]VariableReplacement `json:"variables"`
// The raw query: TODO, should be query.GenericQuery
Properties common.Unstructured `json:"properties"`
// Query target
Properties query.GenericDataQuery `json:"properties"`
}
// TemplateVariable is the definition of a variable that will be interpolated

@ -291,8 +291,8 @@ func schema_pkg_apis_peakq_v0alpha1_Target(ref common.ReferenceCallback) common.
},
"properties": {
SchemaProps: spec.SchemaProps{
Description: "The raw query: TODO, should be query.GenericQuery",
Ref: ref("github.com/grafana/grafana/pkg/apis/common/v0alpha1.Unstructured"),
Description: "Query target",
Ref: ref("github.com/grafana/grafana/pkg/apis/query/v0alpha1.GenericDataQuery"),
},
},
},
@ -300,7 +300,7 @@ func schema_pkg_apis_peakq_v0alpha1_Target(ref common.ReferenceCallback) common.
},
},
Dependencies: []string{
"github.com/grafana/grafana/pkg/apis/common/v0alpha1.Unstructured", "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1.VariableReplacement"},
"github.com/grafana/grafana/pkg/apis/peakq/v0alpha1.VariableReplacement", "github.com/grafana/grafana/pkg/apis/query/v0alpha1.GenericDataQuery"},
}
}
@ -377,7 +377,7 @@ func schema_pkg_apis_peakq_v0alpha1_VariableReplacement(ref common.ReferenceCall
},
"format": {
SchemaProps: spec.SchemaProps{
Description: "How values should be interpolated See: NOTE: the format parameter is not yet supported!\n\nPossible enum values:\n - `\"csv\"` Formats variables with multiple values as a comma-separated string.\n - `\"doublequote\"` Formats single- and multi-valued variables into a comma-separated string\n - `\"json\"` Formats variables with multiple values as a comma-separated string.\n - `\"pipe\"` Formats variables with multiple values into a pipe-separated string.\n - `\"raw\"` Formats variables with multiple values into comma-separated string. This is the default behavior when no format is specified\n - `\"singlequote\"` Formats single- and multi-valued variables into a comma-separated string",
Description: "How values should be interpolated\n\nPossible enum values:\n - `\"csv\"` Formats variables with multiple values as a comma-separated string.\n - `\"doublequote\"` Formats single- and multi-valued variables into a comma-separated string\n - `\"json\"` Formats variables with multiple values as a comma-separated string.\n - `\"pipe\"` Formats variables with multiple values into a pipe-separated string.\n - `\"raw\"` Formats variables with multiple values into comma-separated string. This is the default behavior when no format is specified\n - `\"singlequote\"` Formats single- and multi-valued variables into a comma-separated string",
Type: []string{"string"},
Format: "",
Enum: []interface{}{"csv", "doublequote", "json", "pipe", "raw", "singlequote"},

@ -75,6 +75,12 @@ type GenericDataQuery struct {
props map[string]any `json:"-"`
}
func NewGenericDataQuery(vals map[string]any) GenericDataQuery {
q := GenericDataQuery{}
_ = q.unmarshal(vals)
return q
}
// TimeRange represents a time range for a query and is a property of DataQuery.
type TimeRange struct {
// From is the start time of the query.
@ -120,7 +126,7 @@ func (g GenericDataQuery) MarshalJSON() ([]byte, error) {
}
vals["refId"] = g.RefID
if g.Datasource.Type != "" || g.Datasource.UID != "" {
if g.Datasource != nil && (g.Datasource.Type != "" || g.Datasource.UID != "") {
vals["datasource"] = g.Datasource
}
if g.DatasourceId > 0 {
@ -143,6 +149,15 @@ func (g *GenericDataQuery) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}
return g.unmarshal(vals)
}
func (g *GenericDataQuery) unmarshal(vals map[string]any) error {
if vals == nil {
g.props = nil
return nil
}
key := "refId"
v, ok := vals[key]
if ok {

@ -14,8 +14,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
common "github.com/grafana/grafana/pkg/apis/common/v0alpha1"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
)
type renderREST struct {
@ -198,7 +198,7 @@ func Render(qt peakq.QueryTemplateSpec, selectedValues map[string][]string) (*pe
if err != nil {
return nil, err
}
u := common.Unstructured{}
u := query.GenericDataQuery{}
err = u.UnmarshalJSON(raw)
if err != nil {
return nil, err

@ -3,8 +3,8 @@ package peakq
import (
"github.com/grafana/grafana-plugin-sdk-go/data"
common "github.com/grafana/grafana/pkg/apis/common/v0alpha1"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
)
var basicTemplateSpec = peakq.QueryTemplateSpec{
@ -38,41 +38,37 @@ var basicTemplateSpec = peakq.QueryTemplateSpec{
},
},
Properties: common.Unstructured{
Object: map[string]any{
"refId": "A", // TODO: Set when Where?
"datasource": map[string]any{
"type": "prometheus",
"uid": "foo", // TODO: Probably a default templating thing to set this.
},
"editorMode": "builder",
"expr": "metricName + metricName + 42",
"instant": true,
"range": false,
"exemplar": false,
},
},
},
},
}
var basicTemplateRenderedTargets = []peakq.Target{
{
DataType: data.FrameTypeUnknown,
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Properties: common.Unstructured{
Object: map[string]any{
Properties: query.NewGenericDataQuery(map[string]any{
"refId": "A", // TODO: Set when Where?
"datasource": map[string]any{
"type": "prometheus",
"uid": "foo", // TODO: Probably a default templating thing to set this.
},
"editorMode": "builder",
"expr": "up + up + 42",
"expr": "metricName + metricName + 42",
"instant": true,
"range": false,
"exemplar": false,
},
}),
},
},
}
var basicTemplateRenderedTargets = []peakq.Target{
{
DataType: data.FrameTypeUnknown,
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Properties: query.NewGenericDataQuery(map[string]any{
"refId": "A", // TODO: Set when Where?
"datasource": map[string]any{
"type": "prometheus",
"uid": "foo", // TODO: Probably a default templating thing to set this.
},
"editorMode": "builder",
"expr": "up + up + 42",
"instant": true,
"range": false,
"exemplar": false,
}),
},
}

@ -11,7 +11,9 @@ import (
func TestRender(t *testing.T) {
rT, err := Render(basicTemplateSpec, map[string][]string{"metricName": {"up"}})
require.NoError(t, err)
require.Equal(t, basicTemplateRenderedTargets[0].Properties.Object["expr"], rT.Targets[0].Properties.Object["expr"])
require.Equal(t,
basicTemplateRenderedTargets[0].Properties.AdditionalProperties()["expr"],
rT.Targets[0].Properties.AdditionalProperties()["expr"])
b, _ := json.MarshalIndent(basicTemplateSpec, "", " ")
fmt.Println(string(b))
}

@ -6,8 +6,8 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/require"
common "github.com/grafana/grafana/pkg/apis/common/v0alpha1"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
)
var nestedFieldRender = peakq.QueryTemplateSpec{
@ -33,13 +33,11 @@ var nestedFieldRender = peakq.QueryTemplateSpec{
},
},
},
Properties: common.Unstructured{
Object: map[string]any{
"nestedObject": map[string]any{
"anArray": []any{"foo", .2},
},
Properties: query.NewGenericDataQuery(map[string]any{
"nestedObject": map[string]any{
"anArray": []any{"foo", .2},
},
},
}),
},
},
}
@ -59,13 +57,12 @@ var nestedFieldRenderedTargets = []peakq.Target{
},
},
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Properties: common.Unstructured{
Object: map[string]any{
Properties: query.NewGenericDataQuery(
map[string]any{
"nestedObject": map[string]any{
"anArray": []any{"up", .2},
},
},
},
}),
},
}
@ -121,11 +118,9 @@ var multiVarTemplate = peakq.QueryTemplateSpec{
},
},
Properties: common.Unstructured{
Object: map[string]any{
"expr": "1 + metricName + 1 + anotherMetric + metricName",
},
},
Properties: query.NewGenericDataQuery(map[string]any{
"expr": "1 + metricName + 1 + anotherMetric + metricName",
}),
},
},
}
@ -161,11 +156,9 @@ var multiVarRenderedTargets = []peakq.Target{
},
},
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Properties: common.Unstructured{
Object: map[string]any{
"expr": "1 + up + 1 + sloths_do_like_a_good_nap + up",
},
},
Properties: query.NewGenericDataQuery(map[string]any{
"expr": "1 + up + 1 + sloths_do_like_a_good_nap + up",
}),
},
}
@ -190,11 +183,9 @@ func TestRenderWithRune(t *testing.T) {
},
Targets: []peakq.Target{
{
Properties: common.Unstructured{
Object: map[string]any{
"message": "🐦 name!",
},
},
Properties: query.NewGenericDataQuery(map[string]any{
"message": "🐦 name!",
}),
Variables: map[string][]peakq.VariableReplacement{
"name": {
{
@ -217,5 +208,5 @@ func TestRenderWithRune(t *testing.T) {
rq, err := Render(qt, selectedValues)
require.NoError(t, err)
require.Equal(t, "🐦 🦥!", rq.Targets[0].Properties.Object["message"])
require.Equal(t, "🐦 🦥!", rq.Targets[0].Properties.AdditionalProperties()["message"])
}

Loading…
Cancel
Save