-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbandwidth.go
129 lines (109 loc) · 4.19 KB
/
bandwidth.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 latitude
type BandwidthService interface {
TrafficQuota(opts *ListOptions) (*TrafficQuota, *Response, error)
TrafficConsumption(opts *ListOptions) (*TrafficConsumption, *Response, error)
}
// TrafficConsumption represents consumed traffic in regions
type TrafficConsumption struct {
FromDate int64 `json:"from_date"`
ToDate int64 `json:"to_date"`
TotalInboundGB int64 `json:"total_inbound_gb"`
TotalOutboundGB int64 `json:"total_outbound_gb"`
TotalInbound95thPercentileMbps float64 `json:"total_inbound_95th_percentile_mbps"`
TotalOutbound95thPercentileMbps float64 `json:"total_outbound_95th_percentile_mbps"`
Regions []TrafficRegion `json:"regions"`
}
type TrafficQuota struct {
QuotaPerProject []QuotaPerProject `json:"quota_per_project"`
}
type QuotaPerProject struct {
ProjectID string `json:"project_id"`
ProjectSlug string `json:"project_slug"`
BillingMethod string `json:"billing_method"`
QuotaPerRegion []QuotaPerRegion `json:"quota_per_region"`
}
type QuotaPerRegion struct {
RegionId string `json:"region_id"`
RegionSlug string `json:"region_slug"`
QuotaInTb QuotaDetail `json:"quota_in_tb"`
QuotaInMbps QuotaDetail `json:"quota_in_mbps"`
}
type QuotaDetail struct {
Granted int64 `json:"granted"`
Additional int64 `json:"additional"`
Total int64 `json:"total"`
}
type TrafficRegion struct {
RegionSlug string `json:"region_slug"`
Data []TrafficRegionData `json:"data"`
}
type TrafficRegionData struct {
Date string `json:"date"`
InboundGB int64 `json:"inbound_gb"`
OutboundGB int64 `json:"outbound_gb"`
AvgOutboundSpeedMbps float64 `json:"avg_outbound_speed_mbps"`
AvgInboundSpeedMbps float64 `json:"avg_inbound_speed_mbps"`
OutboundSpeedMbps float64 `json:"outbound_speed_mbps"`
InboundSpeedMbps float64 `json:"inbound_speed_mbps"`
}
// BandwidthServiceOp implements BandwidthService
type BandwidthServiceOp struct {
client requestDoer
}
type TrafficConsumptionResponse struct {
Data TrafficConsumptionData `json:"data"`
Meta meta `json:"meta"`
}
type TrafficQuotaResponse struct {
Data TrafficQuotaData `json:"data"`
Meta meta `json:"meta"`
}
type TrafficConsumptionData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes TrafficConsumption `json:"attributes"`
}
type TrafficQuotaData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes TrafficQuota `json:"attributes"`
}
// Flatten latitude API data structures
func NewFlatTrafficConsumption(t TrafficConsumptionData) TrafficConsumption {
return TrafficConsumption{
t.Attributes.FromDate,
t.Attributes.ToDate,
t.Attributes.TotalInboundGB,
t.Attributes.TotalOutboundGB,
t.Attributes.TotalInbound95thPercentileMbps,
t.Attributes.TotalOutbound95thPercentileMbps,
t.Attributes.Regions,
}
}
func NewFlatTrafficQuota(t TrafficQuotaData) TrafficQuota {
return TrafficQuota{
t.Attributes.QuotaPerProject,
}
}
// TrafficConsumption returns consumed traffic
func (u *BandwidthServiceOp) TrafficConsumption(opts *ListOptions) (*TrafficConsumption, *Response, error) {
trafficConsumptionResponse := new(TrafficConsumptionResponse)
apiPathQuery := opts.WithQuery("/traffic")
resp, err := u.client.DoRequest("GET", apiPathQuery, nil, trafficConsumptionResponse)
if err != nil {
return nil, resp, err
}
flatFlatTrafficConsumption := NewFlatTrafficConsumption(trafficConsumptionResponse.Data)
return &flatFlatTrafficConsumption, resp, err
}
// TrafficQuota returns purchased quota
func (u *BandwidthServiceOp) TrafficQuota(opts *ListOptions) (*TrafficQuota, *Response, error) {
trafficQuotaResponse := new(TrafficQuotaResponse)
apiPathQuery := opts.WithQuery("/traffic/quota")
resp, err := u.client.DoRequest("GET", apiPathQuery, nil, trafficQuotaResponse)
if err != nil {
return nil, resp, err
}
flatFlatTrafficQuota := NewFlatTrafficQuota(trafficQuotaResponse.Data)
return &flatFlatTrafficQuota, resp, err
}