agents_exporter.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class AgentsExporter
  2. attr_accessor :options
  3. def initialize(options)
  4. self.options = options
  5. end
  6. # Filename should have no commas or special characters to support Content-Disposition on older browsers.
  7. def filename
  8. ((options[:name] || '').downcase.gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-|-$/, '').presence || 'exported-agents') + ".json"
  9. end
  10. def as_json(opts = {})
  11. {
  12. :name => options[:name].presence || 'No name provided',
  13. :description => options[:description].presence || 'No description provided',
  14. :source_url => options[:source_url],
  15. :guid => options[:guid],
  16. :tag_fg_color => options[:tag_fg_color],
  17. :tag_bg_color => options[:tag_bg_color],
  18. :exported_at => Time.now.utc.iso8601,
  19. :agents => agents.map { |agent| agent_as_json(agent) },
  20. :links => links
  21. }
  22. end
  23. def agents
  24. options[:agents].to_a
  25. end
  26. def links
  27. agent_ids = agents.map(&:id)
  28. contained_links = agents.map.with_index do |agent, index|
  29. agent.links_as_source.where(:receiver_id => agent_ids).map do |link|
  30. { :source => index, :receiver => agent_ids.index(link.receiver_id) }
  31. end
  32. end
  33. contained_links.flatten.compact
  34. end
  35. def agent_as_json(agent)
  36. {
  37. :type => agent.type,
  38. :name => agent.name,
  39. :disabled => agent.disabled,
  40. :guid => agent.guid,
  41. :options => agent.options
  42. }.tap do |options|
  43. options[:schedule] = agent.schedule if agent.can_be_scheduled?
  44. options[:keep_events_for] = agent.keep_events_for if agent.can_create_events?
  45. options[:propagate_immediately] = agent.propagate_immediately if agent.can_receive_events?
  46. end
  47. end
  48. end