defmodule Superintelligence.TreeInspector do def count_depth do count_depth(Superintelligence.Supervisor) end defp count_depth(supervisor, depth \\ 0) do case Supervisor.which_children(supervisor) do [] -> depth children -> child_supervisor = find_child_supervisor(children) if child_supervisor do count_depth(child_supervisor, depth + 1) else depth + 1 end end end defp find_child_supervisor(children) do Enum.find_value(children, fn {_, pid, :supervisor, _} when is_pid(pid) -> pid _ -> nil end) end def print_tree do print_tree(Superintelligence.Supervisor, 0) end defp print_tree(supervisor, level) do indent = String.duplicate(" ", level) IO.puts("#{indent}#{inspect(supervisor)}") case Supervisor.which_children(supervisor) do [] -> :ok children -> Enum.each(children, fn {id, pid, type, _modules} when is_pid(pid) -> IO.puts("#{indent} └─ #{inspect(id)} (#{type})") if type == :supervisor do print_tree(pid, level + 2) end _ -> :ok end) end end end