mirror of
https://github.com/prymitive/karma
synced 2026-05-19 04:26:41 +00:00
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package filters
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/cloudflare/unsee/models"
|
|
)
|
|
|
|
type limitFilter struct {
|
|
alertFilter
|
|
}
|
|
|
|
func (filter *limitFilter) 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
|
|
if filter.IsValid {
|
|
val, err := strconv.Atoi(value)
|
|
if err != nil || val < 1 {
|
|
filter.IsValid = false
|
|
} else {
|
|
filter.Value = val
|
|
}
|
|
}
|
|
}
|
|
|
|
func (filter *limitFilter) Match(alert *models.Alert, matches int) bool {
|
|
if filter.IsValid {
|
|
if matches < filter.Value.(int) {
|
|
return true
|
|
}
|
|
filter.Hits++
|
|
return false
|
|
}
|
|
e := fmt.Sprintf("Match() called on invalid filter %#v", filter)
|
|
panic(e)
|
|
}
|
|
|
|
func newLimitFilter() FilterT {
|
|
f := limitFilter{}
|
|
return &f
|
|
}
|
|
|
|
func limitAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
|
tokens := []models.Autocomplete{}
|
|
for _, operator := range operators {
|
|
tokens = append(tokens, makeAC(
|
|
fmt.Sprintf("%s%s10", name, operator),
|
|
[]string{
|
|
name,
|
|
strings.TrimPrefix(name, "@"),
|
|
fmt.Sprintf("%s%s", name, operator),
|
|
},
|
|
))
|
|
tokens = append(tokens, makeAC(
|
|
fmt.Sprintf("%s%s50", name, operator),
|
|
[]string{
|
|
name,
|
|
strings.TrimPrefix(name, "@"),
|
|
fmt.Sprintf("%s%s", name, operator),
|
|
},
|
|
))
|
|
}
|
|
return tokens
|
|
}
|