File: utils_table.go

package info (click to toggle)
crowdsec 1.4.6-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,500 kB
  • sloc: sh: 2,870; makefile: 386; python: 74
file content (66 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"fmt"
	"io"

	"github.com/aquasecurity/table"
	"github.com/enescakir/emoji"

	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)

func listHubItemTable(out io.Writer, title string, statuses []cwhub.ItemHubStatus) {
	t := newLightTable(out)
	t.SetHeaders("Name", fmt.Sprintf("%v Status", emoji.Package), "Version", "Local Path")
	t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
	t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)

	for _, status := range statuses {
		t.AddRow(status.Name, status.UTF8_Status, status.LocalVersion, status.LocalPath)
	}
	renderTableTitle(out, title)
	t.Render()
}

func scenarioMetricsTable(out io.Writer, itemName string, metrics map[string]int) {
	if metrics["instantiation"] == 0 {
		return
	}
	t := newTable(out)
	t.SetHeaders("Current Count", "Overflows", "Instantiated", "Poured", "Expired")

	t.AddRow(
		fmt.Sprintf("%d", metrics["curr_count"]),
		fmt.Sprintf("%d", metrics["overflow"]),
		fmt.Sprintf("%d", metrics["instantiation"]),
		fmt.Sprintf("%d", metrics["pour"]),
		fmt.Sprintf("%d", metrics["underflow"]),
	)

	renderTableTitle(out, fmt.Sprintf("\n - (Scenario) %s:", itemName))
	t.Render()
}

func parserMetricsTable(out io.Writer, itemName string, metrics map[string]map[string]int) {
	skip := true
	t := newTable(out)
	t.SetHeaders("Parsers", "Hits", "Parsed", "Unparsed")

	for source, stats := range metrics {
		if stats["hits"] > 0 {
			t.AddRow(
				source,
				fmt.Sprintf("%d", stats["hits"]),
				fmt.Sprintf("%d", stats["parsed"]),
				fmt.Sprintf("%d", stats["unparsed"]),
			)
			skip = false
		}
	}

	if !skip {
		renderTableTitle(out, fmt.Sprintf("\n - (Parser) %s:", itemName))
		t.Render()
	}
}