Skip to content

Commit

Permalink
Split statistics struc
Browse files Browse the repository at this point in the history
  • Loading branch information
rgreinho committed Jun 29, 2022
1 parent b91a353 commit adb273d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
34 changes: 2 additions & 32 deletions internal/printer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ var (
output io.Writer = os.Stdout
)

type Statistics struct {
Passed int
Failed int
Unknown int
Total int
}
type reportResult struct {
ID string
Name string
Expand Down Expand Up @@ -70,12 +64,7 @@ func PrintOutputToFile(data []checkmodels.CheckRunResult, outputFilePath string)

func getPrintFormat(results []checkmodels.CheckRunResult) ([]reportResult, Statistics) {
resultsToDisplay := []reportResult{}
statistics := Statistics{
Passed: 0,
Failed: 0,
Unknown: 0,
Total: len(results),
}
statistics := NewStatistics()

for _, r := range results {
resultsToDisplay = append(resultsToDisplay, reportResult{
Expand All @@ -87,31 +76,12 @@ func getPrintFormat(results []checkmodels.CheckRunResult) ([]reportResult, Stati
Reason: r.Result.Details,
Url: r.Metadata.Url})

switch r.Result.Status {
case "Passed":
statistics.Passed += 1
case "Failed":
statistics.Failed += 1
case "Unknown":
statistics.Unknown += 1
}
statistics.Add(r.Result.Status)
}

return resultsToDisplay, statistics
}

func initializeStatistics() Statistics {
return Statistics{Passed: 0, Failed: 0, Unknown: 0, Total: 0}
}

func addToStatistics(s *Statistics, r checkmodels.ResultStatus) {
if r == checkmodels.Passed {
s.Passed++
} else {
s.Failed++
}
}

// PrintErrorf prints a message with error color
func PrintError(msg string) {
println(fmt.Sprint(ColorRed, msg))
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func PrintFindings(results []checkmodels.CheckRunResult, outputFilePath string,
PrintOutputToFile(results, outputFilePath)
}
if !isQuiet {
s := initializeStatistics()
s := NewStatistics()
table.Header = CreateHeader([]string{"ID", "Name", "Result", "Reason"})
for _, row := range results {
rowData := []CellData{
Expand All @@ -39,7 +39,7 @@ func PrintFindings(results []checkmodels.CheckRunResult, outputFilePath string,
{text: row.Result.Details},
}
table.Body.Cells = append(table.Body.Cells, CreateBodyRow(rowData))
addToStatistics(&s, row.Result.Status)
s.Add(row.Result.Status)
}
table.Footer = CreateFooter(s, len(table.Header.Cells))
fmt.Println(table.String())
Expand Down
54 changes: 54 additions & 0 deletions internal/printer/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package printer

import (
"fmt"

"github.com/aquasecurity/chain-bench/internal/models/checkmodels"
)

type Statistics struct {
Passed int
Failed int
Unknown int
Total int
}

// NewStatistics initializes a new Statistics struct.
func NewStatistics() Statistics {
return Statistics{Passed: 0, Failed: 0, Unknown: 0, Total: 0}
}

// Add increments the value of a specific field as well as the total value.
func (s *Statistics) Add(value checkmodels.ResultStatus) error {
switch value {
case checkmodels.Passed:
s.Passed++
case checkmodels.Failed:
s.Failed++
case checkmodels.Unknown:
s.Unknown++
default:
return fmt.Errorf("unknown statistical value: %s", value)
}
s.Total++

return nil
}

// Sub decrements the value of a specific field as well as the total value.
func (s *Statistics) Sub(value checkmodels.ResultStatus) error {
switch value {
case checkmodels.Passed:
s.Passed--
case checkmodels.Failed:
s.Failed--
case checkmodels.Unknown:
s.Unknown--
default:
return fmt.Errorf("unknown statistical value: %s", value)
}

s.Total--

return nil
}

0 comments on commit adb273d

Please sign in to comment.