Parse alerts in alert groups, add silence edit/new

This commit is contained in:
stuart nelson 2016-12-04 19:47:31 +01:00
parent 6faa1e75c3
commit 58f552c79d
5 changed files with 88 additions and 23 deletions

View File

@ -30,11 +30,11 @@ getSilences =
Http.send SilencesFetch (Http.get url listResponseDecoder)
getSilence : String -> Cmd Msg
getSilence : Int -> Cmd Msg
getSilence id =
let
url =
String.join "/" [ baseUrl, "silence", id ]
String.join "/" [ baseUrl, "silence", toString id ]
in
Http.send SilenceFetch (Http.get url showResponseDecoder)
@ -66,17 +66,18 @@ alertGroupDecoder =
blockDecoder : Json.Decoder (List Alert)
blockDecoder =
Json.at [ "alerts" ] (Json.list alertDecoder)
Json.at [ "alerts" ] (Json.list alertDecoder)
alertDecoder : Json.Decoder Alert
alertDecoder =
Json.map6 Alert
Json.map6 Alert
(field "annotations" (Json.keyValuePairs Json.string))
(field "labels" (Json.keyValuePairs Json.string))
(field "inhibited" Json.bool )
(Json.maybe (field "silenced" Json.int ))
(field "startsAt" Json.string )
(field "generatorURL" Json.string )
(field "labels" (Json.keyValuePairs Json.string))
(field "inhibited" Json.bool)
(Json.maybe (field "silenced" Json.int))
(field "startsAt" Json.string)
(field "generatorURL" Json.string)
showResponseDecoder : Json.Decoder Silence

View File

@ -29,7 +29,12 @@ init location =
route =
Parsing.urlParser location
in
update (urlUpdate location) (Model [] (Silence 0 "" "" "" "" "" []) [] route)
update (urlUpdate location) (Model [] nullSilence [] route)
nullSilence : Silence
nullSilence =
Silence 0 "" "" "" "" "" []
update : Msg -> Model -> ( Model, Cmd Msg )
@ -54,6 +59,12 @@ update msg model =
FetchSilence id ->
( { model | route = SilenceRoute id }, Api.getSilence id )
EditSilence id ->
( { model | route = EditSilenceRoute id }, Api.getSilence id )
NewSilence ->
( { model | silence = nullSilence, route = NewSilenceRoute }, Cmd.none )
FetchAlertGroups ->
( { model | route = AlertGroupsRoute }, Api.getAlertGroups )
@ -81,9 +92,15 @@ urlUpdate location =
SilencesRoute ->
FetchSilences
NewSilenceRoute ->
NewSilence
SilenceRoute id ->
FetchSilence id
EditSilenceRoute id ->
EditSilence id
AlertGroupsRoute ->
FetchAlertGroups

View File

@ -34,9 +34,19 @@ silencesParser =
UrlParser.s "silences"
silenceParser : Parser (String -> a) a
newSilenceParser : Parser a a
newSilenceParser =
UrlParser.s "silences" </> UrlParser.s "new"
silenceParser : Parser (Int -> a) a
silenceParser =
UrlParser.s "silence" </> UrlParser.string
UrlParser.s "silences" </> UrlParser.int
editSilenceParser : Parser (Int -> a) a
editSilenceParser =
UrlParser.s "silences" </> UrlParser.int </> UrlParser.s "edit"
alertsParser : Parser a a
@ -53,6 +63,8 @@ routeParser : Parser (Route -> a) a
routeParser =
UrlParser.oneOf
[ map SilencesRoute silencesParser
, map NewSilenceRoute newSilenceParser
, map EditSilenceRoute editSilenceParser
, map SilenceRoute silenceParser
, map AlertGroupsRoute alertsParser
, map TopLevel topLevelParser

View File

@ -34,21 +34,27 @@ type alias Silence =
type alias AlertGroup =
{ alerts : Maybe (List (List Alert))
, labels : List ( String, String ) }
, labels : List ( String, String )
}
type alias Alert =
{ annotations : List (String, String)
, labels : List (String, String)
{ annotations : List ( String, String )
, labels : List ( String, String )
, inhibited : Bool
, silenced : Maybe Int -- TODO: See how to rename this on parsing from API to silenceId
, silenced :
Maybe Int
-- TODO: See how to rename this on parsing from API to silenceId
, startsAt : String
, generatorUrl : String }
, generatorUrl : String
}
type alias Block =
{ alerts : List Alert }
-- TODO: Implement Matcher.
@ -63,7 +69,9 @@ type Msg
= SilenceFetch (Result Http.Error Silence)
| SilencesFetch (Result Http.Error (List Silence))
| FetchSilences
| FetchSilence String
| FetchSilence Int
| NewSilence
| EditSilence Int
| AlertGroupsFetch (Result Http.Error (List AlertGroup))
| FetchAlertGroups
| RedirectSilences
@ -71,7 +79,9 @@ type Msg
type Route
= SilencesRoute
| SilenceRoute String
| NewSilenceRoute
| SilenceRoute Int
| EditSilenceRoute Int
| AlertGroupsRoute
| TopLevel
| NotFound

View File

@ -20,6 +20,12 @@ view model =
AlertGroupsRoute ->
genericListView alertGroupsView model.alertGroups
NewSilenceRoute ->
silenceFormView "New" model.silence
EditSilenceRoute id ->
silenceFormView "Edit" model.silence
SilencesRoute ->
genericListView silenceListView model.silences
@ -45,11 +51,30 @@ todoView model =
]
silenceFormView : String -> Silence -> Html Msg
silenceFormView kind silence =
div [ class "pa4 black-80" ]
[ fieldset [ class "ba b--transparent ph0 mh0" ]
[ legend [ class "ph0 mh0 fw6" ] [ text <| kind ++ " Silence" ]
]
]
alertGroupsView : AlertGroup -> Html Msg
alertGroupsView alertGroup =
li
[ class "pa3 pa4-ns bb b--black-10" ]
(List.map labelView alertGroup.labels)
let
labels =
case alertGroup.alerts of
Just alerts ->
-- (List.concatMap (\x -> List.concatMap (\y -> y.labels)) alerts)
[]
Nothing ->
[]
in
li
[ class "pa3 pa4-ns bb b--black-10" ]
(List.map labelView alertGroup.labels)
labelView : ( String, String ) -> Html msg
@ -93,7 +118,7 @@ silenceListView silence =
[ class "pa3 pa4-ns bb b--black-10" ]
[ a
[ class "db link dim blue"
, href ("#/silence/" ++ (toString silence.id))
, href ("#/silences/" ++ (toString silence.id))
]
[ b [ class "db f4 mb1" ]
[ text alertName ]