-
Notifications
You must be signed in to change notification settings - Fork 3
/
stats.go
149 lines (118 loc) · 2.77 KB
/
stats.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
package gosnowth
import (
"context"
"fmt"
)
// GetStats retrieves the metrics about the status of an IRONdb node.
func (sc *SnowthClient) GetStats(nodes ...*SnowthNode) (*Stats, error) {
return sc.GetStatsContext(context.Background(), nodes...)
}
// GetStatsContext is the context aware version of GetStats.
func (sc *SnowthClient) GetStatsContext(ctx context.Context,
nodes ...*SnowthNode,
) (*Stats, 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")
}
r := &Stats{}
body, _, err := sc.DoRequestContext(ctx, node, "GET", "/stats.json", 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, nil
}
// GetStatsNodeContext gets the status metrics for a single specified node.
func (sc *SnowthClient) GetStatsNodeContext(ctx context.Context,
node *SnowthNode,
) (*Stats, error) {
r := &Stats{}
body, _, _, err := sc.do(ctx, node, "GET", "/stats.json", 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, nil
}
// Stats values represent a collection of metric data describing the status
// of an IRONdb node.
type Stats map[string]interface{}
// Identity returns the identity string value from a node state value.
func (ns *Stats) Identity() string {
if ns == nil {
return ""
}
m, ok := (*ns)["identity"].(map[string]interface{})
if !ok {
return ""
}
id, ok := m["_value"].(string)
if !ok {
return ""
}
return id
}
// SemVer returns the semantic version string value from a node state value.
func (ns *Stats) SemVer() string {
if ns == nil {
return ""
}
m, ok := (*ns)["semver"].(map[string]interface{})
if !ok {
return ""
}
ver, ok := m["_value"].(string)
if !ok {
return ""
}
return ver
}
// CurrentTopology returns the current topology string value from a node state
// value.
func (ns *Stats) CurrentTopology() string {
if ns == nil {
return ""
}
t, ok := (*ns)["topology"].(map[string]interface{})
if !ok {
return ""
}
m, ok := t["current"].(map[string]interface{})
if !ok {
return ""
}
current, ok := m["_value"].(string)
if !ok {
return ""
}
return current
}
// NextTopology returns the next topology string value from a node state value.
func (ns *Stats) NextTopology() string {
if ns == nil {
return ""
}
t, ok := (*ns)["topology"].(map[string]interface{})
if !ok {
return ""
}
m, ok := t["next"].(map[string]interface{})
if !ok {
return ""
}
next, ok := m["_value"].(string)
if !ok {
return ""
}
return next
}