Created
November 15, 2018 02:00
-
-
Save imetallica/2465a2e2098c96cab51bbf93aeb50851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp refresh_graph(%{graph: g}) do | |
g = | |
if is_nil(g) do | |
:digraph.new() | |
else | |
:digraph.delete(g) | |
:digraph.new() | |
end | |
GraphDocument | |
|> Repo.all() | |
|> Enum.map(fn | |
%{type: "vertex"} = v -> | |
:digraph.add_vertex(g, v) | |
%{type: "edge", from_id: f, to_id: t} = e -> | |
from_vertex = g |> :digraph.vertices() |> Enum.find(fn v -> v.id === f end) | |
to_vertex = g |> :digraph.vertices() |> Enum.find(fn v -> v.id === t end) | |
:digraph.add_edge(g, from_vertex, to_vertex, e) | |
end) | |
{:noreply, %{graph: g}} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment