From 6faa1e75c376ba2e2c6277a62b1118da7b1d5c23 Mon Sep 17 00:00:00 2001 From: stuart nelson Date: Sat, 3 Dec 2016 16:32:25 +0100 Subject: [PATCH] Parse alerts from api --- src/Api.elm | 36 ++++++++++++++++++++++++++++++++++++ src/Main.elm | 20 ++++++++++++++++++-- src/Parsing.elm | 8 +------- src/Types.elm | 22 +++++++++++++++++----- src/Views.elm | 27 +++++++++++++++++---------- 5 files changed, 89 insertions(+), 24 deletions(-) diff --git a/src/Api.elm b/src/Api.elm index ce429000..b822e332 100644 --- a/src/Api.elm +++ b/src/Api.elm @@ -39,10 +39,46 @@ getSilence id = Http.send SilenceFetch (Http.get url showResponseDecoder) +getAlertGroups : Cmd Msg +getAlertGroups = + let + url = + String.join "/" [ baseUrl, "alerts", "groups" ] + in + Http.send AlertGroupsFetch (Http.get url alertGroupsDecoder) + + -- Make these generic when I've gotten to Alerts +alertGroupsDecoder : Json.Decoder (List AlertGroup) +alertGroupsDecoder = + Json.at [ "data" ] (Json.list alertGroupDecoder) + + +alertGroupDecoder : Json.Decoder AlertGroup +alertGroupDecoder = + Json.map2 AlertGroup + (Json.at [ "blocks" ] <| Json.maybe <| Json.list blockDecoder) + (Json.at [ "labels" ] (Json.keyValuePairs Json.string)) + + +blockDecoder : Json.Decoder (List Alert) +blockDecoder = + Json.at [ "alerts" ] (Json.list alertDecoder) + +alertDecoder : Json.Decoder Alert +alertDecoder = + 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 ) + + showResponseDecoder : Json.Decoder Silence showResponseDecoder = (Json.at [ "data" ] silenceDecoder) diff --git a/src/Main.elm b/src/Main.elm index 3eac5763..b078b81f 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -29,7 +29,7 @@ init location = route = Parsing.urlParser location in - update (urlUpdate location) (Model [] (Silence 0 "" "" "" "" "" []) [] (Alert "") route) + update (urlUpdate location) (Model [] (Silence 0 "" "" "" "" "" []) [] route) update : Msg -> Model -> ( Model, Cmd Msg ) @@ -54,8 +54,21 @@ update msg model = FetchSilence id -> ( { model | route = SilenceRoute id }, Api.getSilence id ) + FetchAlertGroups -> + ( { model | route = AlertGroupsRoute }, Api.getAlertGroups ) + + AlertGroupsFetch (Ok alertGroups) -> + ( { model | alertGroups = alertGroups }, Cmd.none ) + + AlertGroupsFetch (Err err) -> + let + one = + Debug.log "error" err + in + ( { model | route = NotFound }, Cmd.none ) + RedirectSilences -> - ( { model | route = AlertsRoute }, Navigation.newUrl "/#/silences" ) + ( { model | route = AlertGroupsRoute }, Navigation.newUrl "/#/silences" ) urlUpdate : Navigation.Location -> Msg @@ -71,6 +84,9 @@ urlUpdate location = SilenceRoute id -> FetchSilence id + AlertGroupsRoute -> + FetchAlertGroups + _ -> -- TODO: 404 page RedirectSilences diff --git a/src/Parsing.elm b/src/Parsing.elm index e93aa2e2..6cec7acc 100644 --- a/src/Parsing.elm +++ b/src/Parsing.elm @@ -44,11 +44,6 @@ alertsParser = UrlParser.s "alerts" -alertParser : Parser (String -> a) a -alertParser = - UrlParser.s "alert" UrlParser.string - - topLevelParser : Parser a a topLevelParser = UrlParser.s "" @@ -59,7 +54,6 @@ routeParser = UrlParser.oneOf [ map SilencesRoute silencesParser , map SilenceRoute silenceParser - , map AlertsRoute alertsParser - , map AlertRoute alertParser + , map AlertGroupsRoute alertsParser , map TopLevel topLevelParser ] diff --git a/src/Types.elm b/src/Types.elm index 64a14ad3..38be9b56 100644 --- a/src/Types.elm +++ b/src/Types.elm @@ -12,8 +12,7 @@ import Http exposing (Error) type alias Model = { silences : List Silence , silence : Silence - , alerts : List Alert - , alert : Alert + , alertGroups : List AlertGroup , route : Route } @@ -33,10 +32,22 @@ type alias Silence = -- TODO: Implement Alert. +type alias AlertGroup = + { alerts : Maybe (List (List Alert)) + , labels : List ( String, String ) } + + type alias Alert = - { id : 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 + , startsAt : String + , generatorUrl : String } +type alias Block = + { alerts : List Alert } -- TODO: Implement Matcher. @@ -53,13 +64,14 @@ type Msg | SilencesFetch (Result Http.Error (List Silence)) | FetchSilences | FetchSilence String + | AlertGroupsFetch (Result Http.Error (List AlertGroup)) + | FetchAlertGroups | RedirectSilences type Route = SilencesRoute | SilenceRoute String - | AlertsRoute - | AlertRoute String + | AlertGroupsRoute | TopLevel | NotFound diff --git a/src/Views.elm b/src/Views.elm index 2df5cbf4..8442088b 100644 --- a/src/Views.elm +++ b/src/Views.elm @@ -6,25 +6,19 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import String +import Tuple -- Internal Imports -import Types exposing (Model, Silence, Alert, Matcher, Msg, Route(..)) +import Types exposing (Model, Silence, AlertGroup, Matcher, Msg, Route(..)) view : Model -> Html Msg view model = case model.route of - AlertsRoute -> - genericListView todoView model.alerts - - AlertRoute name -> - let - one = - Debug.log "view: name" name - in - todoView model.alert + AlertGroupsRoute -> + genericListView alertGroupsView model.alertGroups SilencesRoute -> genericListView silenceListView model.silences @@ -51,6 +45,19 @@ todoView model = ] +alertGroupsView : AlertGroup -> Html Msg +alertGroupsView alertGroup = + li + [ class "pa3 pa4-ns bb b--black-10" ] + (List.map labelView alertGroup.labels) + + +labelView : ( String, String ) -> Html msg +labelView ( key, value ) = + span [ class "f5 db lh-copy measure" ] + [ text <| key ++ "=" ++ value ] + + notFoundView : a -> Html Msg notFoundView model = div []