-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfetch.go
227 lines (182 loc) · 5.71 KB
/
fetch.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package gosnowth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/circonus-labs/gosnowth/fb/fetch"
flatbuffers "github.com/google/flatbuffers/go"
)
// FetchStream values represent queries for individual data streams in an
// IRONdb fetch request.
type FetchStream struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Kind string `json:"kind"`
Label string `json:"label,omitempty"`
Transform string `json:"transform"`
TransformParams []string `json:"transform_params,omitempty"`
}
// FetchReduce values represent reduce operations to perform on specified
// data streams in an IRONdb fetch request.
type FetchReduce struct {
Label string `json:"label"`
Method string `json:"method"`
MethodParams []string `json:"method_params,omitempty"`
}
// FetchQuery values represent queries used to fetch IRONdb data.
type FetchQuery struct {
Start time.Time `json:"start"`
Period time.Duration `json:"period"`
Count int64 `json:"count"`
Streams []FetchStream `json:"streams"`
Reduce []FetchReduce `json:"reduce"`
}
// MarshalJSON encodes a FetchQuery value into a JSON format byte slice.
func (fq *FetchQuery) MarshalJSON() ([]byte, error) {
v := struct {
Start float64 `json:"start"`
Period float64 `json:"period"`
Count int64 `json:"count"`
Streams []FetchStream `json:"streams"`
Reduce []FetchReduce `json:"reduce"`
}{}
fv, err := strconv.ParseFloat(formatTimestamp(fq.Start), 64)
if err != nil {
return nil, fmt.Errorf("invalid fetch start value: " +
formatTimestamp(fq.Start))
}
v.Start = fv
v.Period = fq.Period.Seconds()
v.Count = fq.Count
if len(fq.Streams) > 0 {
v.Streams = fq.Streams
}
if len(fq.Reduce) > 0 {
v.Reduce = fq.Reduce
}
return json.Marshal(v)
}
// UnmarshalJSON decodes a JSON format byte slice into a HistogramValue value.
func (fq *FetchQuery) UnmarshalJSON(b []byte) error {
v := struct {
Start float64 `json:"start"`
Period float64 `json:"period"`
Count int64 `json:"count"`
Streams []FetchStream `json:"streams"`
Reduce []FetchReduce `json:"reduce"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
if v.Start == 0 {
return fmt.Errorf("fetch query missing start: " + string(b))
}
fq.Start, err = parseTimestamp(strconv.FormatFloat(v.Start, 'f', 3, 64))
if err != nil {
return err
}
if v.Period == 0 {
return fmt.Errorf("fetch query missing period: " + string(b))
}
fq.Period = time.Duration(v.Period*1000) * time.Millisecond
if v.Count == 0 {
return fmt.Errorf("fetch query missing count: " + string(b))
}
fq.Count = v.Count
if len(v.Streams) < 1 {
return fmt.Errorf("fetch query requires at least one stream: " +
string(b))
}
fq.Streams = v.Streams
if len(v.Reduce) < 1 {
return fmt.Errorf("fetch query requires at least one reduce: " +
string(b))
}
fq.Reduce = v.Reduce
return nil
}
// Timestamp returns the FetchQuery start time as a string in the IRONdb
// timestamp format.
func (fq *FetchQuery) Timestamp() string {
return formatTimestamp(fq.Start)
}
// FetchValues retrieves data values using the IRONdb fetch API.
func (sc *SnowthClient) FetchValues(q *FetchQuery, nodes ...*SnowthNode) (*DF4Response, error) {
return sc.FetchValuesContext(context.Background(), q, nodes...)
}
// FetchValuesContext is the context aware version of FetchValues.
func (sc *SnowthClient) FetchValuesContext(ctx context.Context,
q *FetchQuery, nodes ...*SnowthNode,
) (*DF4Response, error) {
var node *SnowthNode
switch {
case len(nodes) > 0 && nodes[0] != nil:
node = nodes[0]
case len(q.Streams) > 0:
node = sc.GetActiveNode(sc.FindMetricNodeIDs(q.Streams[0].UUID, q.Streams[0].Name))
default:
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(&q); err != nil {
return nil, err
}
hdrs := http.Header{"Content-Type": {"application/json"}}
body, _, err := sc.DoRequestContext(ctx, node, "POST", "/fetch", buf, hdrs)
if err != nil {
return nil, err
}
rb, err := io.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("unable to read IRONdb response body: %w", err)
}
rb = replaceInf(rb)
r := &DF4Response{}
if err := decodeJSON(bytes.NewBuffer(rb), &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r, nil
}
// FetchFlatbufferContentType is the content type header for flatbuffer fetch data.
const FetchFlatbufferContentType = "x-irondb-fetch-flatbuffer"
// Df4FlatbufferAccept is the accept header for flatbuffer df4 data.
const Df4FlatbufferAccept = "x-irondb-df4-flatbuffer"
// FetchValuesFb retrieves data values using the IRONdb fetch API with FlatBuffers.
func (sc *SnowthClient) FetchValuesFb(node *SnowthNode,
q *fetch.FetchT,
) (*fetch.DF4T, error) {
return sc.FetchValuesFbContext(context.Background(), node, q)
}
// FetchValuesFbContext is the context aware version of FetchValuesFb.
func (sc *SnowthClient) FetchValuesFbContext(ctx context.Context,
node *SnowthNode, q *fetch.FetchT,
) (*fetch.DF4T, error) {
builder := flatbuffers.NewBuilder(8192)
qOffset := fetch.FetchPack(builder, q)
builder.Finish(qOffset)
buf := bytes.NewBuffer(builder.FinishedBytes())
hdrs := http.Header{
"Content-Type": {FetchFlatbufferContentType},
"Accept": {Df4FlatbufferAccept},
}
body, _, err := sc.DoRequestContext(ctx, node, "POST", "/fetch", buf, hdrs)
if err != nil {
return nil, err
}
df4Buf, err := io.ReadAll(body)
if err != nil {
return nil, err
}
df4 := fetch.GetRootAsDF4(df4Buf, flatbuffers.UOffsetT(0))
r := df4.UnPack()
return r, nil
}