Skip to content

Commit

Permalink
done getLargeLog()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebd committed Oct 16, 2023
1 parent 61468c0 commit 568c43f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 19 deletions.
43 changes: 24 additions & 19 deletions service/getLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func getLargeLog(params *GetLogParams) GetLogResult {
result = append(blockResult, result...)
}

return successGetLogResult(reduceToMaxLines(result, maxResultLines), strategy)
result = reduceToMaxLines(result, maxResultLines)

return successGetLogResult(result, strategy)
}

// getSmallLog returns the contents of a small log file that easily fits in memory
Expand All @@ -111,20 +113,22 @@ func getSmallLog(params *GetLogParams) GetLogResult {
}
result := strings.Split(string(byteSlice), "\n")

// Filter out lines that do not match the filters
if params.matchRequested() {
// Single pass through the slice for efficiency
result = slices.DeleteFunc(result, func(line string) bool {
// Cheaper tests first, short circuit more expensive tests
if params.textMatchRequested() && !strings.Contains(line, params.TextMatch) {
return true
}
// Expensive tests last
return params.regexMatchRequested() && !params.Regex.MatchString(line)
})
}
// Single pass through the slice for efficiency
result = slices.DeleteFunc(result, func(line string) bool {
// Cheaper tests first, short circuit more expensive tests
if len(line) == 0 {
return true
}
if params.textMatchRequested() && !strings.Contains(line, params.TextMatch) {
return true
}
// Expensive tests last
return params.regexMatchRequested() && !params.Regex.MatchString(line)
})

result = reduceToMaxLines(result, maxLines(params))

return successGetLogResult(reduceToMaxLines(result, maxLines(params)), strategy)
return successGetLogResult(result, strategy)
}

func maxLines(params *GetLogParams) int {
Expand All @@ -135,10 +139,11 @@ func maxLines(params *GetLogParams) int {
}

func reduceToMaxLines(result []string, maxLines int) []string {
if len(result) > maxLines {
endIndex := len(result) - 1
startIndex := max(0, endIndex-maxLines)
result = result[startIndex:endIndex]
if len(result) <= maxLines {
return result
}
return result

endIndex := len(result)
startIndex := max(0, endIndex-maxLines)
return result[startIndex:endIndex]
}
55 changes: 55 additions & 0 deletions service/maxLines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package service

import (
"fmt"
)

// based on config.MaxResultLines = 2_000

func Example_maxLines_none() {
fmt.Println(maxLines(&GetLogParams{}))
// Output:
// 2000
}

func Example_maxLines_zero() {
fmt.Println(maxLines(&GetLogParams{MaxLines: 0}))
// Output:
// 2000
}

func Example_maxLines_negative() {
fmt.Println(maxLines(&GetLogParams{MaxLines: -1}))
// Output:
// 2000
}

func Example_maxLines_one() {
fmt.Println(maxLines(&GetLogParams{MaxLines: 1}))
// Output:
// 1
}

func Example_maxLines_two() {
fmt.Println(maxLines(&GetLogParams{MaxLines: 2}))
// Output:
// 2
}

func Example_maxLines_nineteen_ninety_nine() {
fmt.Println(maxLines(&GetLogParams{MaxLines: 1_999}))
// Output:
// 1999
}

func Example_maxLines_two_thousand() {
fmt.Println(maxLines(&GetLogParams{MaxLines: 2_000}))
// Output:
// 2000
}

func Example_maxLines_two_thousand_one() {
fmt.Println(maxLines(&GetLogParams{MaxLines: 2_001}))
// Output:
// 2000
}
31 changes: 31 additions & 0 deletions service/reduceToMaxLines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package service

import (
"fmt"
)

func Example_reduceToMaxLines_0() {
fmt.Println([]string{})
fmt.Println(reduceToMaxLines([]string{"a", "b", "c"}, 0))
// Output:
// []
// []
}

func Example_reduceToMaxLines_1() {
fmt.Println(reduceToMaxLines([]string{"a", "b", "c"}, 1))
// Output:
// [c]
}

func Example_reduceToMaxLines_2() {
fmt.Println(reduceToMaxLines([]string{"a", "b", "c"}, 2))
// Output:
// [b c]
}

func Example_reduceToMaxLines_3() {
fmt.Println(reduceToMaxLines([]string{"a", "b", "c"}, 3))
// Output:
// [a b c]
}

0 comments on commit 568c43f

Please sign in to comment.