dashboard/src/Update.elm

138 lines
4.5 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-12-01 14:03:25 +03:00
import Modules.Model exposing (Module)
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-12-07 14:31:02 +03:00
import Route exposing (getAllCmd)
2020-12-01 17:47:52 +03:00
import Service.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-12-01 17:47:52 +03:00
( { model | url = url, page = route, toggledInterface = Nothing }, 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-12-01 14:03:25 +03:00
emptyPeers =
Dict.toList updatedDict
2020-12-01 18:16:56 +03:00
|> List.filter (\( _, data ) -> List.isEmpty data.identify.external_addresses)
2020-12-01 14:03:25 +03:00
|> List.map Tuple.first
2020-11-23 17:39:32 +03:00
in
2020-12-07 14:31:02 +03:00
( { model | discoveredPeers = updatedDict }, getAllCmd model.peerId model.relayId [] )
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
in
( updatedModel, Cmd.none )
2020-11-25 19:51:53 +03:00
2020-11-23 17:39:32 +03:00
_ ->
( model, Cmd.none )
2020-11-23 11:07:07 +03:00
2020-12-01 17:47:52 +03:00
ToggleInterface id ->
2020-12-01 18:16:56 +03:00
case model.toggledInterface of
Just ti ->
2020-12-02 12:10:59 +03:00
( { model
| toggledInterface =
if id == ti then
Nothing
else
Just id
}
, Cmd.none
)
2020-12-01 18:16:56 +03:00
Nothing ->
( { model | toggledInterface = Just id }, Cmd.none )
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-12-01 14:03:25 +03:00
updateModel : Model -> String -> Identify -> List Service -> List Module -> List Blueprint -> Model
2020-11-26 21:47:37 +03:00
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)
2020-12-01 14:51:12 +03:00
moduleDict =
modules |> List.map (\m -> ( m.name, m )) |> Dict.fromList
2020-11-30 14:31:03 +03:00
2020-12-01 14:51:12 +03:00
blueprintDict =
2020-12-01 15:43:06 +03:00
blueprints |> List.map (\b -> ( b.id, b )) |> Dict.fromList
2020-11-30 14:31:03 +03:00
2020-12-01 14:51:12 +03:00
updatedModules =
Dict.union moduleDict model.modules
2020-11-26 21:47:37 +03:00
2020-12-01 14:51:12 +03:00
updatedBlueprints =
Dict.union blueprintDict model.blueprints
2020-11-30 14:31:03 +03:00
2020-12-01 14:51:12 +03:00
newData =
{ data | identify = identify, services = services, modules = Dict.keys moduleDict, blueprints = Dict.keys blueprintDict }
2020-11-30 14:31:03 +03:00
2020-12-01 14:51:12 +03:00
updated =
Dict.insert peer newData model.discoveredPeers
2020-11-27 16:03:18 +03:00
in
2020-12-01 14:51:12 +03:00
{ model | discoveredPeers = updated, modules = updatedModules, blueprints = updatedBlueprints }