agent_helper.rb 3.9 KB

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