forked from mohsenasm/swarm-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
84 lines (65 loc) · 1.88 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Main exposing (..)
import Navigation
import Html exposing (..)
import Dict exposing (Dict)
import Util exposing (..)
import WebSocket
import Docker.Types exposing (..)
import Docker exposing (fromJson)
import Components as UI
localWebsocket : Navigation.Location -> String
localWebsocket location =
if location.protocol == "https:" then
"wss://" ++ location.host ++ "/stream"
else
"ws://" ++ location.host ++ "/stream"
type alias Model =
{ webSocketUrl : String
, swarm : Docker
, tasks : TaskIndex
, errors : List String
}
type Msg
= UrlChange Navigation.Location
| Receive String
init : Navigation.Location -> ( Model, Cmd Msg )
init location =
( { webSocketUrl = localWebsocket location
, swarm = Docker.empty
, tasks = Dict.empty
, errors = []
}
, Cmd.none
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Receive serverJson ->
case fromJson serverJson of
Ok serverData ->
( { model | swarm = serverData, tasks = groupBy taskIndexKey serverData.assignedTasks }, Cmd.none )
Err error ->
( { model | errors = error :: model.errors }, Cmd.none )
UrlChange location ->
( model, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
WebSocket.listen model.webSocketUrl Receive
view : Model -> Html Msg
view { swarm, tasks, errors } =
let
{ services, nodes, networks } =
swarm
in
div []
[ UI.swarmGrid services nodes networks tasks
, ul [] (List.map (\e -> li [] [ text e ]) errors)
]
main : Program Never Model Msg
main =
Navigation.program UrlChange
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}