Skip to content

Commit

Permalink
pkg/dumps.DumpTree: split to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed May 21, 2024
1 parent 20e44cd commit 6918737
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
linters-settings:
cyclop:
# lower this after refactoring
max-complexity: 48
max-complexity: 45

gci:
sections:
Expand All @@ -18,11 +18,11 @@ linters-settings:

gocognit:
# lower this after refactoring
min-complexity: 145
min-complexity: 128

gocyclo:
# lower this after refactoring
min-complexity: 48
min-complexity: 45

funlen:
# Checks the number of lines in a function.
Expand Down Expand Up @@ -55,7 +55,7 @@ linters-settings:

nestif:
# lower this after refactoring
min-complexity: 28
min-complexity: 24

nlreturn:
block-size: 5
Expand Down
84 changes: 53 additions & 31 deletions pkg/dumps/parser_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,79 +92,101 @@ func LoadParserDump(filepath string) (*ParserResults, error) {
return &pdump, nil
}

func DumpTree(parserResults ParserResults, bucketPour BucketPourInfo, opts DumpOpts) {
type tree struct {
// note : we can use line -> time as the unique identifier (of acquisition)
state := make(map[time.Time]map[string]map[string]ParserResult)
assoc := make(map[time.Time]string, 0)
parser_order := make(map[string][]string)
state map[time.Time]map[string]map[string]ParserResult
assoc map[time.Time]string
parserOrder map[string][]string
}

func newTree() *tree {
return &tree{
state: make(map[time.Time]map[string]map[string]ParserResult),
assoc: make(map[time.Time]string),
parserOrder: make(map[string][]string),
}

Check warning on line 107 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L102-L107

Added lines #L102 - L107 were not covered by tests
}

func DumpTree(parserResults ParserResults, bucketPour BucketPourInfo, opts DumpOpts) {
t := newTree()
t.processEvents(parserResults)
t.processBuckets(bucketPour)
t.displayResults(opts)

Check warning on line 114 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L110-L114

Added lines #L110 - L114 were not covered by tests
}

func (t *tree) processEvents(parserResults ParserResults) {

Check warning on line 117 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L117

Added line #L117 was not covered by tests
for stage, parsers := range parserResults {
// let's process parsers in the order according to idx
parser_order[stage] = make([]string, len(parsers))
t.parserOrder[stage] = make([]string, len(parsers))

Check warning on line 120 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L120

Added line #L120 was not covered by tests

for pname, parser := range parsers {
if len(parser) > 0 {
parser_order[stage][parser[0].Idx-1] = pname
t.parserOrder[stage][parser[0].Idx-1] = pname

Check warning on line 124 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L124

Added line #L124 was not covered by tests
}
}

for _, parser := range parser_order[stage] {
for _, parser := range t.parserOrder[stage] {

Check warning on line 128 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L128

Added line #L128 was not covered by tests
results := parsers[parser]
for _, parserRes := range results {
evt := parserRes.Evt
if _, ok := state[evt.Line.Time]; !ok {
state[evt.Line.Time] = make(map[string]map[string]ParserResult)
assoc[evt.Line.Time] = evt.Line.Raw
if _, ok := t.state[evt.Line.Time]; !ok {
t.state[evt.Line.Time] = make(map[string]map[string]ParserResult)
t.assoc[evt.Line.Time] = evt.Line.Raw

Check warning on line 134 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L132-L134

Added lines #L132 - L134 were not covered by tests
}

if _, ok := state[evt.Line.Time][stage]; !ok {
state[evt.Line.Time][stage] = make(map[string]ParserResult)
if _, ok := t.state[evt.Line.Time][stage]; !ok {
t.state[evt.Line.Time][stage] = make(map[string]ParserResult)

Check warning on line 138 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L137-L138

Added lines #L137 - L138 were not covered by tests
}

state[evt.Line.Time][stage][parser] = ParserResult{Evt: evt, Success: parserRes.Success}
t.state[evt.Line.Time][stage][parser] = ParserResult{Evt: evt, Success: parserRes.Success}

Check warning on line 141 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L141

Added line #L141 was not covered by tests
}
}
}
}

func (t *tree) processBuckets(bucketPour BucketPourInfo) {

Check warning on line 147 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L147

Added line #L147 was not covered by tests
for bname, evtlist := range bucketPour {
for _, evt := range evtlist {
if evt.Line.Raw == "" {
continue
}

// it might be bucket overflow being reprocessed, skip this
if _, ok := state[evt.Line.Time]; !ok {
state[evt.Line.Time] = make(map[string]map[string]ParserResult)
assoc[evt.Line.Time] = evt.Line.Raw
if _, ok := t.state[evt.Line.Time]; !ok {
t.state[evt.Line.Time] = make(map[string]map[string]ParserResult)
t.assoc[evt.Line.Time] = evt.Line.Raw

Check warning on line 157 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L155-L157

Added lines #L155 - L157 were not covered by tests
}

// there is a trick : to know if an event successfully exit the parsers, we check if it reached the pour() phase
// we thus use a fake stage "buckets" and a fake parser "OK" to know if it entered
if _, ok := state[evt.Line.Time]["buckets"]; !ok {
state[evt.Line.Time]["buckets"] = make(map[string]ParserResult)
if _, ok := t.state[evt.Line.Time]["buckets"]; !ok {
t.state[evt.Line.Time]["buckets"] = make(map[string]ParserResult)

Check warning on line 163 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L162-L163

Added lines #L162 - L163 were not covered by tests
}

state[evt.Line.Time]["buckets"][bname] = ParserResult{Success: true}
t.state[evt.Line.Time]["buckets"][bname] = ParserResult{Success: true}

Check warning on line 166 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L166

Added line #L166 was not covered by tests
}
}
}

func (t *tree) displayResults(opts DumpOpts) {

Check warning on line 171 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L171

Added line #L171 was not covered by tests
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
whitelistReason := ""

Check warning on line 176 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L176

Added line #L176 was not covered by tests
// get each line
for tstamp, rawstr := range assoc {
for tstamp, rawstr := range t.assoc {

Check warning on line 178 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L178

Added line #L178 was not covered by tests
if opts.SkipOk {
if _, ok := state[tstamp]["buckets"]["OK"]; ok {
if _, ok := t.state[tstamp]["buckets"]["OK"]; ok {

Check warning on line 180 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L180

Added line #L180 was not covered by tests
continue
}
}

fmt.Printf("line: %s\n", rawstr)

skeys := make([]string, 0, len(state[tstamp]))
skeys := make([]string, 0, len(t.state[tstamp]))

Check warning on line 187 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L187

Added line #L187 was not covered by tests

for k := range state[tstamp] {
for k := range t.state[tstamp] {

Check warning on line 189 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L189

Added line #L189 was not covered by tests
// there is a trick : to know if an event successfully exit the parsers, we check if it reached the pour() phase
// we thus use a fake stage "buckets" and a fake parser "OK" to know if it entered
if k == "buckets" {
Expand All @@ -180,18 +202,18 @@ func DumpTree(parserResults ParserResults, bucketPour BucketPourInfo, opts DumpO
var prevItem types.Event

for _, stage := range skeys {
parsers := state[tstamp][stage]
parsers := t.state[tstamp][stage]

Check warning on line 205 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L205

Added line #L205 was not covered by tests

sep := "├"
presep := "|"

fmt.Printf("\t%s %s\n", sep, stage)

for idx, parser := range parser_order[stage] {
for idx, parser := range t.parserOrder[stage] {

Check warning on line 212 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L212

Added line #L212 was not covered by tests
res := parsers[parser].Success
sep := "├"

if idx == len(parser_order[stage])-1 {
if idx == len(t.parserOrder[stage])-1 {

Check warning on line 216 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L216

Added line #L216 was not covered by tests
sep = "└"
}

Expand Down Expand Up @@ -278,12 +300,12 @@ func DumpTree(parserResults ParserResults, bucketPour BucketPourInfo, opts DumpO

sep := "└"

if len(state[tstamp]["buckets"]) > 0 {
if len(t.state[tstamp]["buckets"]) > 0 {

Check warning on line 303 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L303

Added line #L303 was not covered by tests
sep = "├"
}

// did the event enter the bucket pour phase ?
if _, ok := state[tstamp]["buckets"]["OK"]; ok {
if _, ok := t.state[tstamp]["buckets"]["OK"]; ok {

Check warning on line 308 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L308

Added line #L308 was not covered by tests
fmt.Printf("\t%s-------- parser success %s\n", sep, emoji.GreenCircle)
} else if whitelistReason != "" {
fmt.Printf("\t%s-------- parser success, ignored by whitelist (%s) %s\n", sep, whitelistReason, emoji.GreenCircle)
Expand All @@ -292,13 +314,13 @@ func DumpTree(parserResults ParserResults, bucketPour BucketPourInfo, opts DumpO
}

// now print bucket info
if len(state[tstamp]["buckets"]) > 0 {
if len(t.state[tstamp]["buckets"]) > 0 {

Check warning on line 317 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L317

Added line #L317 was not covered by tests
fmt.Printf("\t├ Scenarios\n")
}

bnames := make([]string, 0, len(state[tstamp]["buckets"]))
bnames := make([]string, 0, len(t.state[tstamp]["buckets"]))

Check warning on line 321 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L321

Added line #L321 was not covered by tests

for k := range state[tstamp]["buckets"] {
for k := range t.state[tstamp]["buckets"] {

Check warning on line 323 in pkg/dumps/parser_dump.go

View check run for this annotation

Codecov / codecov/patch

pkg/dumps/parser_dump.go#L323

Added line #L323 was not covered by tests
// there is a trick : to know if an event successfully exit the parsers, we check if it reached the pour() phase
// we thus use a fake stage "buckets" and a fake parser "OK" to know if it entered
if k == "OK" {
Expand Down

0 comments on commit 6918737

Please sign in to comment.