diff --git a/filters/filter_inhibited.go b/filters/filter_inhibited.go new file mode 100644 index 000000000..d435db30d --- /dev/null +++ b/filters/filter_inhibited.go @@ -0,0 +1,72 @@ +package filters + +import ( + "fmt" + "strings" + + "github.com/cloudflare/unsee/models" +) + +type inhibitedFilter struct { + alertFilter +} + +func (filter *inhibitedFilter) init(name string, matcher *matcherT, rawText string, isValid bool, value string) { + filter.Matched = name + if matcher != nil { + filter.Matcher = *matcher + } + filter.RawText = rawText + filter.IsValid = isValid + switch value { + case "true": + filter.Value = true + case "false": + filter.Value = false + default: + filter.IsValid = false + } +} + +func (filter *inhibitedFilter) Match(alert *models.Alert, matches int) bool { + if filter.IsValid { + isMatch := filter.Matcher.Compare(alert.Inhibited, filter.Value) + if isMatch { + filter.Hits++ + } + return isMatch + } + e := fmt.Sprintf("Match() called on invalid filter %#v", filter) + panic(e) +} + +func newInhibitedFilter() FilterT { + f := inhibitedFilter{} + return &f +} + +func inhibitedAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete { + tokens := []models.Autocomplete{} + for _, operator := range operators { + switch operator { + case equalOperator: + tokens = append(tokens, makeAC( + fmt.Sprintf("%s%strue", name, operator), + []string{ + name, + strings.TrimPrefix(name, "@"), + fmt.Sprintf("%s%s", name, operator), + }, + )) + tokens = append(tokens, makeAC( + fmt.Sprintf("%s%sfalse", name, operator), + []string{ + name, + strings.TrimPrefix(name, "@"), + fmt.Sprintf("%s%s", name, operator), + }, + )) + } + } + return tokens +} diff --git a/filters/filter_test.go b/filters/filter_test.go index ad7709f08..7fa39ba5e 100644 --- a/filters/filter_test.go +++ b/filters/filter_test.go @@ -48,6 +48,41 @@ var tests = []filterTest{ IsValid: false, }, + filterTest{ + Expression: "@inhibited=true", + IsValid: true, + Alert: models.Alert{}, + IsMatch: false, + }, + filterTest{ + Expression: "@inhibited!=true", + IsValid: true, + Alert: models.Alert{}, + IsMatch: true, + }, + filterTest{ + Expression: "@inhibited=true", + IsValid: true, + Alert: models.Alert{Inhibited: true}, + IsMatch: true, + }, + filterTest{ + Expression: "@inhibited=true", + IsValid: true, + Alert: models.Alert{Inhibited: false}, + IsMatch: false, + }, + filterTest{ + Expression: "@inhibited!=true", + IsValid: true, + Alert: models.Alert{Inhibited: true}, + IsMatch: false, + }, + filterTest{ + Expression: "@inhibited=xx", + IsValid: false, + }, + filterTest{ Expression: "@silence_jira=1", IsValid: true, diff --git a/filters/registry.go b/filters/registry.go index cdf4c29db..8fbf2bc69 100644 --- a/filters/registry.go +++ b/filters/registry.go @@ -35,6 +35,12 @@ var AllFilters = []filterConfig{ Factory: newSilencedFilter, Autocomplete: silencedAutocomplete, }, + filterConfig{ + Label: "@inhibited", + SupportedOperators: []string{equalOperator, notEqualOperator}, + Factory: newInhibitedFilter, + Autocomplete: inhibitedAutocomplete, + }, filterConfig{ Label: "@age", SupportedOperators: []string{lessThanOperator, moreThanOperator}, diff --git a/views_test.go b/views_test.go index d8695dde1..ccf443cdc 100644 --- a/views_test.go +++ b/views_test.go @@ -238,6 +238,8 @@ var acTests = []acTestCase{ "@age<1h", "@age>10m", "@age>1h", + "@inhibited=false", + "@inhibited=true", "@limit=10", "@limit=50", "@silence_author!=john@example.com",