-
Notifications
You must be signed in to change notification settings - Fork 3
/
raw.go
187 lines (154 loc) · 4.72 KB
/
raw.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
package gosnowth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"time"
"github.com/circonus-labs/gosnowth/fb/noit"
flatbuffers "github.com/google/flatbuffers/go"
)
// MetriclistFlatbufferContentType is the content type header for flatbuffer
// raw data.
const MetriclistFlatbufferContentType = "application/x-circonus-metric-list-flatbuffer"
// RawNumericValueResponse values represent raw numeric data responses
// from IRONdb.
type RawNumericValueResponse struct {
Data []RawNumericValue
}
// UnmarshalJSON decodes a JSON format byte slice into a
// RawNumericValueResponse.
func (rv *RawNumericValueResponse) UnmarshalJSON(b []byte) error {
rv.Data = []RawNumericValue{}
values := [][]interface{}{}
if err := json.Unmarshal(b, &values); err != nil {
return fmt.Errorf("failed to deserialize raw numeric response %w", err)
}
for _, entry := range values {
rnv := RawNumericValue{}
if m, ok := entry[1].(float64); ok {
rnv.Value = m
}
// grab the timestamp
if v, ok := entry[0].(float64); ok {
rnv.Time = time.Unix(int64(v/1000), 0)
}
rv.Data = append(rv.Data, rnv)
}
return nil
}
// RawNumericValue values represent raw numeric data.
type RawNumericValue struct {
Time time.Time
Value float64
}
// ReadRawNumericValues reads raw numeric data from a node.
func (sc *SnowthClient) ReadRawNumericValues(start time.Time, end time.Time,
uuid string, metric string,
nodes ...*SnowthNode,
) ([]RawNumericValue, error) {
return sc.ReadRawNumericValuesContext(context.Background(), start, end,
uuid, metric, nodes...)
}
// ReadRawNumericValuesContext is the context aware version of
// ReadRawNumericValues.
func (sc *SnowthClient) ReadRawNumericValuesContext(ctx context.Context,
start, end time.Time, uuid, metric string,
nodes ...*SnowthNode,
) ([]RawNumericValue, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(uuid, metric))
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
qp := url.Values{}
qp.Add("start_ts", formatTimestamp(start))
qp.Add("end_ts", formatTimestamp(end))
r := &RawNumericValueResponse{}
body, _, err := sc.DoRequestContext(ctx, node, "GET", path.Join("/raw",
uuid, metric)+"?"+qp.Encode(), nil, nil)
if err != nil {
return nil, err
}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r.Data, nil
}
// WriteRaw writes raw IRONdb data to a node.
func (sc *SnowthClient) WriteRaw(data io.Reader,
fb bool, dataPoints uint64,
nodes ...*SnowthNode,
) (*IRONdbPutResponse, error) {
return sc.WriteRawContext(context.Background(), data, fb, dataPoints,
nodes...)
}
// WriteRawContext is the context aware version of WriteRaw.
func (sc *SnowthClient) WriteRawContext(ctx context.Context,
data io.Reader, fb bool, dataPoints uint64,
nodes ...*SnowthNode,
) (*IRONdbPutResponse, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
hdrs := http.Header{
"X-Snowth-Datapoints": {strconv.FormatUint(dataPoints, 10)},
}
if fb { // is flatbuffer?
hdrs["Content-Type"] = []string{MetriclistFlatbufferContentType}
}
body, _, err := sc.DoRequestContext(ctx, node, "POST", "/raw", data, hdrs)
if err != nil {
return nil, err
}
r := &IRONdbPutResponse{}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r, nil
}
// WriteRawMetricList writes raw IRONdb data to a node with FlatBuffers.
func (sc *SnowthClient) WriteRawMetricList(metricList *noit.MetricListT,
builder *flatbuffers.Builder,
nodes ...*SnowthNode,
) (*IRONdbPutResponse, error) {
return sc.WriteRawMetricListContext(context.Background(),
metricList, builder, nodes...)
}
// WriteRawMetricListContext is the context aware version of WriteRawMetricList.
func (sc *SnowthClient) WriteRawMetricListContext(ctx context.Context,
metricList *noit.MetricListT, builder *flatbuffers.Builder,
nodes ...*SnowthNode,
) (*IRONdbPutResponse, error) {
if metricList == nil {
return nil, fmt.Errorf("metric list cannot be nil")
}
datapoints := uint64(len(metricList.Metrics))
if datapoints == 0 {
return nil, fmt.Errorf("metric list cannot be empty")
}
if builder == nil {
builder = flatbuffers.NewBuilder(1024)
} else {
builder.Reset()
}
offset := noit.MetricListPack(builder, metricList)
builder.FinishWithFileIdentifier(offset, []byte("CIML"))
reader := bytes.NewReader(builder.FinishedBytes())
return sc.WriteRawContext(ctx, reader, true, datapoints, nodes...)
}