dry_runs_controller.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. else
  11. []
  12. end
  13. render layout: false
  14. end
  15. def create
  16. attrs = agent_params
  17. if agent = current_user.agents.find_by(id: params[:agent_id])
  18. # POST /agents/:id/dry_run
  19. if attrs.present?
  20. attrs = attrs.merge(memory: agent.memory)
  21. type = agent.type
  22. agent = Agent.build_for_type(type, current_user, attrs)
  23. end
  24. else
  25. # POST /agents/dry_run
  26. type = attrs.delete(:type)
  27. agent = Agent.build_for_type(type, current_user, attrs)
  28. end
  29. agent.name ||= '(Untitled)'
  30. if agent.valid?
  31. if event_payload = params[:event]
  32. dummy_agent = Agent.build_for_type('ManualEventAgent', current_user, name: 'Dry-Runner')
  33. dummy_agent.readonly!
  34. event = dummy_agent.events.build(user: current_user, payload: event_payload, created_at: Time.now)
  35. end
  36. @results = agent.dry_run!(event)
  37. else
  38. @results = { events: [], memory: [],
  39. log: [
  40. "#{pluralize(agent.errors.count, "error")} prohibited this Agent from being saved:",
  41. *agent.errors.full_messages
  42. ].join("\n- ") }
  43. end
  44. render layout: false
  45. end
  46. end
  47. end