dashboard/src/Modules/View.elm

64 lines
2.0 KiB
Elm
Raw Normal View History

module Modules.View exposing (..)
2020-11-28 17:59:09 +03:00
import Dict exposing (Dict)
2020-11-30 14:31:03 +03:00
import Html exposing (Html, div, p, span, text)
2020-11-30 19:56:11 +03:00
import Html.Attributes exposing (attribute)
2020-11-28 17:59:09 +03:00
import Model exposing (Model, PeerData)
2020-12-01 14:03:25 +03:00
import Modules.Model exposing (Module, ModuleShortInfo)
import Palette exposing (classes)
import Utils.Utils exposing (instancesText)
2020-11-25 19:51:53 +03:00
2020-11-30 14:31:03 +03:00
2020-11-28 17:59:09 +03:00
getModuleShortInfo : Model -> List ModuleShortInfo
getModuleShortInfo model =
2020-12-01 14:03:25 +03:00
getAllModules model.discoveredPeers |> Dict.toList |> List.map (\( moduleName, ( moduleInfo, peers ) ) -> { moduleInfo = moduleInfo, instanceNumber = List.length peers })
2020-11-30 14:31:03 +03:00
2020-11-25 19:51:53 +03:00
2020-12-01 14:03:25 +03:00
getAllModules : Dict String PeerData -> Dict String ( Module, List String )
2020-11-28 17:59:09 +03:00
getAllModules peerData =
let
2020-11-30 14:31:03 +03:00
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
2020-11-28 17:59:09 +03:00
in
2020-11-30 14:31:03 +03:00
peersByModuleName
2020-11-28 17:59:09 +03:00
2020-12-01 14:03:25 +03:00
-- group by module name and append peers
updateDict : ( String, Module ) -> Dict String ( Module, List String ) -> Dict String ( Module, List String )
updateDict ( peer, moduleInfo ) dict =
dict
|> Dict.update moduleInfo.name
(\oldM ->
oldM
|> Maybe.map (\( info, peers ) -> ( info, List.append [ peer ] peers ))
|> Maybe.withDefault ( moduleInfo, [ peer ] )
|> Just
)
2020-11-28 17:59:09 +03:00
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-30 14:31:03 +03:00
div [ classes "cf ph2-ns" ] modulesView
2020-11-25 19:51:53 +03:00
2020-11-25 22:11:11 +03:00
viewService : ModuleShortInfo -> Html msg
2020-12-01 14:03:25 +03:00
viewService moduleInfo =
2020-11-30 14:31:03 +03:00
div [ classes "fl w-third-ns pa2" ]
2020-11-30 19:56:11 +03:00
[ div [ attribute "href" "#", classes "fl w-100 link dim black mw5 dt hide-child ba b-black pa4 br2 solid" ]
2020-12-01 14:03:25 +03:00
[ p [ classes "tl di" ] [ span [ classes "b pl2" ] [ text moduleInfo.moduleInfo.name ], span [ classes "di fr pr2" ] [ instancesText moduleInfo.instanceNumber ] ]
2020-11-25 19:51:53 +03:00
]
]