Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #39 from erlanglab/update-traffic-data
Browse files Browse the repository at this point in the history
Update traffic data
  • Loading branch information
baransu authored Apr 9, 2017
2 parents 4140291 + 0830477 commit 51b2619
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
38 changes: 22 additions & 16 deletions apps/epl/src/epl_traffic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ handle_cast({unsubscribe, Pid}, State = #state{subscribers = Subs}) ->
handle_cast(Request, _State) ->
exit({not_implemented, Request}).

handle_info({data, {_Node, _Timestamp}, Proplist},
handle_info({data, {Node, _Timestamp}, Proplist},
State = #state{subscribers = Subs,
traffic = OldTraffic,
msg_pass = OldMsgPass}) ->

{Viz1, NewTraffic} = update_traffic_graph(OldTraffic, new()),
{Viz1, NewTraffic} = update_traffic_graph(Node, OldTraffic, new(Node)),

%% By convention we use <<"INTERNET">> as a name of the region,
%% which represents the observed node
Viz2 = get_message_passing_counters(<<"INTERNET">>, Proplist,

%% We're starting from observed node which is our graph entry point
Viz2 = get_message_passing_counters(Node, Proplist,
Viz1, OldMsgPass),

%% push an update to all subscribed WebSockets
Expand Down Expand Up @@ -107,7 +107,7 @@ get_message_passing_counters(Node, Proplist, Vizceral, OldMsgPass) ->
Vizceral,
Proplist).

update_message_passing_graph(Node, Send, Vizceral, OldMsgPass) ->
update_message_passing_graph(Node, Send, Vizceral, _OldMsgPass) ->
%% the INTERNET node represents the source of ingress traffic
Vizceral1 = push_focused(<<"INTERNET">>, Node, Vizceral),

Expand Down Expand Up @@ -160,13 +160,13 @@ get_traffic_counters() ->
{NodeName, [{owner,_}, {state,up}, {address, _}, {type,normal},
{in,NodeIn}, {out,NodeOut}]} <- NodesInfo].

update_traffic_graph(OldCounters, Vizceral) ->
update_traffic_graph(EntryNode, OldCounters, Vizceral) ->
%% traverse all connected nodes and read their net_kernel counters
NewCounters = get_traffic_counters(),

%% start creating a map, which represents the Vizceral JSON document
%% region named <<"INTERNET">> represents the observed node
V1 = push_region(<<"INTERNET">>, Vizceral),
V1 = push_region(EntryNode, Vizceral),

%% add as many regions as there are nodes in the cluster
V2 = lists:foldl(fun({Node,_,_}, V) ->
Expand All @@ -178,7 +178,7 @@ update_traffic_graph(OldCounters, Vizceral) ->
V3 = lists:foldl(
fun({Node, NewIn, NewOut}, V) ->
{OldIn, OldOut} = get_in_out(Node, OldCounters),
push_region_connection(<<"INTERNET">>, binarify(Node),
push_region_connection(EntryNode, binarify(Node),
{NewOut-OldOut, NewIn-OldIn, 0},
#{}, V)
end, V2, NewCounters),
Expand All @@ -203,8 +203,8 @@ command(Fun, Args) ->
%%%===================================================================
%%% functions manipulating Vizceral map
%%%===================================================================
new() ->
entity(global, "edge", [], #{connections => []}).
new(EntryNode) ->
entity(global, "edge", [], #{connections => [], entryNode => EntryNode}).

entity(Renderer, Name, Nodes, Additional) ->
Map = #{
Expand Down Expand Up @@ -242,6 +242,9 @@ push_region(Name, Vizceral) ->
push_region(Name, #{}, Vizceral).

push_region(Name, Additional, Vizceral) ->
%% We assume that INTERNET is entryNode for every region
%% Vizceral has backward compatibility and in region view INTERNET node is
%% default entryNode if other isn't specified
A = maps:merge(#{
connections => [],
maxVolume => 5000
Expand Down Expand Up @@ -280,10 +283,13 @@ push_connection(Source, Target, {N, W, D} , Additional, To) ->
maps:merge(To, #{connections => [New | Connections]}).

push_region_connection(Source, Target, {N, W, D}, Additional, Vizceral) ->
%% Will crash on nonexisting
pull_region(Source, Vizceral),
pull_region(Target, Vizceral),
push_connection(Source, Target, {N, W, D}, Additional, Vizceral).
%% Will crash on nonexisting
pull_region(Source, Vizceral),
pull_region(Target, Vizceral),
%% Outgoing traffic
Viz = push_connection(Source, Target, {N, 0, D}, Additional, Vizceral),
%% Incoming traffic
push_connection(Target, Source, {W, 0, D}, Additional, Viz).

push_focused_connection(S, T, RN, NWD, Vizceral) ->
push_focused_connection(S, T, RN, NWD, #{}, Vizceral).
Expand All @@ -303,5 +309,5 @@ push_focused(Name, RegionName, Additional, Vizceral) ->
fun(A) ->
maps:get(name, A) == namify(RegionName)
end, Nodes),
NewRegion = push_node(focused, Name, Additional, Region),
NewRegion = push_node(focusedChild, Name, Additional, Region),
maps:merge(Vizceral, #{nodes => [NewRegion | Rest]}).

0 comments on commit 51b2619

Please sign in to comment.