agent_helper.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. builtin_schedule_name(agent.schedule),
  31. *(agent_controllers(agent, delimiter))
  32. ].join(delimiter).html_safe
  33. end
  34. end
  35. def builtin_schedule_name(schedule)
  36. AgentHelper.builtin_schedule_name(schedule)
  37. end
  38. def self.builtin_schedule_name(schedule)
  39. schedule == 'every_7d' ? 'Every Monday' : schedule.humanize.titleize
  40. end
  41. def agent_controllers(agent, delimiter = ', ')
  42. if agent.controllers.present?
  43. agent.controllers.map { |agent|
  44. link_to(agent.name, agent_path(agent))
  45. }.join(delimiter).html_safe
  46. end
  47. end
  48. def agent_dry_run_with_event_mode(agent)
  49. case
  50. when agent.cannot_receive_events?
  51. 'no'.freeze
  52. when agent.cannot_be_scheduled?
  53. # incoming event is the only trigger for the agent
  54. 'yes'.freeze
  55. else
  56. 'maybe'.freeze
  57. end
  58. end
  59. def agent_type_icon(agent, agents)
  60. receiver_count = links_counter_cache(agents)[:links_as_receiver][agent.id] || 0
  61. control_count = links_counter_cache(agents)[:control_links_as_controller][agent.id] || 0
  62. source_count = links_counter_cache(agents)[:links_as_source][agent.id] || 0
  63. if control_count > 0 && receiver_count > 0
  64. content_tag('span') do
  65. concat icon_tag('glyphicon-arrow-right')
  66. concat tag('br')
  67. concat icon_tag('glyphicon-new-window', class: 'glyphicon-flipped')
  68. end
  69. elsif control_count > 0 && receiver_count == 0
  70. icon_tag('glyphicon-new-window', class: 'glyphicon-flipped')
  71. elsif receiver_count > 0 && source_count == 0
  72. icon_tag('glyphicon-arrow-right')
  73. elsif receiver_count == 0 && source_count > 0
  74. icon_tag('glyphicon-arrow-left')
  75. elsif receiver_count > 0 && source_count > 0
  76. icon_tag('glyphicon-transfer')
  77. else
  78. icon_tag('glyphicon-unchecked')
  79. end
  80. end
  81. def agent_type_select_options
  82. Rails.cache.fetch('agent_type_select_options') do
  83. [['Select an Agent Type', 'Agent', {title: ''}]] + Agent.types.map {|type| [agent_type_to_human(type.name), type, {title: h(Agent.build_for_type(type.name, User.new(id: 0), {}).html_description.lines.first.strip)}] }
  84. end
  85. end
  86. private
  87. def links_counter_cache(agents)
  88. @counter_cache ||= {}
  89. @counter_cache[agents.__id__] ||= {}.tap do |cache|
  90. agent_ids = agents.map(&:id)
  91. cache[:links_as_receiver] = Hash[Link.where(receiver_id: agent_ids)
  92. .group(:receiver_id)
  93. .pluck(:receiver_id, Arel.sql('count(receiver_id) as id'))]
  94. cache[:links_as_source] = Hash[Link.where(source_id: agent_ids)
  95. .group(:source_id)
  96. .pluck(:source_id, Arel.sql('count(source_id) as id'))]
  97. cache[:control_links_as_controller] = Hash[ControlLink.where(controller_id: agent_ids)
  98. .group(:controller_id)
  99. .pluck(:controller_id, Arel.sql('count(controller_id) as id'))]
  100. end
  101. end
  102. end