From 8c0cf877b24f1232c13698bc7f3d47bacd0cded0 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Fri, 2 Jul 2021 19:49:14 +0300 Subject: [PATCH] Storing modules --- src/AquaPorts/CollectPeerInfo.elm | 5 +-- src/Cache.elm | 51 +++++++++++++++++++++++++++--- src/Modules/ModuleTile.elm | 40 ++++++++++++++++++++++++ src/Modules/ModulesList.elm | 52 +++++++++++++++++++++++++++++++ src/Pages/Hub.elm | 10 +++--- src/Pages/ModulePage.elm | 29 +++++++++++++++++ 6 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 src/Modules/ModuleTile.elm create mode 100644 src/Modules/ModulesList.elm create mode 100644 src/Pages/ModulePage.elm diff --git a/src/AquaPorts/CollectPeerInfo.elm b/src/AquaPorts/CollectPeerInfo.elm index 9709da0..e78af8d 100644 --- a/src/AquaPorts/CollectPeerInfo.elm +++ b/src/AquaPorts/CollectPeerInfo.elm @@ -12,11 +12,9 @@ type alias ModuleConfigDto = { name : String } - type alias ModuleDto = { name : String , hash : String - , config : ModuleConfigDto } @@ -35,8 +33,7 @@ type alias PeerDto = { peerId : String , identify : Maybe IdentifyDto , services : Maybe (List ServiceDto) - - --, modules : Maybe (List ModuleDto) + , modules : Maybe (List ModuleDto) , blueprints : Maybe (List BlueprintDto) } diff --git a/src/Cache.elm b/src/Cache.elm index 66e0691..c9c94f1 100644 --- a/src/Cache.elm +++ b/src/Cache.elm @@ -1,11 +1,13 @@ module Cache exposing (..) -import AquaPorts.CollectPeerInfo exposing (BlueprintDto, PeerDto, ServiceDto) +import AquaPorts.CollectPeerInfo exposing (BlueprintDto, ModuleDto, PeerDto, ServiceDto) import AquaPorts.CollectServiceInterface exposing (ServiceInterfaceDto) import Array exposing (Array) import Blueprints.Model exposing (Blueprint) -import Dict exposing (Dict) +import Dict exposing (Dict, values) import Dict.Extra as Dict +import Html exposing (b) +import Set exposing (Set) @@ -33,17 +35,32 @@ extractHash str = |> Maybe.withDefault "" +type alias Module = + { hash : Hash + , name : String + , interfaces : Maybe (List Never) + } + + +moduleFromDto : ModuleDto -> Module +moduleFromDto dto = + { name = dto.name + , hash = dto.hash + , interfaces = Nothing + } + + type alias Blueprint = { id : BlueprintId , name : String - , dependencies : Array Hash + , dependencies : Set Hash } blueprintFromDto : BlueprintDto -> Blueprint blueprintFromDto bp = { id = bp.id - , dependencies = bp.dependencies |> List.map extractHash |> Array.fromList + , dependencies = bp.dependencies |> List.map extractHash |> Set.fromList , name = bp.name } @@ -87,6 +104,8 @@ firstExternalAddress node = type alias Model = { blueprintsById : Dict BlueprintId Blueprint , servicesById : Dict ServiceId Service + , modulesByHash : Dict Hash Module + , blueprintsByModuleHash : Dict Hash (Array BlueprintId) , servicesByBlueprintId : Dict BlueprintId (Array ServiceId) , nodeByServiceId : Dict ServiceId PeerId , nodeByBlueprintId : Dict BlueprintId PeerId @@ -98,6 +117,8 @@ init : Model init = { blueprintsById = Dict.empty , servicesById = Dict.empty + , modulesByHash = Dict.empty + , blueprintsByModuleHash = Dict.empty , servicesByBlueprintId = Dict.empty , nodeByServiceId = Dict.empty , nodeByBlueprintId = Dict.empty @@ -121,7 +142,7 @@ type Msg update : Model -> Msg -> Model update model msg = case msg of - CollectPeerInfo { peerId, blueprints, services, identify } -> + CollectPeerInfo { peerId, blueprints, services, identify, modules } -> let newBlueprints = blueprints |> Maybe.withDefault [] |> List.map blueprintFromDto |> Dict.fromListBy (\x -> x.id) @@ -129,6 +150,9 @@ update model msg = newServices = services |> Maybe.withDefault [] |> List.map serviceFromDto |> Dict.fromListBy (\x -> x.id) + newModules = + modules |> Maybe.withDefault [] |> List.map moduleFromDto |> Dict.fromListBy (\x -> x.hash) + resultBlueprints = Dict.union newBlueprints model.blueprintsById @@ -153,11 +177,28 @@ update model msg = , services = Dict.keys newServices |> Array.fromList , blueprints = Dict.keys newBlueprints |> Array.fromList } + + bpMyModuleHash = + Dict.values resultBlueprints + |> List.foldl + (\bp -> + \acc -> + bp.dependencies + |> Set.foldl + (\hash -> + Dict.insertDedupe (\l1 -> \l2 -> l1 ++ l2) hash [ bp.id ] + ) + acc + ) + Dict.empty + |> Dict.map (\k -> \v -> Array.fromList v) in { model | blueprintsById = resultBlueprints , servicesById = resultServices , servicesByBlueprintId = resultServicesByBlueprintId + , modulesByHash = Dict.union model.modulesByHash newModules + , blueprintsByModuleHash = bpMyModuleHash , nodes = Dict.insert newNode.peerId newNode model.nodes , nodeByServiceId = Dict.union model.nodeByServiceId (Dict.map (\x -> \_ -> peerId) newServices) , nodeByBlueprintId = Dict.union model.nodeByBlueprintId (Dict.map (\x -> \_ -> peerId) newBlueprints) diff --git a/src/Modules/ModuleTile.elm b/src/Modules/ModuleTile.elm new file mode 100644 index 0000000..7c0a9a7 --- /dev/null +++ b/src/Modules/ModuleTile.elm @@ -0,0 +1,40 @@ +module Modules.ModuleTile exposing (Model, view) + +import Html exposing (Html, a, div, p, text) +import Html.Attributes exposing (attribute) +import Palette exposing (classes) + + + +-- model + + +type alias Model = + { hash : String + , name : String + , numberOfUsages : Int + } + + + +-- view + + +view : Model -> Html msg +view model = + let + usages = + [ text <| "in " ++ String.fromInt model.numberOfUsages ++ " blueprints" ] + in + div [ classes "fl w-100 w-third-ns pr3" ] + [ a + [ attribute "href" ("/module/" ++ model.name) + , classes "fl w-100 bg-white black mw6 mr2 mb3 hide-child pa2 element-box ba b--white pl3" + ] + [ p [ classes "tl di" ] + [ div [ classes "fl b w-100 mb1 fw5 overflow-hidden" ] + [ text model.name ] + , div [ classes "fl w-100 mt1 lucida gray-font2" ] usages + ] + ] + ] diff --git a/src/Modules/ModulesList.elm b/src/Modules/ModulesList.elm new file mode 100644 index 0000000..59efaaa --- /dev/null +++ b/src/Modules/ModulesList.elm @@ -0,0 +1,52 @@ +module Modules.ModulesList exposing (..) + +import Array exposing (Array) +import Cache +import Components.Spinner +import Dict +import Html exposing (Html, div) +import Modules.ModuleTile +import Palette exposing (classes) +import Set + + + +-- model + + +type alias Model = + List Modules.ModuleTile.Model + + +fromCache : Cache.Model -> Model +fromCache cache = + let + numberOfUsages id = + Dict.get id cache.blueprintsByModuleHash |> Maybe.withDefault Array.empty |> Array.length + in + cache.modulesByHash + |> Dict.values + |> List.map + (\x -> + { hash = x.hash + , name = x.name + , numberOfUsages = numberOfUsages x.hash + } + ) + + + +-- view + + +view : Model -> Html msg +view model = + let + finalView = + if List.isEmpty model then + Components.Spinner.view + + else + List.map Modules.ModuleTile.view model + in + div [ classes "cf" ] finalView diff --git a/src/Pages/Hub.elm b/src/Pages/Hub.elm index 16b8af7..153b503 100644 --- a/src/Pages/Hub.elm +++ b/src/Pages/Hub.elm @@ -6,6 +6,7 @@ import Dict import Html exposing (Html, a, div, span, text) import Html.Attributes exposing (attribute) import Maybe.Extra as Maybe +import Modules.ModulesList import Palette exposing (classes, redFont) import Services.ServiceRow import Services.ServicesTable @@ -21,7 +22,7 @@ type alias FmModel = type alias Model = { featuredBlueprints : Blueprints.BlueprintsList.Model - , featuredModules : FmModel + , featuredModules : Modules.ModulesList.Model , services : Services.ServicesTable.Model } @@ -29,7 +30,7 @@ type alias Model = init : Model init = { featuredBlueprints = [] - , featuredModules = {} + , featuredModules = [] , services = [] } @@ -37,7 +38,7 @@ init = fromCache : Cache.Model -> Model fromCache cache = { featuredBlueprints = Blueprints.BlueprintsList.fromCache cache - , featuredModules = {} + , featuredModules = Modules.ModulesList.fromCache cache , services = cache.servicesById |> Dict.keys @@ -57,8 +58,7 @@ view model = , div [ classes "pt4 f3 fw5 pb4" ] [ text "Featured Service Blueprints" ] , Blueprints.BlueprintsList.view model.featuredBlueprints , div [ classes "pt4 f3 fw5 pb4" ] [ text "Featured Modules" ] - - --, Modules.View.view model + , Modules.ModulesList.view model.featuredModules , div [ classes "pt4 f3 fw5 pb4" ] [ text "Services" ] , Services.ServicesTable.view model.services diff --git a/src/Pages/ModulePage.elm b/src/Pages/ModulePage.elm new file mode 100644 index 0000000..ecdc8eb --- /dev/null +++ b/src/Pages/ModulePage.elm @@ -0,0 +1,29 @@ +module Pages.ModulePage exposing (Model, view) + +import Dict exposing (Dict) +import Html exposing (Html, a, article, div, span, text) +import Html.Attributes exposing (attribute, property) +import Palette exposing (classes, redFont) + + + +-- model + + +type alias Model = + { name : String + , id : String + , author : String + , authorPeerId : String + , description : String + , website : String + } + + + +-- view + + +view : Model -> Html msg +view model = + div [] []