-
Notifications
You must be signed in to change notification settings - Fork 21
/
bson_extract.go
130 lines (107 loc) · 3.32 KB
/
bson_extract.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
package ftdc
import (
"time"
"github.com/evergreen-ci/birch"
"github.com/evergreen-ci/birch/bsontype"
"github.com/mongodb/ftdc/util"
"github.com/pkg/errors"
)
////////////////////////////////////////////////////////////////////////
//
// Helpers for encoding values from birch documents
type extractedMetrics struct {
values []*birch.Value
types []bsontype.Type
ts time.Time
}
func extractMetricsFromDocument(doc *birch.Document) (extractedMetrics, error) {
metrics := extractedMetrics{}
iter := doc.Iterator()
var (
err error
data extractedMetrics
)
catcher := util.NewCatcher()
for iter.Next() {
data, err = extractMetricsFromValue(iter.Element().Value())
catcher.Add(err)
metrics.values = append(metrics.values, data.values...)
metrics.types = append(metrics.types, data.types...)
if metrics.ts.IsZero() {
metrics.ts = data.ts
}
}
catcher.Add(iter.Err())
if metrics.ts.IsZero() {
metrics.ts = time.Now()
}
return metrics, catcher.Resolve()
}
func extractMetricsFromArray(array *birch.Array) (extractedMetrics, error) {
metrics := extractedMetrics{}
var (
err error
data extractedMetrics
)
catcher := util.NewCatcher()
iter := array.Iterator()
for iter.Next() {
data, err = extractMetricsFromValue(iter.Value())
catcher.Add(err)
metrics.values = append(metrics.values, data.values...)
metrics.types = append(metrics.types, data.types...)
if metrics.ts.IsZero() {
metrics.ts = data.ts
}
}
catcher.Add(iter.Err())
return metrics, catcher.Resolve()
}
func extractMetricsFromValue(val *birch.Value) (extractedMetrics, error) {
metrics := extractedMetrics{}
var err error
btype := val.Type()
switch btype {
case bsontype.Array:
metrics, err = extractMetricsFromArray(val.MutableArray())
err = errors.WithStack(err)
case bsontype.EmbeddedDocument:
metrics, err = extractMetricsFromDocument(val.MutableDocument())
err = errors.WithStack(err)
case bsontype.Boolean:
if val.Boolean() {
metrics.values = append(metrics.values, birch.VC.Int64(1))
} else {
metrics.values = append(metrics.values, birch.VC.Int64(0))
}
metrics.types = append(metrics.types, bsontype.Boolean)
case bsontype.Double:
metrics.values = append(metrics.values, val)
metrics.types = append(metrics.types, bsontype.Double)
case bsontype.Int32:
metrics.values = append(metrics.values, birch.VC.Int64(int64(val.Int32())))
metrics.types = append(metrics.types, bsontype.Int32)
case bsontype.Int64:
metrics.values = append(metrics.values, val)
metrics.types = append(metrics.types, bsontype.Int64)
case bsontype.DateTime:
metrics.values = append(metrics.values, birch.VC.Int64(epochMs(val.Time())))
metrics.types = append(metrics.types, bsontype.DateTime)
metrics.ts = val.Time()
case bsontype.Timestamp:
t, i := val.Timestamp()
metrics.values = append(metrics.values, birch.VC.Int64(int64(t)), birch.VC.Int64(int64(i)))
metrics.types = append(metrics.types, bsontype.Timestamp, bsontype.Timestamp)
}
return metrics, err
}
func extractDelta(current *birch.Value, previous *birch.Value) (int64, error) {
switch current.Type() {
case bsontype.Double:
return normalizeFloat(current.Double()) - normalizeFloat(previous.Double()), nil
case bsontype.Int64:
return current.Int64() - previous.Int64(), nil
default:
return 0, errors.Errorf("invalid type %s", current.Type())
}
}