Skip to content

Commit 45ecf1e

Browse files
committed
Fix --region flag on stats usage to filter response by region
The Fastly API returns data for all regions regardless of the region query parameter. So, do client-side filtering for now.
1 parent f7e24e0 commit 45ecf1e

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

pkg/commands/stats/usage.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ func (c *UsageCommand) execPlain(out io.Writer, input *fastly.GetUsageInput) err
7878
return fmt.Errorf("non-success response: %s", fastly.ToValue(resp.Message))
7979
}
8080

81+
filterUsageByRegion(resp.Data, c.region)
82+
8183
switch c.formatFlag {
8284
case "json":
8385
return writeUsageJSON(out, resp.Data)
@@ -97,6 +99,8 @@ func (c *UsageCommand) execByService(out io.Writer, input *fastly.GetUsageInput)
9799
return fmt.Errorf("non-success response: %s", fastly.ToValue(resp.Message))
98100
}
99101

102+
filterUsageByServiceByRegion(resp.Data, c.region)
103+
100104
switch c.formatFlag {
101105
case "json":
102106
return writeUsageByServiceJSON(out, resp.Data)
@@ -182,6 +186,28 @@ func usageToMap(data fastly.RegionsUsage) map[string]any {
182186
return result
183187
}
184188

189+
func filterUsageByRegion(data *fastly.RegionsUsage, region string) {
190+
if region == "" || data == nil {
191+
return
192+
}
193+
for k := range *data {
194+
if k != region {
195+
delete(*data, k)
196+
}
197+
}
198+
}
199+
200+
func filterUsageByServiceByRegion(data *fastly.ServicesByRegionsUsage, region string) {
201+
if region == "" || data == nil {
202+
return
203+
}
204+
for k := range *data {
205+
if k != region {
206+
delete(*data, k)
207+
}
208+
}
209+
}
210+
185211
func usageEntry(u *fastly.Usage) map[string]any {
186212
if u == nil {
187213
return map[string]any{

pkg/commands/stats/usage_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"io"
7+
"strings"
78
"testing"
89

910
"github.com/fastly/go-fastly/v13/fastly"
@@ -22,6 +23,7 @@ func TestUsage(t *testing.T) {
2223
api mock.API
2324
wantError string
2425
wantOutput string
26+
wantAbsent string
2527
}{
2628
{
2729
name: "success plain",
@@ -59,6 +61,20 @@ func TestUsage(t *testing.T) {
5961
api: mock.API{GetUsageFn: getUsageWithNilEntry},
6062
wantOutput: "Region: europe",
6163
},
64+
{
65+
name: "region filter plain",
66+
args: args("stats usage --region=europe"),
67+
api: mock.API{GetUsageFn: getUsageMultiRegion},
68+
wantOutput: "Region: europe",
69+
wantAbsent: "usa",
70+
},
71+
{
72+
name: "region filter by-service",
73+
args: args("stats usage --by-service --region=europe"),
74+
api: mock.API{GetUsageByServiceFn: getUsageByServiceMultiRegion},
75+
wantOutput: "Region: europe",
76+
wantAbsent: "usa",
77+
},
6278
{
6379
name: "non-success status",
6480
args: args("stats usage"),
@@ -83,6 +99,9 @@ func TestUsage(t *testing.T) {
8399
err := app.Run(tc.args, nil)
84100
testutil.AssertErrorContains(t, err, tc.wantError)
85101
testutil.AssertStringContains(t, stdout.String(), tc.wantOutput)
102+
if tc.wantAbsent != "" && strings.Contains(stdout.String(), tc.wantAbsent) {
103+
t.Errorf("output should not contain %q, got: %s", tc.wantAbsent, stdout.String())
104+
}
86105
})
87106
}
88107
}
@@ -138,6 +157,46 @@ func getUsageWithNilEntry(_ context.Context, _ *fastly.GetUsageInput) (*fastly.U
138157
}, nil
139158
}
140159

160+
func getUsageMultiRegion(_ context.Context, _ *fastly.GetUsageInput) (*fastly.UsageResponse, error) {
161+
return &fastly.UsageResponse{
162+
Status: fastly.ToPointer("success"),
163+
Data: &fastly.RegionsUsage{
164+
"usa": &fastly.Usage{
165+
Bandwidth: fastly.ToPointer(uint64(1000)),
166+
Requests: fastly.ToPointer(uint64(500)),
167+
ComputeRequests: fastly.ToPointer(uint64(100)),
168+
},
169+
"europe": &fastly.Usage{
170+
Bandwidth: fastly.ToPointer(uint64(2000)),
171+
Requests: fastly.ToPointer(uint64(300)),
172+
ComputeRequests: fastly.ToPointer(uint64(50)),
173+
},
174+
},
175+
}, nil
176+
}
177+
178+
func getUsageByServiceMultiRegion(_ context.Context, _ *fastly.GetUsageInput) (*fastly.UsageByServiceResponse, error) {
179+
return &fastly.UsageByServiceResponse{
180+
Status: fastly.ToPointer("success"),
181+
Data: &fastly.ServicesByRegionsUsage{
182+
"usa": &fastly.ServicesUsage{
183+
"svc123": &fastly.Usage{
184+
Bandwidth: fastly.ToPointer(uint64(1000)),
185+
Requests: fastly.ToPointer(uint64(500)),
186+
ComputeRequests: fastly.ToPointer(uint64(100)),
187+
},
188+
},
189+
"europe": &fastly.ServicesUsage{
190+
"svc456": &fastly.Usage{
191+
Bandwidth: fastly.ToPointer(uint64(2000)),
192+
Requests: fastly.ToPointer(uint64(300)),
193+
ComputeRequests: fastly.ToPointer(uint64(50)),
194+
},
195+
},
196+
},
197+
}, nil
198+
}
199+
141200
func getUsageNonSuccess(_ context.Context, _ *fastly.GetUsageInput) (*fastly.UsageResponse, error) {
142201
return &fastly.UsageResponse{
143202
Status: fastly.ToPointer("error"),

0 commit comments

Comments
 (0)