From 411baa4dcf2ebf561ebe6eda51957354d8fb432a Mon Sep 17 00:00:00 2001 From: AlteredCoder <64792091+AlteredCoder@users.noreply.github.com> Date: Mon, 21 Mar 2022 12:13:36 +0100 Subject: [PATCH] Improve cscli metrics units (#1374) * Improve cscli metrics units --- cmd/crowdsec-cli/hub.go | 2 +- cmd/crowdsec-cli/metrics.go | 16 ++++++++- cmd/crowdsec-cli/utils.go | 66 +++++++++++++++++++++++++++++++++++-- pkg/cwhub/cwhub.go | 2 ++ 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 721920160..51f85b1d9 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -56,7 +56,7 @@ cscli hub update # Download list of available configurations from the hub log.Info(v) } cwhub.DisplaySummary() - ListItems([]string{cwhub.PARSERS, cwhub.COLLECTIONS, cwhub.SCENARIOS, cwhub.PARSERS_OVFLW}, args, true, false) + ListItems([]string{cwhub.COLLECTIONS, cwhub.PARSERS, cwhub.SCENARIOS, cwhub.PARSERS_OVFLW}, args, true, false) }, } cmdHubList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well") diff --git a/cmd/crowdsec-cli/metrics.go b/cmd/crowdsec-cli/metrics.go index 2191dbdde..0f4fe0f65 100644 --- a/cmd/crowdsec-cli/metrics.go +++ b/cmd/crowdsec-cli/metrics.go @@ -74,7 +74,11 @@ func metricsToTable(table *tablewriter.Table, stats map[string]map[string]int, k row = append(row, alabel) //name for _, sl := range keys { if v, ok := astats[sl]; ok && v != 0 { - row = append(row, fmt.Sprintf("%d", v)) + numberToShow := fmt.Sprintf("%d", v) + if !noUnit { + numberToShow = formatNumber(v) + } + row = append(row, numberToShow) } else { row = append(row, "-") } @@ -327,31 +331,38 @@ func ShowPrometheus(url string) { if bucketsTable.NumLines() > 0 { log.Printf("Buckets Metrics:") + bucketsTable.SetAlignment(tablewriter.ALIGN_LEFT) bucketsTable.Render() } if acquisTable.NumLines() > 0 { log.Printf("Acquisition Metrics:") + acquisTable.SetAlignment(tablewriter.ALIGN_LEFT) acquisTable.Render() } if parsersTable.NumLines() > 0 { log.Printf("Parser Metrics:") + parsersTable.SetAlignment(tablewriter.ALIGN_LEFT) parsersTable.Render() } if lapiTable.NumLines() > 0 { log.Printf("Local Api Metrics:") + lapiTable.SetAlignment(tablewriter.ALIGN_LEFT) lapiTable.Render() } if lapiMachinesTable.NumLines() > 0 { log.Printf("Local Api Machines Metrics:") + lapiMachinesTable.SetAlignment(tablewriter.ALIGN_LEFT) lapiMachinesTable.Render() } if lapiBouncersTable.NumLines() > 0 { log.Printf("Local Api Bouncers Metrics:") + lapiBouncersTable.SetAlignment(tablewriter.ALIGN_LEFT) lapiBouncersTable.Render() } if lapiDecisionsTable.NumLines() > 0 { log.Printf("Local Api Bouncers Decisions:") + lapiDecisionsTable.SetAlignment(tablewriter.ALIGN_LEFT) lapiDecisionsTable.Render() } @@ -374,6 +385,8 @@ func ShowPrometheus(url string) { } } +var noUnit bool + func NewMetricsCmd() *cobra.Command { /* ---- UPDATE COMMAND */ var cmdMetrics = &cobra.Command{ @@ -404,6 +417,7 @@ func NewMetricsCmd() *cobra.Command { }, } cmdMetrics.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url (http://:/metrics)") + cmdMetrics.PersistentFlags().BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units") return cmdMetrics } diff --git a/cmd/crowdsec-cli/utils.go b/cmd/crowdsec-cli/utils.go index a01fd0ef4..51c612052 100644 --- a/cmd/crowdsec-cli/utils.go +++ b/cmd/crowdsec-cli/utils.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math" "net" "net/http" "os" @@ -122,7 +123,13 @@ func ListItems(itemTypes []string, args []string, showType bool, showHeader bool } if csConfig.Cscli.Output == "human" { - for itemType, statuses := range hubStatusByItemType { + for _, itemType := range itemTypes { + var statuses []cwhub.ItemHubStatus + var ok bool + if statuses, ok = hubStatusByItemType[itemType]; !ok { + log.Errorf("unknown item type: %s", itemType) + continue + } fmt.Println(strings.ToUpper(itemType)) table := tablewriter.NewWriter(os.Stdout) table.SetCenterSeparator("") @@ -154,7 +161,13 @@ func ListItems(itemTypes []string, args []string, showType bool, showHeader bool } } - for itemType, statuses := range hubStatusByItemType { + for _, itemType := range itemTypes { + var statuses []cwhub.ItemHubStatus + var ok bool + if statuses, ok = hubStatusByItemType[itemType]; !ok { + log.Errorf("unknown item type: %s", itemType) + continue + } for _, status := range statuses { if status.LocalVersion == "" { status.LocalVersion = "n/a" @@ -745,3 +758,52 @@ func BackupHub(dirPath string) error { return nil } + +type unit struct { + value int + symbol string +} + +var ranges = []unit{ + { + value: 1e18, + symbol: "E", + }, + { + value: 1e15, + symbol: "P", + }, + { + value: 1e12, + symbol: "T", + }, + { + value: 1e6, + symbol: "M", + }, + { + value: 1e3, + symbol: "k", + }, + { + value: 1, + symbol: "", + }, +} + +func formatNumber(num int) string { + goodUnit := unit{} + for _, u := range ranges { + if num >= u.value { + goodUnit = u + break + } + } + + if goodUnit.value == 1 { + return fmt.Sprintf("%d%s", num, goodUnit.symbol) + } + + res := math.Round(float64(num)/float64(goodUnit.value)*100) / 100 + return fmt.Sprintf("%.2f%s", res, goodUnit.symbol) +} diff --git a/pkg/cwhub/cwhub.go b/pkg/cwhub/cwhub.go index 207ea89af..1cdf3e276 100644 --- a/pkg/cwhub/cwhub.go +++ b/pkg/cwhub/cwhub.go @@ -3,6 +3,7 @@ package cwhub import ( "crypto/sha256" "path/filepath" + "sort" "strings" //"errors" @@ -284,5 +285,6 @@ func GetHubStatusForItemType(itemType string, name string, all bool) []ItemHubSt //Check the item status ret = append(ret, item.toHubStatus()) } + sort.Slice(ret, func(i, j int) bool { return ret[i].Name < ret[j].Name }) return ret }