@@ -2,6 +2,9 @@ package cloudflare
2
2
3
3
import (
4
4
"encoding/json"
5
+ "net/url"
6
+ "strings"
7
+ "time"
5
8
6
9
"github.com/pkg/errors"
7
10
)
@@ -18,6 +21,31 @@ type VirtualDNS struct {
18
21
ModifiedOn string `json:"modified_on"`
19
22
}
20
23
24
+ // VirtualDNSAnalyticsMetrics respresents a group of aggregated Virtual DNS metrics.
25
+ type VirtualDNSAnalyticsMetrics struct {
26
+ QueryCount * int64 `json:"queryCount"`
27
+ UncachedCount * int64 `json:"uncachedCount"`
28
+ StaleCount * int64 `json:"staleCount"`
29
+ ResponseTimeAvg * float64 `json:"responseTimeAvg"`
30
+ ResponseTimeMedian * float64 `json:"responseTimeMedian"`
31
+ ResponseTime90th * float64 `json:"responseTime90th"`
32
+ ResponseTime99th * float64 `json:"responseTime99th"`
33
+ }
34
+
35
+ // VirtualDNSAnalytics represents a set of aggregated Virtual DNS metrics.
36
+ // TODO: Add the queried data and not only the aggregated values.
37
+ type VirtualDNSAnalytics struct {
38
+ Totals VirtualDNSAnalyticsMetrics `json:"totals"`
39
+ Min VirtualDNSAnalyticsMetrics `json:"min"`
40
+ Max VirtualDNSAnalyticsMetrics `json:"max"`
41
+ }
42
+
43
+ type VirtualDNSUserAnalyticsOptions struct {
44
+ Metrics []string
45
+ Since * time.Time
46
+ Until * time.Time
47
+ }
48
+
21
49
// VirtualDNSResponse represents a Virtual DNS response.
22
50
type VirtualDNSResponse struct {
23
51
Response
@@ -30,6 +58,12 @@ type VirtualDNSListResponse struct {
30
58
Result []* VirtualDNS `json:"result"`
31
59
}
32
60
61
+ // VirtualDNSAnalyticsResponse represents a Virtual DNS analytics response.
62
+ type VirtualDNSAnalyticsResponse struct {
63
+ Response
64
+ Result VirtualDNSAnalytics `json:"result"`
65
+ }
66
+
33
67
// CreateVirtualDNS creates a new Virtual DNS cluster.
34
68
//
35
69
// API reference: https://api.cloudflare.com/#virtual-dns-users--create-a-virtual-dns-cluster
@@ -123,3 +157,34 @@ func (api *API) DeleteVirtualDNS(virtualDNSID string) error {
123
157
124
158
return nil
125
159
}
160
+
161
+ // encode encodes non-nil fields into URL encoded form.
162
+ func (o VirtualDNSUserAnalyticsOptions ) encode () string {
163
+ v := url.Values {}
164
+ if o .Since != nil {
165
+ v .Set ("since" , (* o .Since ).UTC ().Format (time .RFC3339 ))
166
+ }
167
+ if o .Until != nil {
168
+ v .Set ("until" , (* o .Until ).UTC ().Format (time .RFC3339 ))
169
+ }
170
+ if o .Metrics != nil {
171
+ v .Set ("metrics" , strings .Join (o .Metrics , "," ))
172
+ }
173
+ return v .Encode ()
174
+ }
175
+
176
+ func (api * API ) VirtualDNSUserAnalytics (virtualDNSID string , o VirtualDNSUserAnalyticsOptions ) (VirtualDNSAnalytics , error ) {
177
+ uri := "/user/virtual_dns/" + virtualDNSID + "/dns_analytics/report?" + o .encode ()
178
+ res , err := api .makeRequest ("GET" , uri , nil )
179
+ if err != nil {
180
+ return VirtualDNSAnalytics {}, errors .Wrap (err , errMakeRequestError )
181
+ }
182
+
183
+ response := VirtualDNSAnalyticsResponse {}
184
+ err = json .Unmarshal (res , & response )
185
+ if err != nil {
186
+ return VirtualDNSAnalytics {}, errors .Wrap (err , errUnmarshalError )
187
+ }
188
+
189
+ return response .Result , nil
190
+ }
0 commit comments