Peakq: move templates into query service (#82193)

This commit is contained in:
Ryan McKinley
2024-02-08 09:27:03 -08:00
committed by GitHub
parent a439ee46bf
commit ac5a387086
19 changed files with 812 additions and 744 deletions

View File

@@ -1,70 +0,0 @@
package peakq
import (
"bytes"
"encoding/csv"
"encoding/json"
"strings"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
)
func formatVariables(fmt peakq.VariableFormat, input []string) string {
if len(input) < 1 {
return ""
}
// MultiValued formats
// nolint: exhaustive
switch fmt {
case peakq.FormatJSON:
v, _ := json.Marshal(input)
return string(v)
case peakq.FormatDoubleQuote:
sb := bytes.NewBufferString("")
for idx, val := range input {
if idx > 0 {
_, _ = sb.WriteRune(',')
}
_, _ = sb.WriteRune('"')
_, _ = sb.WriteString(strings.ReplaceAll(val, `"`, `\"`))
_, _ = sb.WriteRune('"')
}
return sb.String()
case peakq.FormatSingleQuote:
sb := bytes.NewBufferString("")
for idx, val := range input {
if idx > 0 {
_, _ = sb.WriteRune(',')
}
_, _ = sb.WriteRune('\'')
_, _ = sb.WriteString(strings.ReplaceAll(val, `'`, `\'`))
_, _ = sb.WriteRune('\'')
}
return sb.String()
case peakq.FormatCSV:
sb := bytes.NewBufferString("")
w := csv.NewWriter(sb)
_ = w.Write(input)
w.Flush()
v := sb.Bytes()
return string(v[:len(v)-1])
}
// Single valued formats
if len(input) == 1 {
return input[0]
}
// nolint: exhaustive
switch fmt {
case peakq.FormatPipe:
return strings.Join(input, "|")
}
// Raw output (joined with a comma)
return strings.Join(input, ",")
}

View File

@@ -1,83 +0,0 @@
package peakq
import (
"testing"
"github.com/stretchr/testify/require"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
)
func TestFormat(t *testing.T) {
// Invalid input
require.Equal(t, "", formatVariables(peakq.FormatCSV, nil))
require.Equal(t, "", formatVariables(peakq.FormatCSV, []string{}))
type check struct {
name string
input []string
output map[peakq.VariableFormat]string
}
tests := []check{
{
name: "three simple variables",
input: []string{"a", "b", "c"},
output: map[peakq.VariableFormat]string{
peakq.FormatCSV: "a,b,c",
peakq.FormatJSON: `["a","b","c"]`,
peakq.FormatDoubleQuote: `"a","b","c"`,
peakq.FormatSingleQuote: `'a','b','c'`,
peakq.FormatPipe: `a|b|c`,
peakq.FormatRaw: "a,b,c",
},
},
{
name: "single value",
input: []string{"a"},
output: map[peakq.VariableFormat]string{
peakq.FormatCSV: "a",
peakq.FormatJSON: `["a"]`,
peakq.FormatDoubleQuote: `"a"`,
peakq.FormatSingleQuote: `'a'`,
peakq.FormatPipe: "a",
peakq.FormatRaw: "a",
},
},
{
name: "value with quote",
input: []string{`hello "world"`},
output: map[peakq.VariableFormat]string{
peakq.FormatCSV: `"hello ""world"""`, // note the double quotes
peakq.FormatJSON: `["hello \"world\""]`,
peakq.FormatDoubleQuote: `"hello \"world\""`,
peakq.FormatSingleQuote: `'hello "world"'`,
peakq.FormatPipe: `hello "world"`,
peakq.FormatRaw: `hello "world"`,
},
},
}
for _, test := range tests {
// Make sure all keys are set in tests
all := map[peakq.VariableFormat]bool{
peakq.FormatRaw: true,
peakq.FormatCSV: true,
peakq.FormatJSON: true,
peakq.FormatDoubleQuote: true,
peakq.FormatSingleQuote: true,
peakq.FormatPipe: true,
}
// Check the default (no format) matches CSV
require.Equal(t, test.output[peakq.FormatRaw],
formatVariables("", test.input),
"test %s default values are not raw", test.name)
// Check each input value
for format, v := range test.output {
require.Equal(t, v, formatVariables(format, test.input), "Test: %s (format:%s)", test.name, format)
delete(all, format)
}
require.Empty(t, all, "test %s is missing cases for: %v", test.name, all)
}
}

View File

@@ -6,16 +6,14 @@ import (
"fmt"
"net/http"
"net/url"
"sort"
"strings"
"github.com/spyzhov/ajson"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
)
type renderREST struct {
@@ -44,7 +42,7 @@ func (r *renderREST) Connect(ctx context.Context, name string, opts runtime.Obje
if err != nil {
return nil, err
}
template, ok := obj.(*peakq.QueryTemplate)
t, ok := obj.(*peakq.QueryTemplate)
if !ok {
return nil, fmt.Errorf("expected template")
}
@@ -55,12 +53,14 @@ func (r *renderREST) Connect(ctx context.Context, name string, opts runtime.Obje
responder.Error(err)
return
}
rq, err := Render(template.Spec, input)
out, err := template.RenderTemplate(t.Spec, input)
if err != nil {
responder.Error(fmt.Errorf("failed to render: %w", err))
return
}
responder.Object(http.StatusOK, rq)
responder.Object(http.StatusOK, &peakq.RenderedQuery{
Targets: out,
})
}), nil
}
@@ -79,7 +79,7 @@ func renderPOSTHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(500)
return
}
results, err := Render(qT.Spec, input)
results, err := template.RenderTemplate(qT.Spec, input)
if err != nil {
_, _ = w.Write([]byte("ERROR: " + err.Error()))
w.WriteHeader(500)
@@ -88,7 +88,9 @@ func renderPOSTHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(results)
_ = json.NewEncoder(w).Encode(peakq.RenderedQuery{
Targets: results,
})
}
// Replicate the grafana dashboard URL syntax
@@ -103,110 +105,3 @@ func makeVarMapFromParams(v url.Values) (map[string][]string, error) {
}
return input, nil
}
type replacement struct {
*peakq.Position
*peakq.TemplateVariable
format peakq.VariableFormat
}
func getReplacementMap(qt peakq.QueryTemplateSpec) map[int]map[string][]replacement {
byTargetPath := make(map[int]map[string][]replacement)
varMap := make(map[string]*peakq.TemplateVariable, len(qt.Variables))
for i, v := range qt.Variables {
varMap[v.Key] = &qt.Variables[i]
}
for i, target := range qt.Targets {
if byTargetPath[i] == nil {
byTargetPath[i] = make(map[string][]replacement)
}
for k, vReps := range target.Variables {
for rI, rep := range vReps {
byTargetPath[i][rep.Path] = append(byTargetPath[i][rep.Path],
replacement{
Position: vReps[rI].Position,
TemplateVariable: varMap[k],
format: rep.Format,
},
)
}
}
}
for idx, byTargetIdx := range byTargetPath {
for path := range byTargetIdx {
sort.Slice(byTargetPath[idx][path], func(i, j int) bool {
return byTargetPath[idx][path][i].Start < byTargetPath[idx][path][j].Start
})
}
}
return byTargetPath
}
func Render(qt peakq.QueryTemplateSpec, selectedValues map[string][]string) (*peakq.RenderedQuery, error) {
targets := qt.DeepCopy().Targets
rawTargetObjects := make([]*ajson.Node, len(qt.Targets))
for i, t := range qt.Targets {
b, err := t.Properties.MarshalJSON()
if err != nil {
return nil, err
}
rawTargetObjects[i], err = ajson.Unmarshal(b)
if err != nil {
return nil, err
}
}
rm := getReplacementMap(qt)
for targetIdx, byTargetIdx := range rm {
for path, reps := range byTargetIdx {
o := rawTargetObjects[targetIdx]
nodes, err := o.JSONPath(path)
if err != nil {
return nil, fmt.Errorf("failed to find path %v: %w", path, err)
}
if len(nodes) != 1 {
return nil, fmt.Errorf("expected one lead node at path %v but got %v", path, len(nodes))
}
n := nodes[0]
if !n.IsString() {
return nil, fmt.Errorf("only string type leaf notes supported currently, %v is not a string", path)
}
s := []rune(n.String())
s = s[1 : len(s)-1]
var offSet int64
for _, r := range reps {
value := []rune(formatVariables(r.format, selectedValues[r.Key]))
if r.Position == nil {
return nil, fmt.Errorf("nil position not support yet, will be full replacement")
}
s = append(s[:r.Start+offSet], append(value, s[r.End+offSet:]...)...)
offSet += int64(len(value)) - (r.End - r.Start)
}
if err = n.SetString(string(s)); err != nil {
return nil, err
}
}
}
for i, aT := range rawTargetObjects {
raw, err := ajson.Marshal(aT)
if err != nil {
return nil, err
}
u := query.GenericDataQuery{}
err = u.UnmarshalJSON(raw)
if err != nil {
return nil, err
}
targets[i].Properties = u
}
return &peakq.RenderedQuery{
Targets: targets,
}, nil
}

