dashboard/src/Update.elm

173 lines
5.3 KiB
Elm
Raw Normal View History

2020-11-23 11:07:07 +03:00
module Update exposing (update)
{-| Copyright 2020 Fluence Labs Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}
2020-11-26 21:47:37 +03:00
import AirScripts.GetAll
import Blueprints.Model exposing (Blueprint)
2020-11-23 15:44:45 +03:00
import Browser
import Browser.Navigation as Nav
import Dict exposing (Dict)
import Maybe exposing (withDefault)
2020-11-27 16:03:18 +03:00
import Model exposing (Model, PeerData, emptyPeerData)
2020-11-23 11:07:07 +03:00
import Msg exposing (..)
2020-11-26 21:47:37 +03:00
import Nodes.Model exposing (Identify)
2020-11-23 14:27:33 +03:00
import Port exposing (sendAir)
2020-11-23 15:44:45 +03:00
import Route
2020-11-26 21:47:37 +03:00
import Services.Model exposing (Service)
2020-11-23 15:44:45 +03:00
import Url
2020-11-23 11:07:07 +03:00
2020-11-25 19:51:53 +03:00
2020-11-23 11:07:07 +03:00
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
2020-11-23 15:44:45 +03:00
UrlChanged url ->
let
route =
2020-11-23 16:31:31 +03:00
Route.parse url
2020-11-23 14:27:33 +03:00
2020-11-23 15:44:45 +03:00
cmd =
Route.routeCommand model route
in
2020-11-30 21:35:08 +03:00
( { model | url = url, page = route }, cmd )
2020-11-23 15:44:45 +03:00
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.key (Url.toString url) )
Browser.External href ->
( model, Nav.load href )
2020-11-23 14:27:33 +03:00
2020-11-26 21:47:37 +03:00
AquamarineEvent { name, peer, peers, identify, services, modules, blueprints } ->
2020-11-23 17:39:32 +03:00
case name of
"peers_discovered" ->
let
2020-11-25 19:51:53 +03:00
peersMap =
List.map (\p -> Tuple.pair p emptyPeerData) (withDefault [] peers)
newDict =
Dict.fromList peersMap
updatedDict =
Dict.union model.discoveredPeers newDict
2020-11-23 17:39:32 +03:00
in
( { model | discoveredPeers = updatedDict }, Cmd.none )
2020-11-23 17:39:32 +03:00
2020-11-26 21:47:37 +03:00
"all_info" ->
let
2020-11-30 14:31:03 +03:00
updated =
Maybe.map4 (updateModel model peer) identify services modules blueprints
updatedModel =
withDefault model updated
2020-11-30 14:31:03 +03:00
byBp =
peersByBlueprintId model.discoveredPeers "623c6d14-2204-43c4-84d5-a237bcd19874"
_ =
Debug.log "by blueprint id" byBp
in
( updatedModel, Cmd.none )
2020-11-25 19:51:53 +03:00
"modules_discovered" ->
let
2020-11-25 19:51:53 +03:00
newModules =
Maybe.withDefault [] modules
empty =
emptyPeerData
up =
\old -> Just (Maybe.withDefault { empty | modules = newModules } (Maybe.map (\o -> { o | modules = newModules }) old))
updatedDict =
Dict.update peer up model.discoveredPeers
in
2020-11-25 19:51:53 +03:00
( { model | discoveredPeers = updatedDict }, Cmd.none )
2020-11-23 17:39:32 +03:00
_ ->
let
2020-11-25 19:51:53 +03:00
_ =
Debug.log "event in ELM" name
2020-11-23 17:39:32 +03:00
in
( model, Cmd.none )
2020-11-23 11:07:07 +03:00
2020-11-26 16:53:31 +03:00
Click command ->
case command of
2020-11-26 21:47:37 +03:00
"get_all" ->
2020-11-26 16:53:31 +03:00
( model
2020-11-26 21:47:37 +03:00
, sendAir (AirScripts.GetAll.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
2020-11-26 16:53:31 +03:00
)
2020-11-30 14:31:03 +03:00
_ ->
( model, Cmd.none )
2020-11-23 11:07:07 +03:00
2020-11-23 14:27:33 +03:00
RelayChanged relayId ->
( { model | relayId = relayId }, Cmd.none )
2020-11-26 21:47:37 +03:00
2020-11-30 14:31:03 +03:00
2020-11-26 21:47:37 +03:00
updateModel : Model -> String -> Identify -> List Service -> List String -> List Blueprint -> Model
updateModel model peer identify services modules blueprints =
let
2020-11-30 14:31:03 +03:00
data =
Maybe.withDefault emptyPeerData (Dict.get peer model.discoveredPeers)
newData =
{ data | identify = identify, services = services, modules = modules, blueprints = blueprints }
updated =
Dict.insert peer newData model.discoveredPeers
2020-11-26 21:47:37 +03:00
in
2020-11-30 14:31:03 +03:00
{ model | discoveredPeers = updated }
2020-11-26 21:47:37 +03:00
peersByModule : Dict String PeerData -> String -> List String
peersByModule peerData moduleId =
2020-11-26 22:26:39 +03:00
let
2020-11-30 14:31:03 +03:00
list =
Dict.toList peerData
found =
list |> List.filter (\( _, pd ) -> existsByModule moduleId pd.modules) |> List.map (\( peer, _ ) -> peer)
2020-11-26 22:26:39 +03:00
in
2020-11-30 14:31:03 +03:00
found
existsByModule : String -> List String -> Bool
existsByModule moduleId modules =
modules |> List.any (\m -> m == moduleId)
2020-11-27 16:03:18 +03:00
2020-11-30 14:31:03 +03:00
peersByBlueprintId : Dict String PeerData -> String -> List String
peersByBlueprintId peerData blueprintId =
2020-11-27 16:03:18 +03:00
let
2020-11-30 14:31:03 +03:00
list =
Dict.toList peerData
found =
list |> List.filter (\( _, pd ) -> existsByBlueprintId blueprintId pd.blueprints) |> List.map (\( peer, _ ) -> peer)
2020-11-27 16:03:18 +03:00
in
2020-11-30 14:31:03 +03:00
found
2020-11-27 16:03:18 +03:00
existsByBlueprintId : String -> List Blueprint -> Bool
existsByBlueprintId id bps =
bps |> List.any (\b -> b.id == id)