Add @inhibited filter

This commit is contained in:
Łukasz Mierzwa
2017-04-17 16:01:52 -07:00
parent 8f3fd319f4
commit 0af0401c6d
4 changed files with 115 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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,

View File

@@ -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},

View File

@@ -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",