forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.go
107 lines (92 loc) · 2.5 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package peakq
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
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"
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
)
type renderREST struct {
getter rest.Getter
}
var _ = rest.Connecter(&renderREST{})
func (r *renderREST) New() runtime.Object {
return &peakq.RenderedQuery{}
}
func (r *renderREST) Destroy() {
}
func (r *renderREST) ConnectMethods() []string {
return []string{"GET"}
}
func (r *renderREST) NewConnectOptions() (runtime.Object, bool, string) {
return nil, false, "" // true means you can use the trailing path as a variable
}
func (r *renderREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
obj, err := r.getter.Get(ctx, name, &v1.GetOptions{})
if err != nil {
return nil, err
}
t, ok := obj.(*peakq.QueryTemplate)
if !ok {
return nil, fmt.Errorf("expected template")
}
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
input, err := makeVarMapFromParams(req.URL.Query())
if err != nil {
responder.Error(err)
return
}
out, err := template.RenderTemplate(t.Spec, input)
if err != nil {
responder.Error(fmt.Errorf("failed to render: %w", err))
return
}
responder.Object(http.StatusOK, &peakq.RenderedQuery{
Targets: out,
})
}), nil
}
func renderPOSTHandler(w http.ResponseWriter, req *http.Request) {
input, err := makeVarMapFromParams(req.URL.Query())
if err != nil {
_, _ = w.Write([]byte("ERROR: " + err.Error()))
w.WriteHeader(500)
return
}
var qT peakq.QueryTemplate
err = json.NewDecoder(req.Body).Decode(&qT.Spec)
if err != nil {
_, _ = w.Write([]byte("ERROR: " + err.Error()))
w.WriteHeader(500)
return
}
results, err := template.RenderTemplate(qT.Spec, input)
if err != nil {
_, _ = w.Write([]byte("ERROR: " + err.Error()))
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(peakq.RenderedQuery{
Targets: results,
})
}
// Replicate the grafana dashboard URL syntax
// &var-abc=1&var=abc=2&var-xyz=3...
func makeVarMapFromParams(v url.Values) (map[string][]string, error) {
input := make(map[string][]string, len(v))
for key, vals := range v {
if !strings.HasPrefix(key, "var-") {
continue
}
input[key[4:]] = vals
}
return input, nil
}