dashboard/src/Modules/View.elm

44 lines
1.6 KiB
Elm
Raw Normal View History

module Modules.View exposing (..)
2020-11-28 17:59:09 +03:00
import Dict exposing (Dict)
import Html exposing (Html)
2020-11-28 17:59:09 +03:00
import Model exposing (Model, PeerData)
import Modules.Model exposing (ModuleShortInfo)
import Palette exposing (classes)
import Utils.Utils exposing (instancesText)
2020-11-25 19:51:53 +03:00
2020-11-28 17:59:09 +03:00
getModuleShortInfo : Model -> List ModuleShortInfo
getModuleShortInfo model =
getAllModules model.discoveredPeers |> Dict.toList |> List.map (\(moduleName, peers) -> {name = moduleName, instanceNumber = List.length peers})
2020-11-25 19:51:53 +03:00
2020-11-28 17:59:09 +03:00
getAllModules : Dict String PeerData -> Dict String (List String)
getAllModules peerData =
let
peerDatas = Dict.toList peerData
allModules = peerDatas |> List.map (\(peer, pd) -> pd.modules |> List.map (\ms -> (peer, ms))) |> List.concat
peersByModuleName = allModules |> List.foldr updateDict Dict.empty
in
peersByModuleName
updateDict : (String, String) -> Dict String (List String) -> Dict String (List String)
updateDict (peer, moduleName) dict =
dict |> Dict.update moduleName (\oldM -> oldM |> Maybe.map (List.append [peer]) |> Maybe.withDefault [peer] |> Just)
view : Model -> Html msg
view modules =
let
2020-11-25 19:51:53 +03:00
modulesView =
2020-11-28 17:59:09 +03:00
List.map viewService (getModuleShortInfo modules)
in
2020-11-25 19:51:53 +03:00
Html.div [ classes "cf ph2-ns" ] modulesView
2020-11-25 22:11:11 +03:00
viewService : ModuleShortInfo -> Html msg
viewService service =
2020-11-25 19:51:53 +03:00
Html.div [ classes "fl w-third-ns pa2" ]
[ Html.div [ classes "fl w-100 br2 ba solid pa2 mh2" ]
[ Html.p [ classes "tl di" ] [ Html.span [ classes "b pl2" ] [ Html.text service.name ], Html.span [ classes "di fr pr2" ] [ instancesText service.instanceNumber ] ]
]
]