mirror of
https://github.com/prometheus/alertmanager
synced 2024-12-25 07:32:19 +00:00
Correctly construct filter query string for api
Updating the api in
bc373f562f
resulted in the UI sending query strings for the
filter that no longer match what the backend
expects. This brings that in to line.
Signed-off-by: stuart nelson <stuartnelson3@gmail.com>
This commit is contained in:
parent
d3e3585719
commit
1f41731881
File diff suppressed because one or more lines are too long
@ -5,7 +5,7 @@ import Data.Receiver exposing (Receiver)
|
||||
import Json.Decode
|
||||
import Regex
|
||||
import Utils.Api exposing (iso8601Time)
|
||||
import Utils.Filter exposing (Filter, generateQueryString)
|
||||
import Utils.Filter exposing (Filter, generateAPIQueryString)
|
||||
import Utils.Types exposing (ApiData)
|
||||
|
||||
|
||||
@ -22,6 +22,6 @@ fetchAlerts : String -> Filter -> Cmd (ApiData (List GettableAlert))
|
||||
fetchAlerts apiUrl filter =
|
||||
let
|
||||
url =
|
||||
String.join "/" [ apiUrl, "alerts" ++ generateQueryString filter ]
|
||||
String.join "/" [ apiUrl, "alerts" ++ generateAPIQueryString filter ]
|
||||
in
|
||||
Utils.Api.send (Utils.Api.get url (Json.Decode.list Data.GettableAlert.decoder))
|
||||
|
@ -2,18 +2,21 @@ module Utils.Filter exposing
|
||||
( Filter
|
||||
, MatchOperator(..)
|
||||
, Matcher
|
||||
, generateAPIQueryString
|
||||
, generateQueryParam
|
||||
, generateQueryString
|
||||
, nullFilter
|
||||
, parseFilter
|
||||
, parseGroup
|
||||
, parseMatcher
|
||||
, silencePreviewFilter
|
||||
, stringifyFilter
|
||||
, stringifyGroup
|
||||
, stringifyMatcher
|
||||
)
|
||||
|
||||
import Char
|
||||
import Data.Matcher
|
||||
import Parser exposing ((|.), (|=), Parser, Trailing(..))
|
||||
import Set
|
||||
import Url exposing (percentEncode)
|
||||
@ -64,6 +67,35 @@ generateQueryString { receiver, showSilenced, showInhibited, text, group } =
|
||||
""
|
||||
|
||||
|
||||
generateAPIQueryString : Filter -> String
|
||||
generateAPIQueryString { receiver, showSilenced, showInhibited, text, group } =
|
||||
let
|
||||
filter_ =
|
||||
case parseFilter (Maybe.withDefault "" text) of
|
||||
Just matchers_ ->
|
||||
List.map (stringifyMatcher >> Just >> Tuple.pair "filter") matchers_
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
|
||||
parts =
|
||||
filter_
|
||||
++ [ ( "silenced", Maybe.withDefault False showSilenced |> boolToString |> Just )
|
||||
, ( "inhibited", Maybe.withDefault False showInhibited |> boolToString |> Just )
|
||||
, ( "receiver", emptyToNothing receiver )
|
||||
, ( "group", group )
|
||||
]
|
||||
|> List.filterMap (\( a, b ) -> generateQueryParam a b)
|
||||
in
|
||||
if List.length parts > 0 then
|
||||
parts
|
||||
|> String.join "&"
|
||||
|> (++) "?"
|
||||
|
||||
else
|
||||
""
|
||||
|
||||
|
||||
boolToString : Bool -> String
|
||||
boolToString b =
|
||||
if b then
|
||||
@ -90,6 +122,19 @@ type alias Matcher =
|
||||
}
|
||||
|
||||
|
||||
convertAPIMatcher : Data.Matcher.Matcher -> Matcher
|
||||
convertAPIMatcher { name, value, isRegex } =
|
||||
{ key = name
|
||||
, value = value
|
||||
, op =
|
||||
if isRegex then
|
||||
RegexMatch
|
||||
|
||||
else
|
||||
Eq
|
||||
}
|
||||
|
||||
|
||||
type MatchOperator
|
||||
= Eq
|
||||
| NotEq
|
||||
@ -212,10 +257,6 @@ item =
|
||||
|= string '"'
|
||||
|
||||
|
||||
|
||||
--"
|
||||
|
||||
|
||||
string : Char -> Parser String
|
||||
string separator =
|
||||
Parser.succeed ()
|
||||
@ -245,3 +286,15 @@ isVarChar char =
|
||||
|| Char.isUpper char
|
||||
|| (char == '_')
|
||||
|| Char.isDigit char
|
||||
|
||||
|
||||
silencePreviewFilter : List Data.Matcher.Matcher -> Filter
|
||||
silencePreviewFilter apiMatchers =
|
||||
{ nullFilter
|
||||
| text =
|
||||
List.map convertAPIMatcher apiMatchers
|
||||
|> stringifyFilter
|
||||
|> Just
|
||||
, showSilenced = Just True
|
||||
, showInhibited = Just True
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
module Utils.List exposing (groupBy, lastElem, mjoin, mstring, nextElem, replaceIf, replaceIndex, zip)
|
||||
module Utils.List exposing (groupBy, lastElem, mstring, nextElem, replaceIf, replaceIndex, zip)
|
||||
|
||||
import Data.Matcher exposing (Matcher)
|
||||
import Dict exposing (Dict)
|
||||
@ -49,11 +49,6 @@ replaceIndex index replacement list =
|
||||
list
|
||||
|
||||
|
||||
mjoin : List Matcher -> String
|
||||
mjoin m =
|
||||
String.join "," (List.map mstring m)
|
||||
|
||||
|
||||
mstring : Matcher -> String
|
||||
mstring m =
|
||||
let
|
||||
|
@ -7,7 +7,7 @@ import Task
|
||||
import Time
|
||||
import Types exposing (Msg(..))
|
||||
import Utils.Date exposing (timeFromString)
|
||||
import Utils.Filter exposing (nullFilter)
|
||||
import Utils.Filter exposing (silencePreviewFilter)
|
||||
import Utils.FormValidation exposing (fromResult, stringNotEmpty, updateValue, validate)
|
||||
import Utils.List
|
||||
import Utils.Types exposing (ApiData(..))
|
||||
@ -237,7 +237,7 @@ update msg model basePath apiUrl =
|
||||
( { model | alerts = Loading }
|
||||
, Alerts.Api.fetchAlerts
|
||||
apiUrl
|
||||
{ nullFilter | text = Just (Utils.List.mjoin silence.matchers), showSilenced = Just True, showInhibited = Just True }
|
||||
(silencePreviewFilter silence.matchers)
|
||||
|> Cmd.map (AlertGroupsPreview >> MsgForSilenceForm)
|
||||
)
|
||||
|
||||
|
@ -3,7 +3,7 @@ module Views.SilenceView.Updates exposing (update)
|
||||
import Alerts.Api
|
||||
import Browser.Navigation as Navigation
|
||||
import Silences.Api exposing (getSilence)
|
||||
import Utils.Filter exposing (nullFilter)
|
||||
import Utils.Filter exposing (silencePreviewFilter)
|
||||
import Utils.List
|
||||
import Utils.Types exposing (ApiData(..))
|
||||
import Views.SilenceView.Types exposing (Model, SilenceViewMsg(..))
|
||||
@ -32,7 +32,7 @@ update msg model apiUrl =
|
||||
}
|
||||
, Alerts.Api.fetchAlerts
|
||||
apiUrl
|
||||
{ nullFilter | text = Just (Utils.List.mjoin silence.matchers), showSilenced = Just True }
|
||||
(silencePreviewFilter silence.matchers)
|
||||
|> Cmd.map AlertGroupsPreview
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user