-
Notifications
You must be signed in to change notification settings - Fork 3
/
metering.go
192 lines (177 loc) · 6.05 KB
/
metering.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
189
190
191
192
package main
import (
"encoding/json"
"fmt"
"math"
"sort"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gitlab.ops.mist.io/mistio/openapi-cli-generator/cli"
)
type resultItem struct {
Metric map[string]string `json:"metric"`
Value []interface{} `json:"value"`
}
type promqlResponse struct {
Data struct {
DataPromql struct {
Result []resultItem `json:"result"`
} `json:"data"`
} `json:"data"`
Metadata map[string]interface{}
}
func formatMeteringData(metricsSet map[string]string, resourceMetrics map[string]map[string]string, resourceNames map[string]string) {
metricsList := []string{}
for metric := range metricsSet {
metricsList = append(metricsList, metric)
}
sort.Strings(metricsList)
resources := make([]string, 0, len(resourceMetrics))
for resource := range resourceMetrics {
resources = append(resources, resource)
}
sort.Strings(resources)
data := make(map[string][]interface{})
for _, resource := range resources {
resourceData := make(map[string]string)
for _, metric := range metricsList {
if _, ok := resourceData["machine_id"]; !ok {
resourceData["machine_id"] = resource
resourceData["name"] = resourceNames[resource]
}
resourceData[metric] = resourceMetrics[resource][metric]
}
if _, ok := data["data"]; !ok {
data["data"] = make([]interface{}, 0)
}
data["data"] = append(data["data"], resourceData)
}
metricSums := make(map[string]float64)
for _, resourceData := range data["data"] {
for _, metric := range metricsList {
valueString, ok := resourceData.((map[string]string))[metric]
if !ok || valueString == "" {
continue
}
value, err := strconv.ParseFloat(valueString, 64)
if err != nil {
fmt.Printf("metric: %s, value: %s\n", metric, valueString)
fmt.Println(err)
} else {
metricSums[metric] += value
}
}
}
sums := make([]string, len(metricsList))
for i, metric := range metricsList {
sums[i] = fmt.Sprintf("%f", metricSums[metric])
}
if err := cli.Formatter.Format(data, &viper.Viper{}, cli.CLIOutputOptions{append([]string{"name"}, metricsList...), append([]string{"machine_id", "name"}, metricsList...), append([]string{"TOTAL"}, sums...), append([]string{"TOTAL", ""}, sums...), map[string]string{}}); err != nil {
logger.Fatalf("Formatting failed: %s", err.Error())
}
}
func parseTime(s string) (time.Time, error) {
if t, err := strconv.ParseFloat(s, 64); err == nil {
s, ns := math.Modf(t)
return time.Unix(int64(s), int64(ns*float64(time.Second))).UTC(), nil
}
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
return t, nil
}
return time.Time{}, errors.Errorf("cannot parse %q to a valid timestamp", s)
}
func getResourceNamesIDMap(search string) map[string]string {
resourceNames := make(map[string]string)
paramsListResources := viper.New()
paramsListResources.Set("search", search)
_, decoded, _, err := MistApiV2ListMachines(paramsListResources)
if err != nil {
logger.Fatalf("Error calling operation: %s", err.Error())
}
for _, item := range decoded["data"].([]interface{}) {
resourceNames[item.(map[string]interface{})["id"].(string)] = item.(map[string]interface{})["name"].(string)
}
_, decoded, _, err = MistApiV2ListVolumes(paramsListResources)
if err != nil {
logger.Fatalf("Error calling operation: %s", err.Error())
}
for _, item := range decoded["data"].([]interface{}) {
resourceNames[item.(map[string]interface{})["id"].(string)] = item.(map[string]interface{})["name"].(string)
}
return resourceNames
}
func mapResourceNamesWithMetrics(response promqlResponse, search string) (map[string]string, map[string]map[string]string, map[string]string) {
metricsNameSet := make(map[string]string)
resourceIDToMetricMap := make(map[string]map[string]string)
resourceIDToNameMap := getResourceNamesIDMap(search)
for _, item := range response.Data.DataPromql.Result {
resourceID, ok := item.Metric["machine_id"]
if !ok {
resourceID, ok = item.Metric["volume_id"]
if !ok {
continue
}
}
if resourceIDToMetricMap[resourceID] == nil {
resourceIDToMetricMap[resourceID] = make(map[string]string)
}
if item.Value != nil {
resourceIDToMetricMap[resourceID][item.Metric["__name__"]] = item.Value[1].(string)
}
metricsNameSet[item.Metric["__name__"]] = item.Metric["value_type"]
}
return metricsNameSet, resourceIDToMetricMap, resourceIDToNameMap
}
func getMeteringData(dtStart, dtEnd, search, queryTemplate string) (map[string]string, map[string]map[string]string, map[string]string) {
paramsGetDatapoints := viper.New()
paramsGetDatapoints.Set("time", dtEnd)
paramsGetDatapoints.Set("search", search)
dtStartTime, _ := parseTime(dtStart)
dtEndTime, _ := parseTime(dtEnd)
timeRange := int((dtEndTime.Sub(dtStartTime)).Seconds())
query := fmt.Sprintf(queryTemplate, timeRange)
_, decoded, _, err := MistApiV2GetDatapoints(query, paramsGetDatapoints)
if err != nil {
logger.Fatalf("Error calling operation: %s", err.Error())
}
rawResponse, err := json.Marshal(decoded)
if err != nil {
fmt.Println("error:", err)
}
var response promqlResponse
err = json.Unmarshal(rawResponse, &response)
if err != nil {
fmt.Println("error:", err)
}
return mapResourceNamesWithMetrics(response, search)
}
func calculateDiffs(resourceMetricsStart map[string]map[string]string, resourceMetricsEnd map[string]map[string]string, metricsSet map[string]string) map[string]map[string]string {
for resourceID, metrics := range resourceMetricsEnd {
for metric, valueEnd := range metrics {
if metricsSet[metric] != "counter" {
continue
}
if _, ok := resourceMetricsStart[resourceID]; !ok {
continue
}
if _, ok := resourceMetricsStart[resourceID][metric]; !ok {
continue
}
valueStart := resourceMetricsStart[resourceID][metric]
valueStartFloat, err := strconv.ParseFloat(valueStart, 64)
if err != nil {
fmt.Println(err)
continue
}
valueEndFloat, err := strconv.ParseFloat(valueEnd, 64)
if err != nil {
fmt.Println(err)
continue
}
resourceMetricsEnd[resourceID][metric] = fmt.Sprintf("%f", valueEndFloat-valueStartFloat)
}
}
return resourceMetricsEnd
}