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

agent_helper.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. module AgentHelper
  2. def agent_show_view(agent)
  3. name = agent.short_type.underscore
  4. if File.exist?(Rails.root.join("app", "views", "agents", "agent_views", name, "_show.html.erb"))
  5. File.join("agents", "agent_views", name, "show")
  6. end
  7. end
  8. def toggle_disabled_text
  9. if cookies[:huginn_view_only_enabled_agents]
  10. " Show Disabled Agents"
  11. else
  12. " Hide Disabled Agents"
  13. end
  14. end
  15. def scenario_links(agent)
  16. agent.scenarios.map { |scenario|
  17. link_to(scenario.name, scenario, class: "label", style: style_colors(scenario))
  18. }.join(" ").html_safe
  19. end
  20. def agent_show_class(agent)
  21. agent.short_type.underscore.dasherize
  22. end
  23. def agent_schedule(agent, delimiter = ', ')
  24. return 'n/a' unless agent.can_be_scheduled?
  25. case agent.schedule
  26. when nil, 'never'
  27. agent_controllers(agent, delimiter) || 'Never'
  28. else
  29. [
  30. agent.schedule.humanize.titleize,
  31. *(agent_controllers(agent, delimiter))
  32. ].join(delimiter).html_safe
  33. end
  34. end
  35. def agent_controllers(agent, delimiter = ', ')
  36. if agent.controllers.present?
  37. agent.controllers.map { |agent|
  38. link_to(agent.name, agent_path(agent))
  39. }.join(delimiter).html_safe
  40. end
  41. end
  42. def agent_dry_run_with_event_mode(agent)
  43. case
  44. when agent.cannot_receive_events?
  45. 'no'.freeze
  46. when agent.cannot_be_scheduled?
  47. # incoming event is the only trigger for the agent
  48. 'yes'.freeze
  49. else
  50. 'maybe'.freeze
  51. end
  52. end
  53. def agent_type_icon(agent, agents)
  54. receiver_count = links_counter_cache(agents)[:links_as_receiver][agent.id] || 0
  55. control_count = links_counter_cache(agents)[:control_links_as_controller][agent.id] || 0
  56. source_count = links_counter_cache(agents)[:links_as_source][agent.id] || 0
  57. if control_count > 0 && receiver_count > 0
  58. content_tag('span') do
  59. concat icon_tag('glyphicon-arrow-right')
  60. concat tag('br')
  61. concat icon_tag('glyphicon-new-window', class: 'glyphicon-flipped')
  62. end
  63. elsif control_count > 0 && receiver_count == 0
  64. icon_tag('glyphicon-new-window', class: 'glyphicon-flipped')
  65. elsif receiver_count > 0 && source_count == 0
  66. icon_tag('glyphicon-arrow-right')
  67. elsif receiver_count == 0 && source_count > 0
  68. icon_tag('glyphicon-arrow-left')
  69. elsif receiver_count > 0 && source_count > 0
  70. icon_tag('glyphicon-transfer')
  71. else
  72. icon_tag('glyphicon-unchecked')
  73. end
  74. end
  75. private
  76. def links_counter_cache(agents)
  77. @counter_cache ||= {}
  78. @counter_cache[agents.__id__] ||= {}.tap do |cache|
  79. agent_ids = agents.map(&:id)
  80. cache[:links_as_receiver] = Hash[Link.where(receiver_id: agent_ids)
  81. .group(:receiver_id)
  82. .pluck('receiver_id', 'count(receiver_id) as id')]
  83. cache[:links_as_source] = Hash[Link.where(source_id: agent_ids)
  84. .group(:source_id)
  85. .pluck('source_id', 'count(source_id) as id')]
  86. cache[:control_links_as_controller] = Hash[ControlLink.where(controller_id: agent_ids)
  87. .group(:controller_id)
  88. .pluck('controller_id', 'count(controller_id) as id')]
  89. end
  90. end
  91. end