Skip to content

Commit

Permalink
fix: Allow multiple --type flags for metrics commands
Browse files Browse the repository at this point in the history
Closes #642
  • Loading branch information
phm07 committed Dec 19, 2023
1 parent 238b7f7 commit 262e230
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
24 changes: 20 additions & 4 deletions internal/cmd/loadbalancer/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"sort"
"strconv"
"time"
Expand All @@ -20,6 +21,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var metricTypeStrings = []string{
string(hcloud.LoadBalancerMetricOpenConnections),
string(hcloud.LoadBalancerMetricConnectionsPerSecond),
string(hcloud.LoadBalancerMetricRequestsPerSecond),
string(hcloud.LoadBalancerMetricBandwidth),
}

var MetricsCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -31,9 +39,9 @@ var MetricsCmd = base.Cmd{
DisableFlagsInUseLine: true,
}

cmd.Flags().String("type", "", "Type of metrics you want to show")
cmd.Flags().StringSlice("type", nil, "Type of metrics you want to show")
cmd.MarkFlagRequired("type")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("open_connections", "connections_per_second", "requests_per_second", "bandwidth"))
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates(metricTypeStrings...))

cmd.Flags().String("start", "", "ISO 8601 timestamp")
cmd.Flags().String("end", "", "ISO 8601 timestamp")
Expand All @@ -53,7 +61,15 @@ var MetricsCmd = base.Cmd{
return fmt.Errorf("LoadBalancer not found: %s", idOrName)
}

metricType, _ := cmd.Flags().GetString("type")
metricTypesStr, _ := cmd.Flags().GetStringSlice("type")
var metricTypes []hcloud.LoadBalancerMetricType
for _, t := range metricTypesStr {
if slices.Contains(metricTypeStrings, t) {
metricTypes = append(metricTypes, hcloud.LoadBalancerMetricType(t))
} else {
return fmt.Errorf("invalid metric type: %s", t)
}
}

start, _ := cmd.Flags().GetString("start")
startTime := time.Now().Add(-30 * time.Minute)
Expand All @@ -74,7 +90,7 @@ var MetricsCmd = base.Cmd{
}

m, resp, err := client.LoadBalancer().GetMetrics(ctx, LoadBalancer, hcloud.LoadBalancerGetMetricsOpts{
Types: []hcloud.LoadBalancerMetricType{hcloud.LoadBalancerMetricType(metricType)},
Types: metricTypes,
Start: startTime,
End: endTime,
})
Expand Down
23 changes: 19 additions & 4 deletions internal/cmd/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"sort"
"strconv"
"time"
Expand All @@ -20,6 +21,12 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var metricTypeStrings = []string{
string(hcloud.ServerMetricCPU),
string(hcloud.ServerMetricDisk),
string(hcloud.ServerMetricNetwork),
}

var MetricsCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -31,9 +38,9 @@ var MetricsCmd = base.Cmd{
DisableFlagsInUseLine: true,
}

cmd.Flags().String("type", "", "Type of metrics you want to show")
cmd.Flags().StringSlice("type", nil, "Types of metrics you want to show")
cmd.MarkFlagRequired("type")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("cpu", "disk", "network"))
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates(metricTypeStrings...))

cmd.Flags().String("start", "", "ISO 8601 timestamp")
cmd.Flags().String("end", "", "ISO 8601 timestamp")
Expand All @@ -53,7 +60,15 @@ var MetricsCmd = base.Cmd{
return fmt.Errorf("server not found: %s", idOrName)
}

metricType, _ := cmd.Flags().GetString("type")
metricTypesStr, _ := cmd.Flags().GetStringSlice("type")
var metricTypes []hcloud.ServerMetricType
for _, t := range metricTypesStr {
if slices.Contains(metricTypeStrings, t) {
metricTypes = append(metricTypes, hcloud.ServerMetricType(t))
} else {
return fmt.Errorf("invalid metric type: %s", t)
}
}

start, _ := cmd.Flags().GetString("start")
startTime := time.Now().Add(-30 * time.Minute)
Expand All @@ -74,7 +89,7 @@ var MetricsCmd = base.Cmd{
}

m, resp, err := client.Server().GetMetrics(ctx, server, hcloud.ServerGetMetricsOpts{
Types: []hcloud.ServerMetricType{hcloud.ServerMetricType(metricType)},
Types: metricTypes,
Start: startTime,
End: endTime,
})
Expand Down

0 comments on commit 262e230

Please sign in to comment.