dry_runs_controller.rb 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module Agents
  2. class DryRunsController < ApplicationController
  3. include ActionView::Helpers::TextHelper
  4. def index
  5. @events = if params[:agent_id]
  6. current_user.agents.find_by(id: params[:agent_id]).received_events.limit(5)
  7. elsif params[:source_ids]
  8. Event.where(agent_id: current_user.agents.where(id: params[:source_ids]).pluck(:id))
  9. .order("id DESC").limit(5)
  10. end
  11. render layout: false
  12. end
  13. def create
  14. attrs = params[:agent] || {}
  15. if agent = current_user.agents.find_by(id: params[:agent_id])
  16. # POST /agents/:id/dry_run
  17. if attrs.present?
  18. type = agent.type
  19. agent = Agent.build_for_type(type, current_user, attrs)
  20. end
  21. else
  22. # POST /agents/dry_run
  23. type = attrs.delete(:type)
  24. agent = Agent.build_for_type(type, current_user, attrs)
  25. end
  26. agent.name ||= '(Untitled)'
  27. if agent.valid?
  28. if event_payload = params[:event]
  29. dummy_agent = Agent.build_for_type('ManualEventAgent', current_user, name: 'Dry-Runner')
  30. dummy_agent.readonly!
  31. event = dummy_agent.events.build(user: current_user, payload: event_payload)
  32. end
  33. @results = agent.dry_run!(event)
  34. else
  35. @results = { events: [], memory: [],
  36. log: [
  37. "#{pluralize(agent.errors.count, "error")} prohibited this Agent from being saved:",
  38. *agent.errors.full_messages
  39. ].join("\n- ") }
  40. end
  41. render layout: false
  42. end
  43. end
  44. end