forked from cloudscale-ch/cloudscale-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
104 lines (84 loc) · 2.58 KB
/
metrics.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
package cloudscale
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
const metricsBasePath = "v1/metrics"
type BucketMetricsRequest struct {
// Interpreted as midnight in the Europe/Zurich time zone at the start of
// the day represented by the day of the passed value in the UTC time zone.
Start time.Time
// Interpreted as midnight in the Europe/Zurich time zone at the end of
// the day represented by the day of the passed value in the UTC time zone.
End time.Time
BucketNames []string
ObjectsUserIDs []string
}
type BucketMetrics struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
Data []BucketMetricsData
}
type BucketMetricsDataSubject struct {
BucketName string `json:"name"`
ObjectsUserID string `json:"objects_user_id"`
}
type BucketMetricsIntervalUsage struct {
Requests int `json:"requests"`
ObjectCount int `json:"object_count"`
StorageBytes int `json:"storage_bytes"`
ReceivedBytes int `json:"received_bytes"`
SentBytes int `json:"sent_bytes"`
}
type BucketMetricsInterval struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
Usage BucketMetricsIntervalUsage `json:"usage"`
}
type BucketMetricsData struct {
Subject BucketMetricsDataSubject `json:"subject"`
TimeSeries []BucketMetricsInterval `json:"time_series"`
}
type MetricsService interface {
GetBucketMetrics(ctx context.Context, request *BucketMetricsRequest) (*BucketMetrics, error)
}
type MetricsServiceOperations struct {
client *Client
}
func encodeGetBucketParameters(request *BucketMetricsRequest) string {
builder := strings.Builder{}
separator := "?"
appendArg := func(name string, value string) {
builder.WriteString(separator)
builder.WriteString(name)
builder.WriteString("=")
builder.WriteString(url.QueryEscape(value))
separator = "&"
}
appendArg("start", request.Start.Format("2006-01-02"))
appendArg("end", request.End.Format("2006-01-02"))
for _, i := range request.BucketNames {
appendArg("bucket_name", i)
}
for _, i := range request.ObjectsUserIDs {
appendArg("objects_user_id", i)
}
return builder.String()
}
func (s MetricsServiceOperations) GetBucketMetrics(ctx context.Context, request *BucketMetricsRequest) (*BucketMetrics, error) {
path := fmt.Sprintf("%s/buckets%s", metricsBasePath, encodeGetBucketParameters(request))
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
result := new(BucketMetrics)
err = s.client.Do(ctx, req, result)
if err != nil {
return nil, err
}
return result, nil
}