crowdsec/cmd/crowdsec-cli/climetrics/statdecision.go
mmetc b93b240bd4
lint: enable (some) gocritic checks (#3238)
* lint: gocritic configuration
* lint: octal literals
* lint: gocritic (filepath.Join)
* ling: gocritic (badRegexp, regexpSimplify)
* lint: gocritic (preferStringWriter)
* lint: gocritic (emtpyStringTest)
* filepath.Clean(a+b) -> filepath.Join(a, b)
* gofumpt
2024-09-17 17:07:30 +02:00

60 lines
1.4 KiB
Go

package climetrics
import (
"io"
"strconv"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
)
type statDecision map[string]map[string]map[string]int
func (s statDecision) Description() (string, string) {
return "Local API Decisions",
`Provides information about all currently active decisions. ` +
`Includes both local (crowdsec) and global decisions (CAPI), and lists subscriptions (lists).`
}
func (s statDecision) Process(reason, origin, action string, val int) {
if _, ok := s[reason]; !ok {
s[reason] = make(map[string]map[string]int)
}
if _, ok := s[reason][origin]; !ok {
s[reason][origin] = make(map[string]int)
}
s[reason][origin][action] += val
}
func (s statDecision) Table(out io.Writer, wantColor string, noUnit bool, showEmpty bool) {
t := cstable.New(out, wantColor).Writer
t.AppendHeader(table.Row{"Reason", "Origin", "Action", "Count"})
numRows := 0
// TODO: sort by reason, origin, action
for reason, origins := range s {
for origin, actions := range origins {
for action, hits := range actions {
t.AppendRow(table.Row{
reason,
origin,
action,
strconv.Itoa(hits),
})
numRows++
}
}
}
if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
}
}