Add 'silenced' query param to /alerts endpoint

This adds the silenced query parameter to the /alerts API endpoint. In
addition it changes the default in the UI. Previously silenced alerts
were displayed by default. This PR hides silenced alerts by default.
This commit is contained in:
Max Leonard Inden 2017-05-17 10:14:42 +02:00
parent d73a655bf4
commit b9acb7a63f
No known key found for this signature in database
GPG Key ID: 5403C5464810BC26
7 changed files with 36 additions and 59 deletions

View File

@ -256,8 +256,9 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
err error
// Initialize result slice to prevent api returning `null` when there
// are no alerts present
res = []*APIAlert{}
matchers = []*labels.Matcher{}
res = []*APIAlert{}
matchers = []*labels.Matcher{}
showSilenced = true
)
if filter := r.FormValue("filter"); filter != "" {
@ -271,6 +272,21 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
}
}
if silencedParam := r.FormValue("silenced"); silencedParam != "" {
if silencedParam == "false" {
showSilenced = false
} else if silencedParam != "true" {
respondError(w, apiError{
typ: errorBadData,
err: fmt.Errorf(
"parameter 'silenced' can either be 'true' or 'false', not '%v'",
silencedParam,
),
}, nil)
return
}
}
alerts := api.alerts.GetPending()
defer alerts.Close()
@ -286,6 +302,10 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
status := api.getAlertStatus(a.Fingerprint())
if !showSilenced && len(status.SilencedBy) != 0 {
continue
}
apiAlert := &APIAlert{
Alert: &a.Alert,
Status: status,

View File

@ -48,7 +48,7 @@ generateQueryString { receiver, showSilenced, text, group } =
let
-- TODO: Re-add receiver once it is parsed on the server side.
parts =
[ ( "silenced", Maybe.map (toString >> String.toLower) showSilenced )
[ ( "silenced", Maybe.withDefault False showSilenced |> toString |> String.toLower |> Just )
, ( "filter", emptyToNothing text )
, ( "group", group )
]

View File

@ -1,4 +1,4 @@
module Views.AlertList.Filter exposing (silenced, matchers)
module Views.AlertList.Filter exposing (matchers)
import Alerts.Types exposing (Alert, AlertGroup, Block)
import Utils.Types exposing (Matchers)
@ -15,30 +15,6 @@ matchers matchers alerts =
alerts
silenced : Maybe Bool -> List Alert -> List Alert
silenced maybeShowSilenced alerts =
let
showSilenced =
Maybe.withDefault False maybeShowSilenced
in
if showSilenced then
alerts
else
List.filter (.silenced >> not) alerts
filterAlertGroup : (String -> Bool) -> AlertGroup -> Maybe AlertGroup
filterAlertGroup fn alertGroup =
let
blocks =
List.filter (\b -> fn b.routeOpts.receiver) alertGroup.blocks
in
if not <| List.isEmpty blocks then
Just { alertGroup | blocks = blocks }
else
Nothing
alertsFromBlock : (Alert -> Bool) -> Block -> Maybe Block
alertsFromBlock fn block =
let
@ -95,28 +71,6 @@ filterAlertGroupLabels matchers alertGroup =
Nothing
matchersToLabels : Utils.Types.Matchers -> Utils.Types.Labels
matchersToLabels matchers =
List.map (\m -> ( m.name, m.value )) matchers
alertGroupsSilenced : AlertGroup -> Maybe AlertGroup
alertGroupsSilenced alertGroup =
let
blocks =
List.filterMap filterSilencedAlerts alertGroup.blocks
in
if not <| List.isEmpty blocks then
Just { alertGroup | blocks = blocks }
else
Nothing
filterSilencedAlerts : Block -> Maybe Block
filterSilencedAlerts block =
alertsFromBlock (.silenced >> not) block
by : (a -> Maybe a) -> List a -> List a
by fn groups =
List.filterMap fn groups

View File

@ -11,7 +11,7 @@ import Utils.Types exposing (ApiResponse(Initial, Success, Loading, Failure), La
import Utils.Views exposing (buttonLink, listButton)
import Utils.List
import Views.AlertList.AlertView as AlertView
import Views.AlertList.Filter exposing (silenced, matchers)
import Views.AlertList.Filter exposing (matchers)
import Views.GroupBar.Types as GroupBar
import Utils.Views exposing (buttonLink, listButton)
import Views.AlertList.Types exposing (AlertListMsg(MsgForFilterBar, MsgForGroupBar, SetTab, ToggleSilenced), Model, Tab(..))
@ -94,7 +94,6 @@ alertGroups filter groupBar alerts =
let
grouped =
alerts
|> silenced filter.showSilenced
|> Utils.List.groupBy
(.labels >> List.filter (\( key, _ ) -> List.member key groupBar.fields))
in

View File

@ -34,7 +34,7 @@ update msg model =
| silence = Success { silence | silencedAlerts = Loading }
}
, Alerts.Api.fetchAlerts
({ nullFilter | text = Just (Utils.List.mjoin silence.matchers) })
({ nullFilter | text = Just (Utils.List.mjoin silence.matchers), showSilenced = Just True })
|> Cmd.map (AlertGroupsPreview >> MsgForSilence)
)

View File

@ -38,17 +38,21 @@ parseMatcher =
generateQueryString : Test
generateQueryString =
describe "generateQueryString"
[ test "should not render keys with Nothing value" <|
[ test "should default silenced parameter to false if showSilenced is Nothing" <|
\() ->
Expect.equal ""
Expect.equal "?silenced=false"
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Nothing, showSilenced = Nothing })
, test "should not render keys with Nothing value except the silenced parameter" <|
\() ->
Expect.equal "?silenced=false"
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Nothing, showSilenced = Nothing })
, test "should not render filter key with empty value" <|
\() ->
Expect.equal ""
Expect.equal "?silenced=false"
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Just "", showSilenced = Nothing })
, test "should render filter key with values" <|
\() ->
Expect.equal "?filter=%7Bfoo%3D%22bar%22%2C%20baz%3D~%22quux.*%22%7D"
Expect.equal "?silenced=false&filter=%7Bfoo%3D%22bar%22%2C%20baz%3D~%22quux.*%22%7D"
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Just "{foo=\"bar\", baz=~\"quux.*\"}", showSilenced = Nothing })
, test "should render silenced key with bool" <|
\() ->

File diff suppressed because one or more lines are too long