-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
129 lines (104 loc) · 2.62 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package graphiteapi
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// NewRenderQuery returns a RenderQuery instance
func NewRenderQuery(base, from, until string, targets ...*RenderTarget) *RenderQuery {
q := &RenderQuery{
Base: base,
Targets: targets,
From: from,
Until: until,
MaxDataPoints: 0,
}
return q
}
func (q *RenderQuery) SetFrom(from string) *RenderQuery {
q.From = from
return q
}
func (q *RenderQuery) SetUntil(until string) *RenderQuery {
q.Until = until
return q
}
func (q *RenderQuery) AddTarget(target *RenderTarget) *RenderQuery {
q.Targets = append(q.Targets, target)
return q
}
func (q *RenderQuery) SetMaxDataPoints(maxDataPoints int) *RenderQuery {
q.MaxDataPoints = maxDataPoints
return q
}
// URL implements Query interface
func (q *RenderQuery) URL() *url.URL {
u, _ := url.Parse(q.Base + "/render/")
v := url.Values{}
// force set format to protobuf
v.Set("format", "protobuf")
for _, target := range q.Targets {
v.Add("target", target.String())
}
if q.From != "" {
v.Set("from", q.From)
}
if q.Until != "" {
v.Set("until", q.Until)
}
if q.MaxDataPoints != 0 {
v.Set("maxDataPoints", strconv.Itoa(q.MaxDataPoints))
}
u.RawQuery = v.Encode()
return u
}
// Request implements Query interface
func (q *RenderQuery) Request(ctx context.Context) (*RenderResponse, error) {
var req *http.Request
var err error
response := &RenderResponse{}
if req, err = httpNewRequest("GET", q.URL().String(), nil); err != nil {
return nil, err
}
if err = httpDo(ctx, req, &response.MultiFetchResponse); err != nil {
return nil, err
}
return response, nil
}
func (t *RenderTarget) String() string {
return t.str
}
func NewRenderTarget(seriesList string) *RenderTarget {
return &RenderTarget{
str: seriesList,
}
}
func (t *RenderTarget) ApplyFunction(name string, args ...interface{}) *RenderTarget {
tmp := make([]string, len(args)+1)
tmp[0] = t.String()
for i, a := range args {
tmp[i+1] = fmt.Sprintf("%v", a)
}
t.str = fmt.Sprintf("%s(%s)", name, strings.Join(tmp, ","))
return t
}
func (t *RenderTarget) ApplyFunctionWithoutSeries(name string, args ...interface{}) *RenderTarget {
tmp := make([]string, len(args))
for i, a := range args {
tmp[i] = fmt.Sprintf("%v", a)
}
t.str = fmt.Sprintf("%s(%s)", name, strings.Join(tmp, ","))
return t
}
//
// function shortcuts, for code completion
//
func (t *RenderTarget) SumSeries() *RenderTarget {
return t.ApplyFunction("sumSeries")
}
func (t *RenderTarget) ConstantLine(value interface{}) *RenderTarget {
return t.ApplyFunctionWithoutSeries("constantLine", value)
}