Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions pkg/commands/stats/domain_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,40 +143,9 @@ func (c *DomainInspectorCommand) Exec(_ io.Reader, out io.Writer) error {
if fastly.ToValue(resp.Status) != statusSuccess {
return fmt.Errorf("non-success response: %s", fastly.ToValue(resp.Status))
}
return writeDomainInspector(out, resp)
}
}

func writeDomainInspector(out io.Writer, resp *fastly.DomainInspector) error {
if resp.Meta != nil {
if resp.Meta.Start != nil {
text.Output(out, "Start: %s", *resp.Meta.Start)
}
if resp.Meta.End != nil {
text.Output(out, "End: %s", *resp.Meta.End)
}
fmt.Fprintln(out, "---")
}
for _, d := range resp.Data {
if d.Dimensions != nil {
for k, v := range d.Dimensions {
text.Output(out, "%s: %s", k, fastly.ToValue(v))
}
}
for _, v := range d.Values {
if v.Timestamp != nil {
text.Output(out, " Timestamp: %s", time.Unix(int64(*v.Timestamp), 0).UTC()) //nolint:gosec // timestamp won't overflow
}
text.Output(out, " Requests: %d", fastly.ToValue(v.Requests))
text.Output(out, " Bandwidth: %d", fastly.ToValue(v.Bandwidth))
text.Output(out, " Edge Requests: %d", fastly.ToValue(v.EdgeRequests))
text.Output(out, " Edge Hit Ratio: %.4f", fastly.ToValue(v.EdgeHitRatio))
}
}
if resp.Meta != nil && resp.Meta.NextCursor != nil {
text.Output(out, "Next cursor: %s", *resp.Meta.NextCursor)
text.PrintDomainInspectorTbl(out, resp)
return nil
}
return nil
}

func parseTime(s string) (time.Time, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/commands/stats/domain_inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDomainInspector(t *testing.T) {
api: mock.API{
GetDomainMetricsForServiceFn: getDomainMetricsOK,
},
wantOutput: "Requests:",
wantOutput: "REQUESTS",
},
{
name: "success json",
Expand Down Expand Up @@ -78,23 +78,23 @@ func TestDomainInspector(t *testing.T) {
api: mock.API{
GetDomainMetricsForServiceFn: getDomainMetricsAssertStart(time.Date(2024, 1, 15, 10, 0, 0, 0, time.UTC)),
},
wantOutput: "Requests:",
wantOutput: "REQUESTS",
},
{
name: "from Unix epoch maps to Start",
args: args("stats domain-inspector --service-id 123 --from 1705312800"),
api: mock.API{
GetDomainMetricsForServiceFn: getDomainMetricsAssertStart(time.Unix(1705312800, 0)),
},
wantOutput: "Requests:",
wantOutput: "REQUESTS",
},
{
name: "to RFC3339 maps to End",
args: args("stats domain-inspector --service-id 123 --to 2024-01-15T11:00:00Z"),
api: mock.API{
GetDomainMetricsForServiceFn: getDomainMetricsAssertEnd(time.Date(2024, 1, 15, 11, 0, 0, 0, time.UTC)),
},
wantOutput: "Requests:",
wantOutput: "REQUESTS",
},
{
name: "from invalid format error",
Expand Down
36 changes: 2 additions & 34 deletions pkg/commands/stats/origin_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"time"

"github.com/fastly/go-fastly/v13/fastly"

Expand Down Expand Up @@ -142,38 +141,7 @@ func (c *OriginInspectorCommand) Exec(_ io.Reader, out io.Writer) error {
if fastly.ToValue(resp.Status) != statusSuccess {
return fmt.Errorf("non-success response: %s", fastly.ToValue(resp.Status))
}
return writeOriginInspector(out, resp)
}
}

func writeOriginInspector(out io.Writer, resp *fastly.OriginInspector) error {
if resp.Meta != nil {
if resp.Meta.Start != nil {
text.Output(out, "Start: %s", *resp.Meta.Start)
}
if resp.Meta.End != nil {
text.Output(out, "End: %s", *resp.Meta.End)
}
fmt.Fprintln(out, "---")
}
for _, d := range resp.Data {
if d.Dimensions != nil {
for k, v := range d.Dimensions {
text.Output(out, "%s: %s", k, v)
}
}
for _, v := range d.Values {
if v.Timestamp != nil {
text.Output(out, " Timestamp: %s", time.Unix(int64(*v.Timestamp), 0).UTC()) //nolint:gosec // timestamp won't overflow
}
text.Output(out, " Responses: %d", fastly.ToValue(v.Responses))
text.Output(out, " Status 2xx: %d", fastly.ToValue(v.Status2xx))
text.Output(out, " Status 4xx: %d", fastly.ToValue(v.Status4xx))
text.Output(out, " Status 5xx: %d", fastly.ToValue(v.Status5xx))
}
}
if resp.Meta != nil && resp.Meta.NextCursor != nil {
text.Output(out, "Next cursor: %s", *resp.Meta.NextCursor)
text.PrintOriginInspectorTbl(out, resp)
return nil
}
return nil
}
8 changes: 4 additions & 4 deletions pkg/commands/stats/origin_inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestOriginInspector(t *testing.T) {
api: mock.API{
GetOriginMetricsForServiceFn: getOriginMetricsOK,
},
wantOutput: "Responses:",
wantOutput: "RESPONSES",
},
{
name: "success json",
Expand Down Expand Up @@ -78,23 +78,23 @@ func TestOriginInspector(t *testing.T) {
api: mock.API{
GetOriginMetricsForServiceFn: getOriginMetricsAssertStart(time.Date(2024, 1, 15, 10, 0, 0, 0, time.UTC)),
},
wantOutput: "Responses:",
wantOutput: "RESPONSES",
},
{
name: "from Unix epoch maps to Start",
args: args("stats origin-inspector --service-id 123 --from 1705312800"),
api: mock.API{
GetOriginMetricsForServiceFn: getOriginMetricsAssertStart(time.Unix(1705312800, 0)),
},
wantOutput: "Responses:",
wantOutput: "RESPONSES",
},
{
name: "to RFC3339 maps to End",
args: args("stats origin-inspector --service-id 123 --to 2024-01-15T11:00:00Z"),
api: mock.API{
GetOriginMetricsForServiceFn: getOriginMetricsAssertEnd(time.Date(2024, 1, 15, 11, 0, 0, 0, time.UTC)),
},
wantOutput: "Responses:",
wantOutput: "RESPONSES",
},
{
name: "from invalid format error",
Expand Down
50 changes: 3 additions & 47 deletions pkg/commands/stats/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"maps"
"slices"

"github.com/fastly/go-fastly/v13/fastly"

Expand Down Expand Up @@ -84,7 +82,8 @@ func (c *UsageCommand) execPlain(out io.Writer, input *fastly.GetUsageInput) err
case "json":
return writeUsageJSON(out, resp.Data)
default:
return writeUsageTable(out, resp.Data)
text.PrintUsageTbl(out, resp.Data)
return nil
}
}

Expand All @@ -105,26 +104,9 @@ func (c *UsageCommand) execByService(out io.Writer, input *fastly.GetUsageInput)
case "json":
return writeUsageByServiceJSON(out, resp.Data)
default:
return writeUsageByServiceTable(out, resp.Data)
}
}

