Skip to content

Commit

Permalink
feat: update operator usange cli command to includes nodes count
Browse files Browse the repository at this point in the history
  • Loading branch information
JadhavPoonam committed Jun 29, 2023
1 parent 54cdccd commit 5ab59f5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/operator_usage.go
Expand Up @@ -10,6 +10,7 @@ type Usage struct {

// ServiceUsage contains information about the number of services and service instances for a datacenter.
type ServiceUsage struct {
Nodes int
Services int
ServiceInstances int
ConnectServiceInstances map[string]int
Expand Down
36 changes: 36 additions & 0 deletions command/operator/usage/instances/usage_instances.go
Expand Up @@ -99,6 +99,14 @@ func (c *cmd) Run(args []string) int {
return 1
}
c.UI.Output(billableOutput + "\n")

c.UI.Output("\nNodes")
nodesOutput, err := formatNodesCounts(usage.Usage)
if err != nil {
c.UI.Error(err.Error())
return 1
}
c.UI.Output(nodesOutput + "\n\n")
}

// Output Connect service counts
Expand All @@ -115,6 +123,34 @@ func (c *cmd) Run(args []string) int {
return 0
}

func formatNodesCounts(usageStats map[string]api.ServiceUsage) (string, error) {
var output bytes.Buffer
tw := tabwriter.NewWriter(&output, 0, 2, 6, ' ', 0)

nodesTotal := 0

fmt.Fprintf(tw, "Datacenter\t")

fmt.Fprintf(tw, "Count\t")

fmt.Fprint(tw, "\t\n")

for dc, usage := range usageStats {
nodesTotal += usage.Nodes
fmt.Fprintf(tw, "%s\t%d\n", dc, usage.Nodes)
}

fmt.Fprint(tw, "\t\n")
fmt.Fprintf(tw, "Total")

fmt.Fprintf(tw, "\t%d", nodesTotal)

if err := tw.Flush(); err != nil {
return "", fmt.Errorf("Error flushing tabwriter: %s", err)
}
return strings.TrimSpace(output.String()), nil
}

func formatServiceCounts(usageStats map[string]api.ServiceUsage, billable, showDatacenter bool) (string, error) {
var output bytes.Buffer
tw := tabwriter.NewWriter(&output, 0, 2, 6, ' ', 0)
Expand Down
51 changes: 51 additions & 0 deletions command/operator/usage/instances/usage_instances_oss_test.go
Expand Up @@ -117,3 +117,54 @@ Total 45`,
})
}
}

func TestUsageInstances_formatNodesCounts(t *testing.T) {
usageBasic := map[string]api.ServiceUsage{
"dc1": {
Nodes: 10,
},
}

usageMultiDC := map[string]api.ServiceUsage{
"dc1": {
Nodes: 10,
},
"dc2": {
Nodes: 11,
},
}

cases := []struct {
name string
usageStats map[string]api.ServiceUsage
expectedNodes string
}{
{
name: "basic",
usageStats: usageBasic,
expectedNodes: `
Datacenter Count
dc1 10
Total 10`,
},
{
name: "multi-datacenter",
usageStats: usageMultiDC,
expectedNodes: `
Datacenter Count
dc1 10
dc2 11
Total 21`,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
nodesOutput, err := formatNodesCounts(tc.usageStats)
require.NoError(t, err)
require.Equal(t, strings.TrimSpace(tc.expectedNodes), nodesOutput)
})
}
}
13 changes: 13 additions & 0 deletions website/content/commands/operator/usage.mdx
Expand Up @@ -50,6 +50,12 @@ Billable Services
Services Service instances
2 3
Nodes
Datacenter Count
dc1 1
Total 1
Connect Services
Type Service instances
connect-native 0
Expand All @@ -74,6 +80,13 @@ dc2 1 1
Total 3 4
Nodes
Datacenter Count
dc1 1
dc2 2
Total 3
Connect Services
Datacenter Type Service instances
dc1 connect-native 0
Expand Down

0 comments on commit 5ab59f5

Please sign in to comment.