2019-07-14 11:47:06 +00:00
|
|
|
defmodule BackendWeb.GraphView do
|
|
|
|
use BackendWeb, :view
|
|
|
|
alias BackendWeb.GraphView
|
|
|
|
|
|
|
|
def render("index.json", %{nodes: nodes, edges: edges}) do
|
|
|
|
%{
|
|
|
|
nodes: render_many(nodes, GraphView, "node.json"),
|
|
|
|
edges: render_many(edges, GraphView, "edge.json")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("node.json", %{graph: node}) do
|
|
|
|
size =
|
|
|
|
case node.user_count > 1 do
|
|
|
|
true -> :math.log(node.user_count)
|
|
|
|
false -> 1
|
|
|
|
end
|
|
|
|
|
2019-07-18 10:21:12 +00:00
|
|
|
# This is the format that cytoscape.js expects.
|
2019-07-14 11:47:06 +00:00
|
|
|
%{
|
2019-07-18 10:21:12 +00:00
|
|
|
data: %{
|
|
|
|
id: node.domain,
|
|
|
|
label: node.domain,
|
|
|
|
size: size
|
|
|
|
},
|
|
|
|
position: %{
|
|
|
|
x: node.x,
|
|
|
|
y: node.y
|
|
|
|
}
|
2019-07-14 11:47:06 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("edge.json", %{graph: edge}) do
|
|
|
|
%{
|
2019-07-18 10:21:12 +00:00
|
|
|
data: %{
|
|
|
|
id: edge.id,
|
|
|
|
source: edge.source_domain,
|
|
|
|
target: edge.target_domain,
|
|
|
|
weight: edge.weight
|
|
|
|
}
|
2019-07-14 11:47:06 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|