aboutsummaryrefslogtreecommitdiff
path: root/apps/dreki_web/src/ui/dreki_web_ui_index.erl
blob: 9f4684ecb8fd390c1997518a89cb24a201882509 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-module(dreki_web_ui_index).
-behaviour(cowboy_handler).
-export([init/2]).

init(Req = #{method := <<"GET">>}, State) ->
  PrettyWorld = jsone:encode(dreki_world:to_map(), [canonical_form, {space, 1}, {indent, 4}]),
  LocalTasksStores = dreki_tasks:local_stores(),
  {ok, Peers} = partisan_peer_service:members(),
  NavTree = nav_tree(dreki_world_dns:as_map()),
  Html = dreki_web_ui:render(Req, index_dtl, [
    {"page_title", "Admin UI"},
    {"dreki_world_pretty_json", PrettyWorld},
    {"dreki_local_tasks_stores", LocalTasksStores},
    {"dreki_peers", Peers},
    {"nav_tree", NavTree}
  ]),
  {ok, dreki_web_ui:reply_html(Req, 200, Html), State};
init(Req, _) ->
  dreki_web_ui_error:init(Req, #{code => 400, status => "Bad request"}).

nav_tree(#{vertices := Vertices}) ->
  nav_tree(Vertices, []).

nav_tree([Region = #{type := region, name := Name, data := #{display_name := Display}} | Rest], Acc) ->
  nav_tree(Rest, [Region#{href => <<"/admin/regions/", Name/binary>>, title => Display} | Acc]);
nav_tree([Node = #{type := node, name := Name, data := #{display_name := Display}} | Rest], Acc) ->
  nav_tree(Rest, [Node#{href => <<"/admin/nodes/", Name/binary>>, title => Display} | Acc]);
nav_tree([#{type := Root} | Rest], Acc) ->
  nav_tree(Rest, Acc);
nav_tree([], Acc) ->
  Acc.