-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
197 lines (184 loc) · 4.41 KB
/
query.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package meter
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
// Query is a query for event results
type Query struct {
TimeRange
Match Fields `json:"match,omitempty"`
Group []string `json:"group,omitempty"`
EmptyValue string `json:"empty,omitempty"`
}
// URL adds the query to a URL
func (q *Query) URL(baseURL string, events ...string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
values := url.Values(make(map[string][]string))
values.Set("start", strconv.FormatInt(q.Start.Unix(), 10))
values.Set("end", strconv.FormatInt(q.End.Unix(), 10))
for _, label := range q.Group {
values.Add("group", label)
}
if q.Step != 0 {
values.Set("step", q.Step.String())
}
match := q.Match.Sorted()
for _, field := range match {
values.Add(`match.`+field.Label, field.Value)
}
if q.EmptyValue != "" {
values.Set("empty", q.EmptyValue)
}
for _, event := range events {
values.Add("event", event)
}
u.RawQuery = values.Encode()
return u.String(), nil
}
// TruncateTimestamp truncates a timestamp to Query.Step
func (q *Query) TruncateTimestamp(ts int64) int64 {
step := int64(q.Step / time.Second)
return stepTS(ts, step)
}
// SetValues sets query values from a URL query
func (q *Query) SetValues(values url.Values) {
if step, ok := values["step"]; ok {
if len(step) > 0 {
q.Step, _ = time.ParseDuration(step[0])
} else {
q.Step = 0
}
} else {
q.Step = -1
}
start, _ := strconv.ParseInt(values.Get("start"), 10, 64)
if start > 0 {
q.Start = time.Unix(start, 0).In(time.UTC)
}
if end, _ := strconv.ParseInt(values.Get("end"), 10, 64); end > 0 {
q.End = time.Unix(end, 0).In(time.UTC)
}
match := q.Match[:0]
for key, values := range values {
if strings.HasPrefix(key, "match.") {
label := strings.TrimPrefix(key, "match.")
for _, value := range values {
match = append(match, Field{
Label: label,
Value: value,
})
}
}
}
group, ok := values["group"]
if ok && len(group) == 0 {
group = make([]string, 0, len(match))
for i := range match {
m := &match[i]
group = appendDistinct(group, m.Label)
}
}
sort.Stable(match)
q.Match, q.Group = match, group
q.EmptyValue = values.Get("empty")
}
// TimeRange is a range of time with a specific step
type TimeRange struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
Step time.Duration `json:"step"`
}
// QueryRunner runs queries
type QueryRunner interface {
RunQuery(ctx context.Context, q *Query, events ...string) (Results, error)
}
// QueryHandler returns an HTTP endpoint for a QueryRunner
func QueryHandler(qr QueryRunner) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
values := r.URL.Query()
events := values["event"]
q := Query{}
q.SetValues(values)
if q.Start.IsZero() {
q.Start = time.Unix(0, 0)
}
if q.End.IsZero() {
q.End = time.Now()
}
typ := ResultTypeFromString(values.Get("results"))
if typ == TotalsResult {
q.Step = -1
}
ctx := r.Context()
results, err := qr.RunQuery(ctx, &q, events...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var x interface{}
switch typ {
case TotalsResult:
x = results.Totals()
case FieldSummaryResult:
x = results.FieldSummaries()
case EventSummaryResult:
x = results.EventSummaries(q.EmptyValue)
default:
x = results
}
enc := json.NewEncoder(w)
w.Header().Set("Content-Type", "application/json")
enc.Encode(x)
}
}
// HTTPQueryRunner runs queries over http
type HTTPQueryRunner struct {
URL string
Client *http.Client
}
// RunQuery implements QueryRunner interface
func (qr *HTTPQueryRunner) RunQuery(ctx context.Context, q *Query, events ...string) (Results, error) {
u, err := q.URL(qr.URL, events...)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
if ctx != nil {
req = req.WithContext(ctx)
}
c := qr.Client
if c == nil {
c = http.DefaultClient
}
res, err := c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(`Invalid response status`)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var results Results
if err := json.Unmarshal(data, &results); err != nil {
return nil, err
}
return results, nil
}