mirror of
https://github.com/prymitive/karma
synced 2026-05-09 03:36:44 +00:00
Add @inhibited filter
This commit is contained in:
72
filters/filter_inhibited.go
Normal file
72
filters/filter_inhibited.go
Normal 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
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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},
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user