Skip to content

Allow filtering silences by creator #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ func (api *API) getSilencesHandler(params silence_ops.GetSilencesParams) middlew
if !CheckSilenceMatchesFilterLabels(ps, matchers) {
continue
}
if !CheckSilenceCreator(ps, params.Creator) {
continue
}
silence, err := GettableSilenceFromProto(ps)
if err != nil {
level.Error(logger).Log("msg", "Failed to unmarshal silence from proto", "err", err)
Expand Down Expand Up @@ -589,6 +592,20 @@ func CheckSilenceMatchesFilterLabels(s *silencepb.Silence, matchers []*labels.Ma
return true
}

// CheckSilenceCreator returns true if a given silence
// matches a list of creators.
func CheckSilenceCreator(s *silencepb.Silence, creators []string) bool {
if len(creators) < 1 {
return true
}
for _, creator := range creators {
if creator == s.CreatedBy {
return true
}
}
return false
}

func (api *API) getSilenceHandler(params silence_ops.GetSilenceParams) middleware.Responder {
logger := api.requestLogger(params.HTTPRequest)

Expand Down
24 changes: 24 additions & 0 deletions api/v2/client/silence/get_silences_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions api/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ paths:
collectionFormat: multi
items:
type: string
- name: creator
in: query
description: A list of creators to filter silences by
required: false
type: array
collectionFormat: multi
items:
type: string
post:
tags:
- silence
Expand Down
20 changes: 20 additions & 0 deletions api/v2/restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions api/v2/restapi/operations/silence/get_silences_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion api/v2/restapi/operations/silence/get_silences_urlbuilder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions asset/assets_vfsdata.go

Large diffs are not rendered by default.

92 changes: 86 additions & 6 deletions ui/app/src/Utils/Filter.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Utils.Filter exposing
, fromApiMatcher
, generateAPIQueryString
, nullFilter
, parseCreatedBy
, parseCreatedByList
, parseFilter
, parseGroup
, parseMatcher
Expand All @@ -17,7 +19,7 @@ module Utils.Filter exposing
, stringifyMatcher
, toApiMatcher
, toUrl
, withMatchers
, withMatchersAndCreatedBy
)

import Char
Expand All @@ -29,6 +31,7 @@ import Url exposing (percentEncode)

type alias Filter =
{ text : Maybe String
, createdByList : Maybe String
, group : Maybe String
, customGrouping : Bool
, receiver : Maybe String
Expand All @@ -41,6 +44,7 @@ type alias Filter =
nullFilter : Filter
nullFilter =
{ text = Nothing
, createdByList = Nothing
, group = Nothing
, customGrouping = False
, receiver = Nothing
Expand All @@ -56,13 +60,14 @@ generateQueryParam name =


toUrl : String -> Filter -> String
toUrl baseUrl { receiver, customGrouping, showSilenced, showInhibited, showActive, text, group } =
toUrl baseUrl { receiver, customGrouping, showSilenced, showInhibited, showActive, text, createdByList, group } =
let
parts =
[ ( "silenced", Maybe.withDefault False showSilenced |> boolToString |> Just )
, ( "inhibited", Maybe.withDefault False showInhibited |> boolToString |> Just )
, ( "active", Maybe.withDefault True showActive |> boolToString |> Just )
, ( "filter", emptyToNothing text )
, ( "creator", emptyToNothing createdByList )
, ( "receiver", emptyToNothing receiver )
, ( "group", group )
, ( "customGrouping", boolToMaybeString customGrouping )
Expand All @@ -81,7 +86,7 @@ toUrl baseUrl { receiver, customGrouping, showSilenced, showInhibited, showActiv


generateAPIQueryString : Filter -> String
generateAPIQueryString { receiver, showSilenced, showInhibited, showActive, text, group } =
generateAPIQueryString { receiver, showSilenced, showInhibited, showActive, text, createdByList, group } =
let
filter_ =
case parseFilter (Maybe.withDefault "" text) of
Expand All @@ -91,8 +96,17 @@ generateAPIQueryString { receiver, showSilenced, showInhibited, showActive, text
Nothing ->
[]

createdByList_ =
case parseCreatedByList (Maybe.withDefault "" createdByList) of
Just createdByList__ ->
List.map (Just >> Tuple.pair "creator") createdByList__

Nothing ->
[]

parts =
filter_
++ createdByList_
++ [ ( "silenced", Maybe.withDefault False showSilenced |> boolToString |> Just )
, ( "inhibited", Maybe.withDefault False showInhibited |> boolToString |> Just )
, ( "active", Maybe.withDefault True showActive |> boolToString |> Just )
Expand Down Expand Up @@ -227,6 +241,18 @@ parseMatcher =
>> Result.toMaybe


parseCreatedByList : String -> Maybe (List String)
parseCreatedByList =
Parser.run creators
>> Result.toMaybe


parseCreatedBy : String -> Maybe String
parseCreatedBy =
Parser.run creator
>> Result.toMaybe


stringifyGroup : List String -> Maybe String
stringifyGroup list =
if List.isEmpty list then
Expand Down Expand Up @@ -265,6 +291,20 @@ stringifyFilter matchers_ =
++ "}"


stringifyCreatedByList : List String -> String
stringifyCreatedByList createdByList_ =
case createdByList_ of
[] ->
""

list ->
(list
|> String.join ", "
|> (++) "{"
)
++ "}"


stringifyMatcher : Matcher -> String
stringifyMatcher { key, op, value } =
key
Expand Down Expand Up @@ -311,6 +351,29 @@ matcher =
|. Parser.end


creators : Parser (List String)
creators =
Parser.succeed identity
|= Parser.sequence
{ start = "{"
, separator = ","
, end = "}"
, spaces = Parser.spaces
, item = itemCreator
, trailing = Forbidden
}
|. Parser.end


creator : Parser String
creator =
Parser.succeed identity
|. Parser.spaces
|= itemCreator
|. Parser.spaces
|. Parser.end


item : Parser Matcher
item =
Parser.succeed Matcher
Expand All @@ -330,6 +393,15 @@ item =
|= string '"'


itemCreator : Parser String
itemCreator =
Parser.variable
{ start = isVarCharAndSymbol
, inner = isVarCharAndSymbol
, reserved = Set.empty
}


string : Char -> Parser String
string separator =
Parser.succeed ()
Expand Down Expand Up @@ -361,9 +433,17 @@ isVarChar char =
|| Char.isDigit char


withMatchers : List Matcher -> Filter -> Filter
withMatchers matchers_ filter_ =
{ filter_ | text = Just (stringifyFilter matchers_) }
isVarCharAndSymbol : Char -> Bool
isVarCharAndSymbol char =
Char.isLower char
|| Char.isUpper char
|| String.contains (String.fromChar char) "_ .&-^[]:;*+?!#$%&()"
|| Char.isDigit char


withMatchersAndCreatedBy : List Matcher -> List String -> Filter -> Filter
withMatchersAndCreatedBy matchers_ createdByList_ filter_ =
{ filter_ | text = Just (stringifyFilter matchers_), createdByList = Just (stringifyCreatedByList createdByList_) }


silencePreviewFilter : List Data.Matcher.Matcher -> Filter
Expand Down
Loading