-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to filter logs by status
- Loading branch information
Showing
11 changed files
with
385 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package filter | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Filter is a collection of rules | ||
type Filter struct { | ||
Rules []Rule | ||
} | ||
|
||
// NewFilter returns a new filter with a set number of rules | ||
func NewFilter() *Filter { | ||
filter := Filter{ | ||
Rules: []Rule{ | ||
RuleIsValue, | ||
RuleIsNotValue, | ||
RuleIsInRange, | ||
RuleIsNotInRange, | ||
RuleIsGreaterThan, | ||
RuleIsSmallerThan, | ||
}, | ||
} | ||
return &filter | ||
} | ||
|
||
// IsValid checks if the query is valid for the given value | ||
func (f *Filter) IsValid(query string, value int64) bool { | ||
queries := strings.Split(query, ",") | ||
|
||
result := false | ||
|
||
for _, q := range queries { | ||
q = strings.TrimSpace(q) | ||
|
||
for _, rule := range f.Rules { | ||
res := rule(q, value) | ||
if res.DoesApply() { | ||
result = result || res.AsBool() | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package filter | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFilterIsValidSimple(t *testing.T) { | ||
filter := NewFilter() | ||
assert.True(t, filter.IsValid("200", 200)) | ||
assert.False(t, filter.IsValid("200", 404)) | ||
assert.False(t, filter.IsValid("!200", 200)) | ||
assert.True(t, filter.IsValid("!200", 404)) | ||
} | ||
|
||
func TestFilterIsValidRange(t *testing.T) { | ||
filter := NewFilter() | ||
assert.True(t, filter.IsValid("200-299", 200)) | ||
assert.True(t, filter.IsValid("200-299", 218)) | ||
assert.True(t, filter.IsValid("200-299", 299)) | ||
assert.False(t, filter.IsValid("200-299", 404)) | ||
} | ||
|
||
func TestFilterIsValidMultipleRanges(t *testing.T) { | ||
filter := NewFilter() | ||
assert.True(t, filter.IsValid("200-299,400-499", 218)) | ||
assert.True(t, filter.IsValid("200-299,400-499", 404)) | ||
assert.False(t, filter.IsValid("200-299,400-499", 500)) | ||
} | ||
|
||
func TestFilterIsValidNotInRange(t *testing.T) { | ||
filter := NewFilter() | ||
assert.True(t, filter.IsValid("!200-299", 404)) | ||
assert.True(t, filter.IsValid("!200-299,400-499", 404)) | ||
} | ||
|
||
func TestFilterIsValidRangeWithExtras(t *testing.T) { | ||
filter := NewFilter() | ||
assert.True(t, filter.IsValid("200-299,404", 200)) | ||
assert.True(t, filter.IsValid("200-299,404", 218)) | ||
assert.True(t, filter.IsValid("200-299,404", 299)) | ||
assert.True(t, filter.IsValid("200-299,404", 404)) | ||
assert.False(t, filter.IsValid("200-299,404", 500)) | ||
assert.True(t, filter.IsValid("200-299,404,500", 500)) | ||
} | ||
|
||
func TestFilterIsValidGreaterSmallerThan(t *testing.T) { | ||
filter := NewFilter() | ||
assert.True(t, filter.IsValid(">400", 500)) | ||
assert.True(t, filter.IsValid("<400,404", 404)) | ||
assert.True(t, filter.IsValid(">200,<299", 218)) | ||
} | ||
|
||
func TestFilterInvalidCases(t *testing.T) { | ||
filter := NewFilter() | ||
// should be true because one of them is correct | ||
assert.True(t, filter.IsValid("200-299,!218", 218)) | ||
// should be true because 404 is not !200-299 | ||
assert.True(t, filter.IsValid("!200-299,!400-499", 404)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package filter | ||
|
||
// RuleResult is the result of a rule execution | ||
type RuleResult uint8 | ||
|
||
const ( | ||
// ResultFalse means the Rule did fail | ||
ResultFalse RuleResult = iota | ||
// ResultTrue means the Rule succeeded | ||
ResultTrue | ||
// ResultDoesNotApply means the Rule was not applicable for the given query | ||
ResultDoesNotApply | ||
) | ||
|
||
// Rule is a function that returns a result depending on the given query and value | ||
type Rule func(query string, value int64) RuleResult | ||
|
||
// AsBool converts result into boolean, keep in mind that a rule that did not apply | ||
// will always return true | ||
func (res RuleResult) AsBool() bool { | ||
if !res.DoesApply() { | ||
return true | ||
} | ||
return res == ResultTrue | ||
} | ||
|
||
// DoesApply checks if the rule did apply to the rule | ||
func (res RuleResult) DoesApply() bool { | ||
return res != ResultDoesNotApply | ||
} | ||
|
||
// Invert the boolean result, ResultDoesNotApply will stay the same though | ||
func (res RuleResult) Invert() RuleResult { | ||
if res == ResultDoesNotApply { | ||
return ResultDoesNotApply | ||
} | ||
|
||
if res == ResultFalse { | ||
return ResultTrue | ||
} | ||
|
||
return ResultFalse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package filter | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestAsBool(t *testing.T) { | ||
assert.True(t, ResultTrue.AsBool()) | ||
assert.False(t, ResultFalse.AsBool()) | ||
assert.True(t, ResultDoesNotApply.AsBool()) | ||
} | ||
|
||
func TestDoesApply(t *testing.T) { | ||
assert.False(t, ResultDoesNotApply.DoesApply()) | ||
assert.True(t, ResultTrue.DoesApply()) | ||
assert.True(t, ResultFalse.DoesApply()) | ||
} | ||
|
||
func TestInvert(t *testing.T) { | ||
assert.Equal(t, ResultTrue, ResultFalse.Invert()) | ||
assert.Equal(t, ResultFalse, ResultTrue.Invert()) | ||
assert.Equal(t, ResultDoesNotApply, ResultDoesNotApply.Invert()) | ||
} |
Oops, something went wrong.