Няма описание http://j1x-huginn.herokuapp.com

dot_helper.rb 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. module DotHelper
  2. def render_agents_diagram(agents)
  3. if (command = ENV['USE_GRAPHVIZ_DOT']) &&
  4. (svg = IO.popen([command, *%w[-Tsvg -q1 -o/dev/stdout /dev/stdin]], 'w+') { |dot|
  5. dot.print agents_dot(agents, true)
  6. dot.close_write
  7. dot.read
  8. } rescue false)
  9. svg.html_safe
  10. else
  11. tag('img', src: URI('https://chart.googleapis.com/chart').tap { |uri|
  12. uri.query = URI.encode_www_form(cht: 'gv', chl: agents_dot(agents))
  13. })
  14. end
  15. end
  16. private
  17. def dot_id(string)
  18. # Backslash escaping seems to work for the backslash itself,
  19. # despite the DOT language document.
  20. '"%s"' % string.gsub(/\\/, "\\\\\\\\").gsub(/"/, "\\\\\"")
  21. end
  22. def disabled_label(agent)
  23. agent.disabled? ? dot_id(agent.name + " (Disabled)") : dot_id(agent.name)
  24. end
  25. def agents_dot(agents, rich = false)
  26. "digraph foo {".tap { |dot|
  27. agents.each.with_index do |agent, index|
  28. if rich
  29. dot << '%s[URL=%s];' % [disabled_label(agent), dot_id(agent_path(agent.id))]
  30. else
  31. dot << '%s;' % disabled_label(agent)
  32. end
  33. agent.receivers.each do |receiver|
  34. dot << "%s->%s;" % [disabled_label(agent), disabled_label(receiver)]
  35. end
  36. end
  37. dot << "}"
  38. }
  39. end
  40. end