Skip to content

Instantly share code, notes, and snippets.

@imetallica
Created November 15, 2018 02:00
Show Gist options
  • Save imetallica/2465a2e2098c96cab51bbf93aeb50851 to your computer and use it in GitHub Desktop.
Save imetallica/2465a2e2098c96cab51bbf93aeb50851 to your computer and use it in GitHub Desktop.
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