Storing modules

This commit is contained in:
Pavel Murygin 2021-07-02 19:49:14 +03:00
parent df2a4e119f
commit 8c0cf877b2
6 changed files with 173 additions and 14 deletions

View File

@ -12,11 +12,9 @@ type alias ModuleConfigDto =
{ name : String { name : String
} }
type alias ModuleDto = type alias ModuleDto =
{ name : String { name : String
, hash : String , hash : String
, config : ModuleConfigDto
} }
@ -35,8 +33,7 @@ type alias PeerDto =
{ peerId : String { peerId : String
, identify : Maybe IdentifyDto , identify : Maybe IdentifyDto
, services : Maybe (List ServiceDto) , services : Maybe (List ServiceDto)
, modules : Maybe (List ModuleDto)
--, modules : Maybe (List ModuleDto)
, blueprints : Maybe (List BlueprintDto) , blueprints : Maybe (List BlueprintDto)
} }

View File

@ -1,11 +1,13 @@
module Cache exposing (..) module Cache exposing (..)
import AquaPorts.CollectPeerInfo exposing (BlueprintDto, PeerDto, ServiceDto) import AquaPorts.CollectPeerInfo exposing (BlueprintDto, ModuleDto, PeerDto, ServiceDto)
import AquaPorts.CollectServiceInterface exposing (ServiceInterfaceDto) import AquaPorts.CollectServiceInterface exposing (ServiceInterfaceDto)
import Array exposing (Array) import Array exposing (Array)
import Blueprints.Model exposing (Blueprint) import Blueprints.Model exposing (Blueprint)
import Dict exposing (Dict) import Dict exposing (Dict, values)
import Dict.Extra as Dict import Dict.Extra as Dict
import Html exposing (b)
import Set exposing (Set)
@ -33,17 +35,32 @@ extractHash str =
|> Maybe.withDefault "" |> 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 = type alias Blueprint =
{ id : BlueprintId { id : BlueprintId
, name : String , name : String
, dependencies : Array Hash , dependencies : Set Hash
} }
blueprintFromDto : BlueprintDto -> Blueprint blueprintFromDto : BlueprintDto -> Blueprint
blueprintFromDto bp = blueprintFromDto bp =
{ id = bp.id { id = bp.id
, dependencies = bp.dependencies |> List.map extractHash |> Array.fromList , dependencies = bp.dependencies |> List.map extractHash |> Set.fromList
, name = bp.name , name = bp.name
} }
@ -87,6 +104,8 @@ firstExternalAddress node =
type alias Model = type alias Model =
{ blueprintsById : Dict BlueprintId Blueprint { blueprintsById : Dict BlueprintId Blueprint
, servicesById : Dict ServiceId Service , servicesById : Dict ServiceId Service
, modulesByHash : Dict Hash Module
, blueprintsByModuleHash : Dict Hash (Array BlueprintId)
, servicesByBlueprintId : Dict BlueprintId (Array ServiceId) , servicesByBlueprintId : Dict BlueprintId (Array ServiceId)
, nodeByServiceId : Dict ServiceId PeerId , nodeByServiceId : Dict ServiceId PeerId
, nodeByBlueprintId : Dict BlueprintId PeerId , nodeByBlueprintId : Dict BlueprintId PeerId
@ -98,6 +117,8 @@ init : Model
init = init =
{ blueprintsById = Dict.empty { blueprintsById = Dict.empty
, servicesById = Dict.empty , servicesById = Dict.empty
, modulesByHash = Dict.empty
, blueprintsByModuleHash = Dict.empty
, servicesByBlueprintId = Dict.empty , servicesByBlueprintId = Dict.empty
, nodeByServiceId = Dict.empty , nodeByServiceId = Dict.empty
, nodeByBlueprintId = Dict.empty , nodeByBlueprintId = Dict.empty
@ -121,7 +142,7 @@ type Msg
update : Model -> Msg -> Model update : Model -> Msg -> Model
update model msg = update model msg =
case msg of case msg of
CollectPeerInfo { peerId, blueprints, services, identify } -> CollectPeerInfo { peerId, blueprints, services, identify, modules } ->
let let
newBlueprints = newBlueprints =
blueprints |> Maybe.withDefault [] |> List.map blueprintFromDto |> Dict.fromListBy (\x -> x.id) blueprints |> Maybe.withDefault [] |> List.map blueprintFromDto |> Dict.fromListBy (\x -> x.id)
@ -129,6 +150,9 @@ update model msg =
newServices = newServices =
services |> Maybe.withDefault [] |> List.map serviceFromDto |> Dict.fromListBy (\x -> x.id) services |> Maybe.withDefault [] |> List.map serviceFromDto |> Dict.fromListBy (\x -> x.id)
newModules =
modules |> Maybe.withDefault [] |> List.map moduleFromDto |> Dict.fromListBy (\x -> x.hash)
resultBlueprints = resultBlueprints =
Dict.union newBlueprints model.blueprintsById Dict.union newBlueprints model.blueprintsById
@ -153,11 +177,28 @@ update model msg =
, services = Dict.keys newServices |> Array.fromList , services = Dict.keys newServices |> Array.fromList
, blueprints = Dict.keys newBlueprints |> 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 in
{ model { model
| blueprintsById = resultBlueprints | blueprintsById = resultBlueprints
, servicesById = resultServices , servicesById = resultServices
, servicesByBlueprintId = resultServicesByBlueprintId , servicesByBlueprintId = resultServicesByBlueprintId
, modulesByHash = Dict.union model.modulesByHash newModules
, blueprintsByModuleHash = bpMyModuleHash
, nodes = Dict.insert newNode.peerId newNode model.nodes , nodes = Dict.insert newNode.peerId newNode model.nodes
, nodeByServiceId = Dict.union model.nodeByServiceId (Dict.map (\x -> \_ -> peerId) newServices) , nodeByServiceId = Dict.union model.nodeByServiceId (Dict.map (\x -> \_ -> peerId) newServices)
, nodeByBlueprintId = Dict.union model.nodeByBlueprintId (Dict.map (\x -> \_ -> peerId) newBlueprints) , nodeByBlueprintId = Dict.union model.nodeByBlueprintId (Dict.map (\x -> \_ -> peerId) newBlueprints)

View File

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

View File

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

View File

@ -6,6 +6,7 @@ import Dict
import Html exposing (Html, a, div, span, text) import Html exposing (Html, a, div, span, text)
import Html.Attributes exposing (attribute) import Html.Attributes exposing (attribute)
import Maybe.Extra as Maybe import Maybe.Extra as Maybe
import Modules.ModulesList
import Palette exposing (classes, redFont) import Palette exposing (classes, redFont)
import Services.ServiceRow import Services.ServiceRow
import Services.ServicesTable import Services.ServicesTable
@ -21,7 +22,7 @@ type alias FmModel =
type alias Model = type alias Model =
{ featuredBlueprints : Blueprints.BlueprintsList.Model { featuredBlueprints : Blueprints.BlueprintsList.Model
, featuredModules : FmModel , featuredModules : Modules.ModulesList.Model
, services : Services.ServicesTable.Model , services : Services.ServicesTable.Model
} }
@ -29,7 +30,7 @@ type alias Model =
init : Model init : Model
init = init =
{ featuredBlueprints = [] { featuredBlueprints = []
, featuredModules = {} , featuredModules = []
, services = [] , services = []
} }
@ -37,7 +38,7 @@ init =
fromCache : Cache.Model -> Model fromCache : Cache.Model -> Model
fromCache cache = fromCache cache =
{ featuredBlueprints = Blueprints.BlueprintsList.fromCache cache { featuredBlueprints = Blueprints.BlueprintsList.fromCache cache
, featuredModules = {} , featuredModules = Modules.ModulesList.fromCache cache
, services = , services =
cache.servicesById cache.servicesById
|> Dict.keys |> Dict.keys
@ -57,8 +58,7 @@ view model =
, div [ classes "pt4 f3 fw5 pb4" ] [ text "Featured Service Blueprints" ] , div [ classes "pt4 f3 fw5 pb4" ] [ text "Featured Service Blueprints" ]
, Blueprints.BlueprintsList.view model.featuredBlueprints , Blueprints.BlueprintsList.view model.featuredBlueprints
, div [ classes "pt4 f3 fw5 pb4" ] [ text "Featured Modules" ] , div [ classes "pt4 f3 fw5 pb4" ] [ text "Featured Modules" ]
, Modules.ModulesList.view model.featuredModules
--, Modules.View.view model
, div [ classes "pt4 f3 fw5 pb4" ] , div [ classes "pt4 f3 fw5 pb4" ]
[ text "Services" ] [ text "Services" ]
, Services.ServicesTable.view model.services , Services.ServicesTable.view model.services

29
src/Pages/ModulePage.elm Normal file
View File

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