-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
serialize_series.go
188 lines (163 loc) · 4.29 KB
/
serialize_series.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
package common
import (
"fmt"
"protocol"
"regexp"
)
var (
TRUE = true
FALSE = false
)
type TimePrecision int
const (
MicrosecondPrecision TimePrecision = iota
MillisecondPrecision
SecondPrecision
)
var VALID_TABLE_NAMES *regexp.Regexp
func init() {
var err error
VALID_TABLE_NAMES, err = regexp.Compile("^[a-zA-Z][a-zA-Z0-9._-]*$")
if err != nil {
panic(err)
}
}
func removeField(fields []string, name string) []string {
index := -1
for idx, field := range fields {
if field == name {
index = idx
break
}
}
if index == -1 {
return fields
}
return append(fields[:index], fields[index+1:]...)
}
func removeTimestampFieldDefinition(fields []string) []string {
fields = removeField(fields, "time")
return removeField(fields, "sequence_number")
}
type ApiSeries interface {
GetName() string
GetColumns() []string
GetPoints() [][]interface{}
}
func ConvertToDataStoreSeries(s ApiSeries, precision TimePrecision) (*protocol.Series, error) {
if !VALID_TABLE_NAMES.MatchString(s.GetName()) {
return nil, fmt.Errorf("%s is not a valid series name", s.GetName())
}
points := []*protocol.Point{}
for _, point := range s.GetPoints() {
values := []*protocol.FieldValue{}
var timestamp *int64
var sequence *uint64
for idx, field := range s.GetColumns() {
value := point[idx]
if field == "time" {
switch value.(type) {
case float64:
_timestamp := int64(value.(float64))
switch precision {
case SecondPrecision:
_timestamp *= 1000
fallthrough
case MillisecondPrecision:
_timestamp *= 1000
}
timestamp = &_timestamp
continue
default:
return nil, fmt.Errorf("time field must be float but is %T (%v)", value, value)
}
}
if field == "sequence_number" {
switch value.(type) {
case float64:
_sequenceNumber := uint64(value.(float64))
sequence = &_sequenceNumber
continue
default:
return nil, fmt.Errorf("sequence_number field must be float but is %T (%v)", value, value)
}
}
switch v := value.(type) {
case string:
values = append(values, &protocol.FieldValue{StringValue: &v})
case float64:
if i := int64(v); float64(i) == v {
values = append(values, &protocol.FieldValue{Int64Value: &i})
} else {
values = append(values, &protocol.FieldValue{DoubleValue: &v})
}
case bool:
values = append(values, &protocol.FieldValue{BoolValue: &v})
case nil:
values = append(values, &protocol.FieldValue{IsNull: &TRUE})
default:
// if we reached this line then the dynamic type didn't match
return nil, fmt.Errorf("Unknown type %T", value)
}
}
points = append(points, &protocol.Point{
Values: values,
Timestamp: timestamp,
SequenceNumber: sequence,
})
}
fields := removeTimestampFieldDefinition(s.GetColumns())
series := &protocol.Series{
Name: protocol.String(s.GetName()),
Fields: fields,
Points: points,
}
return series, nil
}
// takes a slice of protobuf series and convert them to the format
// that the http api expect
func SerializeSeries(memSeries map[string]*protocol.Series, precision TimePrecision) []*SerializedSeries {
serializedSeries := []*SerializedSeries{}
for _, series := range memSeries {
includeSequenceNumber := true
if len(series.Points) > 0 && series.Points[0].SequenceNumber == nil {
includeSequenceNumber = false
}
columns := []string{"time"}
if includeSequenceNumber {
columns = append(columns, "sequence_number")
}
for _, field := range series.Fields {
columns = append(columns, field)
}
points := [][]interface{}{}
for _, row := range series.Points {
timestamp := *row.GetTimestampInMicroseconds()
switch precision {
case SecondPrecision:
timestamp /= 1000
fallthrough
case MillisecondPrecision:
timestamp /= 1000
}
rowValues := []interface{}{timestamp}
if includeSequenceNumber {
rowValues = append(rowValues, *row.SequenceNumber)
}
for _, value := range row.Values {
if value == nil {
rowValues = append(rowValues, nil)
continue
}
rowValues = append(rowValues, value.GetValue())
}
points = append(points, rowValues)
}
serializedSeries = append(serializedSeries, &SerializedSeries{
Name: *series.Name,
Columns: columns,
Points: points,
})
}
return serializedSeries
}