utils_table.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "strconv"
  6. "github.com/aquasecurity/table"
  7. "github.com/enescakir/emoji"
  8. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  9. )
  10. func listHubItemTable(out io.Writer, title string, items []*cwhub.Item) {
  11. t := newLightTable(out)
  12. t.SetHeaders("Name", fmt.Sprintf("%v Status", emoji.Package), "Version", "Local Path")
  13. t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
  14. t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
  15. for _, item := range items {
  16. status := fmt.Sprintf("%v %s", item.State.Emoji(), item.State.Text())
  17. t.AddRow(item.Name, status, item.State.LocalVersion, item.State.LocalPath)
  18. }
  19. renderTableTitle(out, title)
  20. t.Render()
  21. }
  22. func appsecMetricsTable(out io.Writer, itemName string, metrics map[string]int) {
  23. t := newTable(out)
  24. t.SetHeaders("Inband Hits", "Outband Hits")
  25. t.AddRow(
  26. strconv.Itoa(metrics["inband_hits"]),
  27. strconv.Itoa(metrics["outband_hits"]),
  28. )
  29. renderTableTitle(out, fmt.Sprintf("\n - (AppSec Rule) %s:", itemName))
  30. t.Render()
  31. }
  32. func scenarioMetricsTable(out io.Writer, itemName string, metrics map[string]int) {
  33. if metrics["instantiation"] == 0 {
  34. return
  35. }
  36. t := newTable(out)
  37. t.SetHeaders("Current Count", "Overflows", "Instantiated", "Poured", "Expired")
  38. t.AddRow(
  39. strconv.Itoa(metrics["curr_count"]),
  40. strconv.Itoa(metrics["overflow"]),
  41. strconv.Itoa(metrics["instantiation"]),
  42. strconv.Itoa(metrics["pour"]),
  43. strconv.Itoa(metrics["underflow"]),
  44. )
  45. renderTableTitle(out, fmt.Sprintf("\n - (Scenario) %s:", itemName))
  46. t.Render()
  47. }
  48. func parserMetricsTable(out io.Writer, itemName string, metrics map[string]map[string]int) {
  49. t := newTable(out)
  50. t.SetHeaders("Parsers", "Hits", "Parsed", "Unparsed")
  51. // don't show table if no hits
  52. showTable := false
  53. for source, stats := range metrics {
  54. if stats["hits"] > 0 {
  55. t.AddRow(
  56. source,
  57. strconv.Itoa(stats["hits"]),
  58. strconv.Itoa(stats["parsed"]),
  59. strconv.Itoa(stats["unparsed"]),
  60. )
  61. showTable = true
  62. }
  63. }
  64. if showTable {
  65. renderTableTitle(out, fmt.Sprintf("\n - (Parser) %s:", itemName))
  66. t.Render()
  67. }
  68. }