-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.go
332 lines (283 loc) · 9.02 KB
/
metric.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Metric definition https://api.ilert.com/api-docs/#tag/Metrics
type Metric struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
AggregationType string `json:"aggregationType"`
DisplayType string `json:"displayType"`
InterpolateGaps bool `json:"interpolateGaps,omitempty"`
LockYAxisMax float64 `json:"lockYAxisMax,omitempty"`
LockYAxisMin float64 `json:"lockYAxisMin,omitempty"`
MouseOverDecimal float64 `json:"mouseOverDecimal,omitempty"`
ShowValuesOnMouseOver bool `json:"showValuesOnMouseOver,omitempty"`
Teams []TeamShort `json:"teams,omitempty"`
UnitLabel string `json:"unitLabel,omitempty"`
IntegrationKey string `json:"integrationKey,omitempty"`
Metadata *MetricProviderMetadata `json:"metadata,omitempty"`
DataSource *MetricDataSource `json:"dataSource,omitempty"`
}
// MetricProviderMetadata defines provider metadata for the metric
type MetricProviderMetadata struct {
Query string `json:"query,omitempty"` // used for Datadog, Prometheus
}
// MetricAggregationType defines aggregation type for the metric
var MetricAggregationType = struct {
Average string
Sum string
Minimum string
Maximum string
Last string
}{
Average: "AVG",
Sum: "SUM",
Minimum: "MIN",
Maximum: "MAX",
Last: "LAST",
}
// MetricAggregationType defines aggregation type list
var MetricAggregationTypeAll = []string{
MetricAggregationType.Average,
MetricAggregationType.Sum,
MetricAggregationType.Minimum,
MetricAggregationType.Maximum,
MetricAggregationType.Last,
}
// MetricDisplayType defines display type for the metric
var MetricDisplayType = struct {
Graph string
Single string
}{
Graph: "GRAPH",
Single: "SINGLE",
}
// MetricDisplayType defines display type list
var MetricDisplayTypeAll = []string{
MetricDisplayType.Graph,
MetricDisplayType.Single,
}
// CreateMetricInput represents the input of a CreateMetric operation.
type CreateMetricInput struct {
_ struct{}
Metric *Metric
}
// CreateMetricOutput represents the output of a CreateMetric operation.
type CreateMetricOutput struct {
_ struct{}
Metric *Metric
}
// CreateMetric creates a new metric. https://api.ilert.com/api-docs/#tag/Metrics/paths/~1metrics/post
func (c *Client) CreateMetric(input *CreateMetricInput) (*CreateMetricOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Metric == nil {
return nil, errors.New("metric input is required")
}
if input.Metric.Metadata != nil && input.Metric.DataSource == nil {
return nil, errors.New("data source id is required when setting provider metadata")
}
if input.Metric.DataSource != nil && input.Metric.Metadata == nil {
return nil, errors.New("provider metadata is required when setting metric data source")
}
resp, err := c.httpClient.R().SetBody(input.Metric).Post(apiRoutes.metrics)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
metric := &Metric{}
err = json.Unmarshal(resp.Body(), metric)
if err != nil {
return nil, err
}
return &CreateMetricOutput{Metric: metric}, nil
}
// GetMetricsInput represents the input of a GetMetrics operation.
type GetMetricsInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
// Default: 0
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Default: 10, Maximum: 25 or 100 without include
MaxResults *int
// describes optional properties that should be included in the response
// possible values: "dataSource", "integrationKey"
Include []*string
}
// GetMetricsOutput represents the output of a GetMetrics operation.
type GetMetricsOutput struct {
_ struct{}
Metrics []*Metric
}
// GetMetrics lists existing metrics. https://api.ilert.com/api-docs/#tag/Metrics/paths/~1metrics/get
func (c *Client) GetMetrics(input *GetMetricsInput) (*GetMetricsOutput, error) {
if input == nil {
input = &GetMetricsInput{}
}
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
} else {
q.Add("start-index", "0")
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
} else {
q.Add("max-results", "10")
}
for _, include := range input.Include {
q.Add("include", *include)
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.metrics, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metrics := make([]*Metric, 0)
err = json.Unmarshal(resp.Body(), &metrics)
if err != nil {
return nil, err
}
return &GetMetricsOutput{Metrics: metrics}, nil
}
// GetMetricInput represents the input of a GetMetric operation.
type GetMetricInput struct {
_ struct{}
MetricID *int64
}
// GetMetricOutput represents the output of a GetMetric operation.
type GetMetricOutput struct {
_ struct{}
Metric *Metric
}
// GetMetric gets a metric by id. https://api.ilert.com/api-docs/#tag/Metrics/paths/~1metrics~1{id}/get
func (c *Client) GetMetric(input *GetMetricInput) (*GetMetricOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricID == nil {
return nil, errors.New("metric id is required")
}
var url = fmt.Sprintf("%s/%d", apiRoutes.metrics, *input.MetricID)
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metric := &Metric{}
err = json.Unmarshal(resp.Body(), metric)
if err != nil {
return nil, err
}
return &GetMetricOutput{Metric: metric}, nil
}
// SearchMetricInput represents the input of a SearchMetric operation.
type SearchMetricInput struct {
_ struct{}
MetricName *string
}
// SearchMetricOutput represents the output of a SearchMetric operation.
type SearchMetricOutput struct {
_ struct{}
Metric *Metric
}
// SearchMetric gets the metric with specified name.
func (c *Client) SearchMetric(input *SearchMetricInput) (*SearchMetricOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricName == nil {
return nil, errors.New("metric name is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/name/%s", apiRoutes.metrics, *input.MetricName))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metric := &Metric{}
err = json.Unmarshal(resp.Body(), metric)
if err != nil {
return nil, err
}
return &SearchMetricOutput{Metric: metric}, nil
}
// UpdateMetricInput represents the input of a UpdateMetric operation.
type UpdateMetricInput struct {
_ struct{}
MetricID *int64
Metric *Metric
}
// UpdateMetricOutput represents the output of a UpdateMetric operation.
type UpdateMetricOutput struct {
_ struct{}
Metric *Metric
}
// UpdateMetric updates the specific metric. https://api.ilert.com/api-docs/#tag/Metrics/paths/~1metrics~1{id}/put
func (c *Client) UpdateMetric(input *UpdateMetricInput) (*UpdateMetricOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricID == nil {
return nil, errors.New("metric id is required")
}
if input.Metric == nil {
return nil, errors.New("metric input is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.metrics, *input.MetricID)
resp, err := c.httpClient.R().SetBody(input.Metric).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metric := &Metric{}
err = json.Unmarshal(resp.Body(), metric)
if err != nil {
return nil, err
}
return &UpdateMetricOutput{Metric: metric}, nil
}
// DeleteMetricInput represents the input of a DeleteMetric operation.
type DeleteMetricInput struct {
_ struct{}
MetricID *int64
}
// DeleteMetricOutput represents the output of a DeleteMetric operation.
type DeleteMetricOutput struct {
_ struct{}
}
// DeleteMetric deletes the specified metric. https://api.ilert.com/api-docs/#tag/Metrics/paths/~1metrics~1{id}/delete
func (c *Client) DeleteMetric(input *DeleteMetricInput) (*DeleteMetricOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricID == nil {
return nil, errors.New("metric id is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.metrics, *input.MetricID)
resp, err := c.httpClient.R().Delete(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteMetricOutput{}, nil
}