/
api_metricdb.go
129 lines (116 loc) · 3.45 KB
/
api_metricdb.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
package apstra
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
const (
apiUrlMetricdbMetric = "/api/metricdb/metric"
apiUrlMetricdbQuery = "/api/metricdb/query"
aggrWriterSplit = "_aggr_"
aggrWriterRegex = "^.+" + aggrWriterSplit + "[0-9]+$"
)
// metricdbMetricResponse is generated by GET to apstra apiUrlMetricdbMetric API endpoint
type metricdbMetricResponse struct {
Items []MetricdbMetric `json:"items"`
}
type MetricdbMetric struct {
Application string `json:"application"`
Namespace string `json:"namespace"`
Name string `json:"name"`
}
type metricdbQuery struct {
Application string `json:"application"` // required
Namespace string `json:"namespace"` // required
Name string `json:"name"` // required
BeginTime time.Time `json:"begin_time"` // required
EndTime time.Time `json:"end_time"` // required
//Filter string `json:"filter"`
//Paging struct {
// PageNumber int `json:"page_number"`
// EndTime time.Time `json:"end_time"`
// PageSize int `json:"page_size"`
// BeginTime time.Time `json:"begin_time"`
//} `json:"paging"`
//Caching struct {
// Clear bool `json:"clear"`
// KeepAlive bool `json:"keep_alive"`
// Timeout int `json:"timeout"`
//} `json:"caching"`
//Aggregation struct {
// Metrics struct {
// AdditionalProp1 string `json:"additionalProp1"`
// AdditionalProp2 string `json:"additionalProp2"`
// AdditionalProp3 string `json:"additionalProp3"`
// } `json:"metrics"`
// Period int `json:"period"`
//} `json:"aggregation"`
}
type MetricDbQueryRequest struct {
metric MetricdbMetric
begin time.Time
end time.Time
}
type MetricDbQueryResponse struct {
Status struct {
TotalCount int `json:"total_count"`
BeginTime time.Time `json:"begin_time"`
EndTime time.Time `json:"end_time"`
ResultCode string `json:"result_code"`
LastTimestamp time.Time `json:"last_timestamp"`
} `json:"status"`
Items []json.RawMessage `json:"items"`
}
func (o *Client) getMetricdbMetrics(ctx context.Context) (*metricdbMetricResponse, error) {
apstraUrl, err := url.Parse(apiUrlMetricdbMetric)
if err != nil {
return nil, fmt.Errorf("error parsing url '%s' - %w", apiUrlVersion, err)
}
response := &metricdbMetricResponse{}
return response, o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
url: apstraUrl,
apiResponse: response,
})
}
func (o *Client) queryMetricdb(ctx context.Context, begin, end time.Time, metric MetricdbMetric) (*MetricDbQueryResponse, error) {
apstraUrl, err := url.Parse(apiUrlMetricdbQuery)
if err != nil {
return nil, fmt.Errorf("error parsing url '%s' - %w", apiUrlVersion, err)
}
response := &MetricDbQueryResponse{}
q := metricdbQuery{
Application: metric.Application,
Namespace: metric.Namespace,
Name: metric.Name,
BeginTime: begin,
EndTime: end,
}
return response, o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
url: apstraUrl,
apiInput: q,
apiResponse: response,
})
}
func useAggregation(in string) (bool, string, int, error) {
aggrWriter, err := regexp.MatchString(aggrWriterRegex, in)
if err != nil {
return false, "", 0, err
}
if !aggrWriter {
return false, in, 0, nil
}
split := strings.Split(in, aggrWriterSplit)
seconds, err := strconv.Atoi(split[1])
if err != nil {
return false, "", 0, err
}
return true, split[0], seconds, nil
}