Parse alerts from api

This commit is contained in:
stuart nelson 2016-12-03 16:32:25 +01:00
parent e1e64f4415
commit 6faa1e75c3
5 changed files with 89 additions and 24 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
]

View File

@ -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

View File

@ -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 []