View File

@@ -3,34 +3,34 @@ package peakq
import (
"github.com/grafana/grafana-plugin-sdk-go/data"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
)
var basicTemplateSpec = peakq.QueryTemplateSpec{
var basicTemplateSpec = template.QueryTemplate{
Title: "Test",
Variables: []peakq.TemplateVariable{
Variables: []template.TemplateVariable{
{
Key: "metricName",
DefaultValues: []string{`down`},
},
},
Targets: []peakq.Target{
Targets: []template.Target{
{
DataType: data.FrameTypeUnknown,
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Variables: map[string][]peakq.VariableReplacement{
Variables: map[string][]template.VariableReplacement{
"metricName": {
{
Path: "$.expr",
Position: &peakq.Position{
Position: &template.Position{
Start: 0,
End: 10,
},
},
{
Path: "$.expr",
Position: &peakq.Position{
Position: &template.Position{
Start: 13,
End: 23,
},
@@ -54,7 +54,7 @@ var basicTemplateSpec = peakq.QueryTemplateSpec{
},
}
var basicTemplateRenderedTargets = []peakq.Target{
var basicTemplateRenderedTargets = []template.Target{
{
DataType: data.FrameTypeUnknown,
//DataTypeVersion: data.FrameTypeVersion{0, 0},

View File

@@ -6,14 +6,16 @@ import (
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
)
func TestRender(t *testing.T) {
rT, err := Render(basicTemplateSpec, map[string][]string{"metricName": {"up"}})
rT, err := template.RenderTemplate(basicTemplateSpec, map[string][]string{"metricName": {"up"}})
require.NoError(t, err)
require.Equal(t,
basicTemplateRenderedTargets[0].Properties.AdditionalProperties()["expr"],
rT.Targets[0].Properties.AdditionalProperties()["expr"])
rT[0].Properties.AdditionalProperties()["expr"])
b, _ := json.MarshalIndent(basicTemplateSpec, "", " ")
fmt.Println(string(b))
}

View File

@@ -1,212 +0,0 @@
package peakq
import (
"testing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/require"
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
)
var nestedFieldRender = peakq.QueryTemplateSpec{
Title: "Test",
Variables: []peakq.TemplateVariable{
{
Key: "metricName",
},
},
Targets: []peakq.Target{
{
DataType: data.FrameTypeUnknown,
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Variables: map[string][]peakq.VariableReplacement{
"metricName": {
{
Path: "$.nestedObject.anArray[0]",
Position: &peakq.Position{
Start: 0,
End: 3,
},
},
},
},
Properties: query.NewGenericDataQuery(map[string]any{
"nestedObject": map[string]any{
"anArray": []any{"foo", .2},
},
}),
},
},
}
var nestedFieldRenderedTargets = []peakq.Target{
{
DataType: data.FrameTypeUnknown,
Variables: map[string][]peakq.VariableReplacement{
"metricName": {
{
Path: "$.nestedObject.anArray[0]",
Position: &peakq.Position{
Start: 0,
End: 3,
},
},
},
},
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Properties: query.NewGenericDataQuery(
map[string]any{
"nestedObject": map[string]any{
"anArray": []any{"up", .2},
},
}),
},
}
func TestNestedFieldRender(t *testing.T) {
rT, err := Render(nestedFieldRender, map[string][]string{"metricName": {"up"}})
require.NoError(t, err)
require.Equal(t,
nestedFieldRenderedTargets,
rT.Targets,
)
}
var multiVarTemplate = peakq.QueryTemplateSpec{
Title: "Test",
Variables: []peakq.TemplateVariable{
{
Key: "metricName",
},
{
Key: "anotherMetric",
},
},
Targets: []peakq.Target{
{
DataType: data.FrameTypeUnknown,
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Variables: map[string][]peakq.VariableReplacement{
"metricName": {
{
Path: "$.expr",
Position: &peakq.Position{
Start: 4,
End: 14,
},
},
{
Path: "$.expr",
Position: &peakq.Position{
Start: 37,
End: 47,
},
},
},
"anotherMetric": {
{
Path: "$.expr",
Position: &peakq.Position{
Start: 21,
End: 34,
},
},
},
},
Properties: query.NewGenericDataQuery(map[string]any{
"expr": "1 + metricName + 1 + anotherMetric + metricName",
}),
},
},
}
var multiVarRenderedTargets = []peakq.Target{
{
DataType: data.FrameTypeUnknown,
Variables: map[string][]peakq.VariableReplacement{
"metricName": {
{
Path: "$.expr",
Position: &peakq.Position{
Start: 4,
End: 14,
},
},
{
Path: "$.expr",
Position: &peakq.Position{
Start: 37,
End: 47,
},
},
},
"anotherMetric": {
{
Path: "$.expr",
Position: &peakq.Position{
Start: 21,
End: 34,
},
},
},
},
//DataTypeVersion: data.FrameTypeVersion{0, 0},
Properties: query.NewGenericDataQuery(map[string]any{
"expr": "1 + up + 1 + sloths_do_like_a_good_nap + up",
}),
},
}
func TestMultiVarTemplate(t *testing.T) {
rT, err := Render(multiVarTemplate, map[string][]string{
"metricName": {"up"},
"anotherMetric": {"sloths_do_like_a_good_nap"},
})
require.NoError(t, err)
require.Equal(t,
multiVarRenderedTargets,
rT.Targets,
)
}
func TestRenderWithRune(t *testing.T) {
qt := peakq.QueryTemplateSpec{
Variables: []peakq.TemplateVariable{
{
Key: "name",
},
},
Targets: []peakq.Target{
{
Properties: query.NewGenericDataQuery(map[string]any{
"message": "🐦 name!",
}),
Variables: map[string][]peakq.VariableReplacement{
"name": {
{
Path: "$.message",
Position: &peakq.Position{
Start: 2,
End: 6,
},
},
},
},
},
},
}
selectedValues := map[string][]string{
"name": {"🦥"},
}
rq, err := Render(qt, selectedValues)
require.NoError(t, err)
require.Equal(t, "🐦 🦥!", rq.Targets[0].Properties.AdditionalProperties()["message"])
}