forked from arangodb-helper/arangodb-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arangodb.go
177 lines (162 loc) · 4.81 KB
/
arangodb.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
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package main
import (
"context"
_ "net/http/pprof"
driver "github.com/arangodb/go-driver"
)
// StatisticsDescription is the JSON representation of the result of an _admin/statistics-description call.
type StatisticsDescription struct {
Groups []StatisticGroup `json:"groups"`
Figures []StatisticFigure `json:"figures"`
}
// StatisticGroup describes a group of statistics.
type StatisticGroup struct {
Group string `json:"group"`
Name string `json:"name"`
Description string `json:"description"`
}
// StatisticFigure describes a single statistic.
type StatisticFigure struct {
Group string `json:"group"`
Identifier string `json:"identifier"`
Name string `json:"name"`
Description string `json:"description"`
Type FigureType `json:"type"`
Units string `json:"unit,omitempty"`
Cuts []float64 `json:"cuts,omitempty"`
}
// FigureType is a strongly typed type of statistic.
type FigureType string
const (
FigureTypeAccumulated FigureType = "accumulated"
FigureTypeCurrent FigureType = "current"
FigureTypeDistribution FigureType = "distribution"
)
// Statistics is a stringly typed map containing statistic values
type Statistics map[string]interface{}
// GetGroup returns the statistics for the given group.
// If not found, nil is returned
func (s Statistics) GetGroup(group string) Statistics {
entry, ok := s[group]
if !ok {
return nil
}
result, ok := entry.(map[string]interface{})
if !ok {
return nil
}
return result
}
// GetFloat returns a float value for a statistic with given key.
// If not found, or the value could not be converted to a float, false is returned.
func (s Statistics) GetFloat(key string) (float64, bool) {
raw, ok := s[key]
if !ok {
return 0.0, false
}
if f, ok := raw.(float64); ok {
return f, true
}
if f, ok := raw.(int64); ok {
return float64(f), true
}
return 0.0, false
}
// GetCounts returns an int64 array value for a statistic with given key.
// If not found, or the value could not be converted to an int64 array, false is returned.
func (s Statistics) GetCounts(key string) ([]int64, bool) {
raw, ok := s[key]
if !ok {
return nil, false
}
if rawSlice, ok := raw.([]interface{}); ok {
result := make([]int64, len(rawSlice))
for i, x := range rawSlice {
if v, ok := x.(int64); ok {
result[i] = v
} else if v, ok := x.(int); ok {
result[i] = int64(v)
} else if v, ok := x.(float64); ok {
result[i] = int64(v)
} else {
return nil, false
}
}
return result, true
}
return nil, false
}
// GetInt returns an int value for a statistic with given key.
// If not found, or the value could not be converted to an int, false is returned.
func (s Statistics) GetInt(key string) (int64, bool) {
raw, ok := s[key]
if !ok {
return 0.0, false
}
if i, ok := raw.(int64); ok {
return i, true
}
if i, ok := raw.(int); ok {
return int64(i), true
}
return 0, false
}
// GetStatistics requests the statistics values from the given connection.
func GetStatistics(ctx context.Context, conn driver.Connection) (Statistics, error) {
req, err := conn.NewRequest("GET", "_admin/statistics")
if err != nil {
return nil, maskAny(err)
}
resp, err := conn.Do(ctx, req)
if err != nil {
return nil, maskAny(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, maskAny(err)
}
var result Statistics
if err := resp.ParseBody("", &result); err != nil {
return nil, maskAny(err)
}
return result, nil
}
// GetStatisticsDescription requests the statistics description from the given connection.
func GetStatisticsDescription(ctx context.Context, conn driver.Connection) (StatisticsDescription, error) {
req, err := conn.NewRequest("GET", "_admin/statistics-description")
if err != nil {
return StatisticsDescription{}, maskAny(err)
}
resp, err := conn.Do(ctx, req)
if err != nil {
return StatisticsDescription{}, maskAny(err)
}
if err := resp.CheckStatus(200); err != nil {
return StatisticsDescription{}, maskAny(err)
}
var result StatisticsDescription
if err := resp.ParseBody("", &result); err != nil {
return StatisticsDescription{}, maskAny(err)
}
return result, nil
}