| 1 |
package fastly |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
|
| 6 |
type Stats struct { |
| 7 |
Requests uint64 `mapstructure:"requests"` |
| 8 |
Hits uint64 `mapstructure:"hits"` |
| 9 |
HitsTime float64 `mapstructure:"hits_time"` |
| 10 |
Miss uint64 `mapstructure:"miss"` |
| 11 |
MissTime float64 `mapstructure:"miss_time"` |
| 12 |
Pass uint64 `mapstructure:"pass"` |
| 13 |
PassTime float64 `mapstructure:"pass_time"` |
| 14 |
Synth uint64 `mapstructure:"synth"` |
| 15 |
Errors uint64 `mapstructure:"errors"` |
| 16 |
Restarts uint64 `mapstructure:"restarts"` |
| 17 |
HitRatio float64 `mapstructure:"hit_ratio"` |
| 18 |
Bandwidth uint64 `mapstructure:"bandwidth"` |
| 19 |
RequestBodyBytes uint64 `mapstructure:"req_body_bytes"` |
| 20 |
RequestHeaderBytes uint64 `mapstructure:"req_header_bytes"` |
| 21 |
ResponseBodyBytes uint64 `mapstructure:"resp_body_bytes"` |
| 22 |
ResponseHeaderBytes uint64 `mapstructure:"resp_header_bytes"` |
| 23 |
BERequestBodyBytes uint64 `mapstructure:"bereq_body_bytes"` |
| 24 |
BERequestHeaderbytes uint64 `mapstructure:"bereq_header_bytes"` |
| 25 |
Uncachable uint64 `mapstructure:"uncachable"` |
| 26 |
Pipe uint64 `mapstructure:"pipe"` |
| 27 |
TLS uint64 `mapstructure:"tls"` |
| 28 |
TLSv10 uint64 `mapstructure:"tls_v10"` |
| 29 |
TLSv11 uint64 `mapstructure:"tls_v11"` |
| 30 |
TLSv12 uint64 `mapstructure:"tls_v12"` |
| 31 |
TLSv13 uint64 `mapstructure:"tls_v13"` |
| 32 |
Shield uint64 `mapstructure:"shield"` |
| 33 |
ShieldResponseBodyBytes uint64 `mapstructure:"shield_resp_body_bytes"` |
| 34 |
ShieldResponseHeaderBytes uint64 `mapstructure:"shield_resp_header_bytes"` |
| 35 |
IPv6 uint64 `mapstructure:"ipv6"` |
| 36 |
OTFP uint64 `mapstructure:"otfp"` |
| 37 |
Video uint64 `mapstructure:"video"` |
| 38 |
PCI uint64 `mapstructure:"pci"` |
| 39 |
Log uint64 `mapstructure:"log"` |
| 40 |
HTTP2 uint64 `mapstructure:"http2"` |
| 41 |
WAFLogged uint64 `mapstructure:"waf_logged"` |
| 42 |
WAFBlocked uint64 `mapstructure:"waf_blocked"` |
| 43 |
WAFPassed uint64 `mapstructure:"waf_passed"` |
| 44 |
AttackRequestBodyBytes uint64 `mapstructure:"attack_req_body_bytes"` |
| 45 |
AttachRequestHeaderBytes uint64 `mapstructure:"attack_req_header_bytes"` |
| 46 |
AttackResponseSynthBytes uint64 `mapstructure:"attack_resp_synth_bytes"` |
| 47 |
ImageOptimizer uint64 `mapstructure:"imgopto"` |
| 48 |
Status200 uint64 `mapstructure:"status_200"` |
| 49 |
Status204 uint64 `mapstructure:"status_204"` |
| 50 |
Status301 uint64 `mapstructure:"status_301"` |
| 51 |
Status302 uint64 `mapstructure:"status_302"` |
| 52 |
Status304 uint64 `mapstructure:"status_304"` |
| 53 |
Status400 uint64 `mapstructure:"status_400"` |
| 54 |
Status401 uint64 `mapstructure:"status_401"` |
| 55 |
Status403 uint64 `mapstructure:"status_403"` |
| 56 |
Status404 uint64 `mapstructure:"status_404"` |
| 57 |
Status416 uint64 `mapstructure:"status_416"` |
| 58 |
Status500 uint64 `mapstructure:"status_500"` |
| 59 |
Status501 uint64 `mapstructure:"status_501"` |
| 60 |
Status502 uint64 `mapstructure:"status_502"` |
| 61 |
Status503 uint64 `mapstructure:"status_503"` |
| 62 |
Status504 uint64 `mapstructure:"status_504"` |
| 63 |
Status505 uint64 `mapstructure:"status_505"` |
| 64 |
Status1xx uint64 `mapstructure:"status_1xx"` |
| 65 |
Status2xx uint64 `mapstructure:"status_2xx"` |
| 66 |
Status3xx uint64 `mapstructure:"status_3xx"` |
| 67 |
Status4xx uint64 `mapstructure:"status_4xx"` |
| 68 |
Status5xx uint64 `mapstructure:"status_5xx"` |
| 69 |
ObjectSize1k uint64 `mapstructure:"object_size_1k"` |
| 70 |
ObjectSize10k uint64 `mapstructure:"object_size_10k"` |
| 71 |
ObjectSize100k uint64 `mapstructure:"object_size_100k"` |
| 72 |
ObjectSize1m uint64 `mapstructure:"object_size_1m"` |
| 73 |
ObjectSize10m uint64 `mapstructure:"object_size_10m"` |
| 74 |
ObjectSize100m uint64 `mapstructure:"object_size_100m"` |
| 75 |
ObjectSize1g uint64 `mapstructure:"object_size_1g"` |
| 76 |
MissHistogram map[int]int `mapstructure:"miss_histogram"` |
| 77 |
BilledHeaderBytes uint64 `mapstructure:"billed_header_bytes"` |
| 78 |
BilledBodyBytes uint64 `mapstructure:"billed_body_bytes"` |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
type GetStatsInput struct { |
| 86 |
Service string |
| 87 |
Field string |
| 88 |
From string |
| 89 |
To string |
| 90 |
By string |
| 91 |
Region string |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
type StatsResponse struct { |
| 96 |
Status string `mapstructure:"status"` |
| 97 |
Meta map[string]string `mapstructure:"meta"` |
| 98 |
Message string `mapstructure:"msg"` |
| 99 |
Data []*Stats `mapstructure:"data"` |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func (c *Client) GetStats(i *GetStatsInput) (*StatsResponse, error) { |
| 104 |
|
| 105 |
p := "/stats" |
| 106 |
|
| 107 |
if i.Service != "" { |
| 108 |
p = fmt.Sprintf("%s/service/%s", p, i.Service) |
| 109 |
} |
| 110 |
|
| 111 |
if i.Field != "" { |
| 112 |
p = fmt.Sprintf("%s/field/%s", p, i.Field) |
| 113 |
} |
| 114 |
|
| 115 |
r, err := c.Get(p, &RequestOptions{ |
| 116 |
Params: map[string]string{ |
| 117 |
"from": i.From, |
| 118 |
"to": i.To, |
| 119 |
"by": i.By, |
| 120 |
"region": i.Region, |
| 121 |
}, |
| 122 |
}) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
var sr *StatsResponse |
| 128 |
if err := decodeJSON(&sr, r.Body); err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
|
| 132 |
return sr, nil |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
type UsageStatsResponse struct { |
| 137 |
Status string `mapstructure:"status"` |
| 138 |
Meta map[string]string `mapstructure:"meta"` |
| 139 |
Message string `mapstructure:"msg"` |
| 140 |
Data map[string]*Usage `mapstructure:"data"` |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
type Usage struct { |
| 145 |
Requests uint64 `mapstructure:"requests"` |
| 146 |
Bandwidth uint64 `mapstructure:"bandwidth"` |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
type RegionsUsage map[string]*Usage |
| 151 |
|
| 152 |
|
| 153 |
type UsageResponse struct { |
| 154 |
Status string `mapstructure:"status"` |
| 155 |
Meta map[string]string `mapstructure:"meta"` |
| 156 |
Message string `mapstructure:"msg"` |
| 157 |
Data *RegionsUsage `mapstructure:"data"` |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
type GetUsageInput struct { |
| 163 |
From string |
| 164 |
To string |
| 165 |
By string |
| 166 |
Region string |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
func (c *Client) GetUsage(i *GetUsageInput) (*UsageResponse, error) { |
| 171 |
r, err := c.Get("/stats/usage", &RequestOptions{ |
| 172 |
Params: map[string]string{ |
| 173 |
"from": i.From, |
| 174 |
"to": i.To, |
| 175 |
"by": i.By, |
| 176 |
"region": i.Region, |
| 177 |
}, |
| 178 |
}) |
| 179 |
if err != nil { |
| 180 |
return nil, err |
| 181 |
} |
| 182 |
var sr *UsageResponse |
| 183 |
if err := decodeJSON(&sr, r.Body); err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
|
| 187 |
return sr, nil |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
type UsageByServiceResponse struct { |
| 192 |
Status string `mapstructure:"status"` |
| 193 |
Meta map[string]string `mapstructure:"meta"` |
| 194 |
Message string `mapstructure:"msg"` |
| 195 |
Data *ServicesByRegionsUsage `mapstructure:"data"` |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
type ServicesUsage map[string]*Usage |
| 200 |
|
| 201 |
|
| 202 |
type ServicesByRegionsUsage map[string]*ServicesUsage |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func (c *Client) GetUsageByService(i *GetUsageInput) (*UsageByServiceResponse, error) { |
| 207 |
r, err := c.Get("/stats/usage_by_service", &RequestOptions{ |
| 208 |
Params: map[string]string{ |
| 209 |
"from": i.From, |
| 210 |
"to": i.To, |
| 211 |
"by": i.By, |
| 212 |
"region": i.Region, |
| 213 |
}, |
| 214 |
}) |
| 215 |
if err != nil { |
| 216 |
return nil, err |
| 217 |
} |
| 218 |
var sr *UsageByServiceResponse |
| 219 |
if err := decodeJSON(&sr, r.Body); err != nil { |
| 220 |
return nil, err |
| 221 |
} |
| 222 |
|
| 223 |
return sr, nil |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
type RegionsResponse struct { |
| 228 |
Status string `mapstructure:"status"` |
| 229 |
Meta map[string]string `mapstructure:"meta"` |
| 230 |
Message string `mapstructure:"msg"` |
| 231 |
Data []string `mapstructure:"data"` |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
func (c *Client) GetRegions() (*RegionsResponse, error) { |
| 236 |
r, err := c.Get("stats/regions", nil) |
| 237 |
if err != nil { |
| 238 |
return nil, err |
| 239 |
} |
| 240 |
|
| 241 |
var rr *RegionsResponse |
| 242 |
if err := decodeJSON(&rr, r.Body); err != nil { |
| 243 |
return nil, err |
| 244 |
} |
| 245 |
|
| 246 |
return rr, nil |
| 247 |
} |