agents_controller.rb 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. class AgentsController < ApplicationController
  2. include DotHelper
  3. include ActionView::Helpers::TextHelper
  4. include SortableTable
  5. def index
  6. set_table_sort sorts: %w[name created_at last_check_at last_event_at last_receive_at], default: { created_at: :desc }
  7. @agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
  8. respond_to do |format|
  9. format.html
  10. format.json { render json: @agents }
  11. end
  12. end
  13. def handle_details_post
  14. @agent = current_user.agents.find(params[:id])
  15. if @agent.respond_to?(:handle_details_post)
  16. render :json => @agent.handle_details_post(params) || {}
  17. else
  18. @agent.error "#handle_details_post called on an instance of #{@agent.class} that does not define it."
  19. head 500
  20. end
  21. end
  22. def run
  23. @agent = current_user.agents.find(params[:id])
  24. Agent.async_check(@agent.id)
  25. respond_to do |format|
  26. format.html { redirect_back "Agent run queued for '#{@agent.name}'" }
  27. format.json { head :ok }
  28. end
  29. end
  30. def dry_run
  31. attrs = params[:agent] || {}
  32. if agent = current_user.agents.find_by(id: params[:id])
  33. # POST /agents/:id/dry_run
  34. if attrs.present?
  35. type = agent.type
  36. agent = Agent.build_for_type(type, current_user, attrs)
  37. end
  38. else
  39. # POST /agents/dry_run
  40. type = attrs.delete(:type)
  41. agent = Agent.build_for_type(type, current_user, attrs)
  42. end
  43. agent.name ||= '(Untitled)'
  44. if agent.valid?
  45. if event_payload = params[:event]
  46. dummy_agent = Agent.build_for_type('ManualEventAgent', current_user, name: 'Dry-Runner')
  47. dummy_agent.readonly!
  48. event = dummy_agent.events.build(user: current_user, payload: event_payload)
  49. end
  50. results = agent.dry_run!(event)
  51. render json: {
  52. log: results[:log],
  53. events: Utils.pretty_print(results[:events], false),
  54. memory: Utils.pretty_print(results[:memory] || {}, false),
  55. }
  56. else
  57. render json: {
  58. log: [
  59. "#{pluralize(agent.errors.count, "error")} prohibited this Agent from being saved:",
  60. *agent.errors.full_messages
  61. ].join("\n- "),
  62. events: '',
  63. memory: '',
  64. }
  65. end
  66. end
  67. def type_details
  68. @agent = Agent.build_for_type(params[:type], current_user, {})
  69. initialize_presenter
  70. render json: {
  71. can_be_scheduled: @agent.can_be_scheduled?,
  72. default_schedule: @agent.default_schedule,
  73. can_receive_events: @agent.can_receive_events?,
  74. can_create_events: @agent.can_create_events?,
  75. can_control_other_agents: @agent.can_control_other_agents?,
  76. can_dry_run: @agent.can_dry_run?,
  77. options: @agent.default_options,
  78. description_html: @agent.html_description,
  79. oauthable: render_to_string(partial: 'oauth_dropdown', locals: { agent: @agent }),
  80. form_options: render_to_string(partial: 'options', locals: { agent: @agent })
  81. }
  82. end
  83. def event_descriptions
  84. html = current_user.agents.find(params[:ids].split(",")).group_by(&:type).map { |type, agents|
  85. agents.map(&:html_event_description).uniq.map { |desc|
  86. "<p><strong>#{type}</strong><br />" + desc + "</p>"
  87. }
  88. }.flatten.join()
  89. render :json => { :description_html => html }
  90. end
  91. def remove_events
  92. @agent = current_user.agents.find(params[:id])
  93. @agent.events.delete_all
  94. respond_to do |format|
  95. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  96. format.json { head :ok }
  97. end
  98. end
  99. def propagate
  100. respond_to do |format|
  101. if AgentPropagateJob.can_enqueue?
  102. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  103. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  104. format.json { head :ok }
  105. else
  106. format.html { redirect_back "Event propagation is already scheduled to run." }
  107. format.json { head :locked }
  108. end
  109. end
  110. end
  111. def destroy_memory
  112. @agent = current_user.agents.find(params[:id])
  113. @agent.update!(memory: {})
  114. respond_to do |format|
  115. format.html { redirect_back "Memory erased for '#{@agent.name}'" }
  116. format.json { head :ok }
  117. end
  118. end
  119. def show
  120. @agent = current_user.agents.find(params[:id])
  121. respond_to do |format|
  122. format.html
  123. format.json { render json: @agent }
  124. end
  125. end
  126. def new
  127. agents = current_user.agents
  128. if id = params[:id]
  129. @agent = agents.build_clone(agents.find(id))
  130. else
  131. @agent = agents.build
  132. end
  133. @agent.scenario_ids = [params[:scenario_id]] if params[:scenario_id] && current_user.scenarios.find_by(id: params[:scenario_id])
  134. initialize_presenter
  135. respond_to do |format|
  136. format.html
  137. format.json { render json: @agent }
  138. end
  139. end
  140. def edit
  141. @agent = current_user.agents.find(params[:id])
  142. initialize_presenter
  143. end
  144. def create
  145. build_agent
  146. respond_to do |format|
  147. if @agent.save
  148. format.html { redirect_back "'#{@agent.name}' was successfully created.", return: agents_path }
  149. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  150. else
  151. initialize_presenter
  152. format.html { render action: "new" }
  153. format.json { render json: @agent.errors, status: :unprocessable_entity }
  154. end
  155. end
  156. end
  157. def update
  158. @agent = current_user.agents.find(params[:id])
  159. respond_to do |format|
  160. if @agent.update_attributes(params[:agent])
  161. format.html { redirect_back "'#{@agent.name}' was successfully updated.", return: agents_path }
  162. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  163. else
  164. initialize_presenter
  165. format.html { render action: "edit" }
  166. format.json { render json: @agent.errors, status: :unprocessable_entity }
  167. end
  168. end
  169. end
  170. def leave_scenario
  171. @agent = current_user.agents.find(params[:id])
  172. @scenario = current_user.scenarios.find(params[:scenario_id])
  173. @agent.scenarios.destroy(@scenario)
  174. respond_to do |format|
  175. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  176. format.json { head :no_content }
  177. end
  178. end
  179. def destroy
  180. @agent = current_user.agents.find(params[:id])
  181. @agent.destroy
  182. respond_to do |format|
  183. format.html { redirect_back "'#{@agent.name}' deleted" }
  184. format.json { head :no_content }
  185. end
  186. end
  187. def validate
  188. build_agent
  189. if @agent.validate_option(params[:attribute])
  190. render text: 'ok'
  191. else
  192. render text: 'error', status: 403
  193. end
  194. end
  195. def complete
  196. build_agent
  197. render json: @agent.complete_option(params[:attribute])
  198. end
  199. protected
  200. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  201. def redirect_back(message, options = {})
  202. if path = filtered_agent_return_link(options)
  203. redirect_to path, notice: message
  204. else
  205. super agents_path, notice: message
  206. end
  207. end
  208. def build_agent
  209. @agent = Agent.build_for_type(params[:agent].delete(:type),
  210. current_user,
  211. params[:agent])
  212. end
  213. def initialize_presenter
  214. if @agent.present? && @agent.is_form_configurable?
  215. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  216. end
  217. end
  218. end