func writeUsageTable(out io.Writer, data *fastly.RegionsUsage) error {
if data == nil {
text.PrintUsageByServiceTbl(out, resp.Data)
return nil
}
regions := slices.Sorted(maps.Keys(*data))
for _, region := range regions {
usage := (*data)[region]
if usage == nil {
continue
}
text.Output(out, "Region: %s", region)
text.Output(out, " Bandwidth: %d", fastly.ToValue(usage.Bandwidth))
text.Output(out, " Requests: %d", fastly.ToValue(usage.Requests))
text.Output(out, " Compute Requests: %d", fastly.ToValue(usage.ComputeRequests))
}
return nil
}

func writeUsageJSON(out io.Writer, data *fastly.RegionsUsage) error {
Expand All @@ -134,32 +116,6 @@ func writeUsageJSON(out io.Writer, data *fastly.RegionsUsage) error {
return json.NewEncoder(out).Encode(usageToMap(*data))
}

func writeUsageByServiceTable(out io.Writer, data *fastly.ServicesByRegionsUsage) error {
if data == nil {
return nil
}
regions := slices.Sorted(maps.Keys(*data))
for _, region := range regions {
services := (*data)[region]
if services == nil {
continue
}
text.Output(out, "Region: %s", region)
serviceIDs := slices.Sorted(maps.Keys(*services))
for _, svcID := range serviceIDs {
usage := (*services)[svcID]
if usage == nil {
continue
}
text.Output(out, " Service: %s", svcID)
text.Output(out, " Bandwidth: %d", fastly.ToValue(usage.Bandwidth))
text.Output(out, " Requests: %d", fastly.ToValue(usage.Requests))
text.Output(out, " Compute Requests: %d", fastly.ToValue(usage.ComputeRequests))
}
}
return nil
}

func writeUsageByServiceJSON(out io.Writer, data *fastly.ServicesByRegionsUsage) error {
if data == nil {
return json.NewEncoder(out).Encode(map[string]any{})
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/stats/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUsage(t *testing.T) {
name: "success plain",
args: args("stats usage"),
api: mock.API{GetUsageFn: getUsageOK},
wantOutput: "Region: usa",
wantOutput: "usa",
},
{
name: "success json",
Expand All @@ -41,7 +41,7 @@ func TestUsage(t *testing.T) {
name: "success by-service",
args: args("stats usage --by-service"),
api: mock.API{GetUsageByServiceFn: getUsageByServiceOK},
wantOutput: "Service: svc123",
wantOutput: "svc123",
},
{
name: "success by-service json",
Expand All @@ -59,20 +59,20 @@ func TestUsage(t *testing.T) {
name: "nil usage entry table skipped",
args: args("stats usage"),
api: mock.API{GetUsageFn: getUsageWithNilEntry},
wantOutput: "Region: europe",
wantOutput: "europe",
},
{
name: "region filter plain",
args: args("stats usage --region=europe"),
api: mock.API{GetUsageFn: getUsageMultiRegion},
wantOutput: "Region: europe",
wantOutput: "europe",
wantAbsent: "usa",
},
{
name: "region filter by-service",
args: args("stats usage --by-service --region=europe"),
api: mock.API{GetUsageByServiceFn: getUsageByServiceMultiRegion},
wantOutput: "Region: europe",
wantOutput: "svc456",
wantAbsent: "usa",
},
{
Expand Down
Loading
Loading