crowdsec/cmd/crowdsec-cli/climetrics/statappsecrule.go
mmetc 8d96ddd48e
refact cscli metrics: fix lines between tables, skip wrapper api (#3137)
* fix empty line between metrics tables

* refact metrics tables: use go-pretty api directly

* lint
2024-07-17 12:30:52 +02:00

48 lines
1.4 KiB
Go

package climetrics
import (
"fmt"
"io"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
)
type statAppsecRule map[string]map[string]map[string]int
func (s statAppsecRule) Description() (string, string) {
return "Appsec Rule Metrics",
`Provides “per AppSec Component” information about the number of matches for loaded AppSec Rules.`
}
func (s statAppsecRule) Process(appsecEngine, appsecRule string, metric string, val int) {
if _, ok := s[appsecEngine]; !ok {
s[appsecEngine] = make(map[string]map[string]int)
}
if _, ok := s[appsecEngine][appsecRule]; !ok {
s[appsecEngine][appsecRule] = make(map[string]int)
}
s[appsecEngine][appsecRule][metric] += val
}
func (s statAppsecRule) Table(out io.Writer, wantColor string, noUnit bool, showEmpty bool) {
// TODO: sort keys
for appsecEngine, appsecEngineRulesStats := range s {
t := cstable.New(out, wantColor).Writer
t.AppendHeader(table.Row{"Rule ID", "Triggered"})
keys := []string{"triggered"}
if numRows, err := metricsToTable(t, appsecEngineRulesStats, keys, noUnit); err != nil {
log.Warningf("while collecting appsec rules stats: %s", err)
} else if numRows > 0 || showEmpty {
io.WriteString(out, fmt.Sprintf("Appsec '%s' Rules Metrics:\n", appsecEngine))
io.WriteString(out, t.Render() + "\n")
io.WriteString(out, "\n")
}
}
}