2014-05-31 23:36:33 -07:00
|
|
|
class AgentsExporter
|
|
|
|
attr_accessor :options
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
self.options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
# Filename should have no commas or special characters to support Content-Disposition on older browsers.
|
|
|
|
def filename
|
|
|
|
((options[:name] || '').downcase.gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-|-$/, '').presence || 'exported-agents') + ".json"
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(opts = {})
|
|
|
|
{
|
2016-06-16 02:52:50 -04:00
|
|
|
schema_version: 1,
|
|
|
|
name: options[:name].presence || 'No name provided',
|
|
|
|
description: options[:description].presence || 'No description provided',
|
|
|
|
source_url: options[:source_url],
|
|
|
|
guid: options[:guid],
|
|
|
|
tag_fg_color: options[:tag_fg_color],
|
|
|
|
tag_bg_color: options[:tag_bg_color],
|
|
|
|
icon: options[:icon],
|
|
|
|
exported_at: Time.now.utc.iso8601,
|
|
|
|
agents: agents.map { |agent| agent_as_json(agent) },
|
|
|
|
links: links,
|
|
|
|
control_links: control_links
|
2014-05-31 23:36:33 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def agents
|
2015-12-07 10:30:14 -07:00
|
|
|
options[:agents].sort_by{|agent| agent.guid}.to_a
|
2014-05-31 23:36:33 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def links
|
|
|
|
agent_ids = agents.map(&:id)
|
|
|
|
|
|
|
|
contained_links = agents.map.with_index do |agent, index|
|
2015-07-18 23:07:50 -04:00
|
|
|
agent.links_as_source.where(receiver_id: agent_ids).map do |link|
|
|
|
|
{ source: index, receiver: agent_ids.index(link.receiver_id) }
|
2014-05-31 23:36:33 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
contained_links.flatten.compact
|
|
|
|
end
|
|
|
|
|
2015-07-18 23:07:50 -04:00
|
|
|
def control_links
|
|
|
|
agent_ids = agents.map(&:id)
|
|
|
|
|
|
|
|
contained_controller_links = agents.map.with_index do |agent, index|
|
|
|
|
agent.control_links_as_controller.where(control_target_id: agent_ids).map do |control_link|
|
|
|
|
{ controller: index, control_target: agent_ids.index(control_link.control_target_id) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
contained_controller_links.flatten.compact
|
|
|
|
end
|
|
|
|
|
2014-05-31 23:36:33 -07:00
|
|
|
def agent_as_json(agent)
|
|
|
|
{
|
|
|
|
:type => agent.type,
|
|
|
|
:name => agent.name,
|
|
|
|
:disabled => agent.disabled,
|
2014-06-04 22:26:56 -07:00
|
|
|
:guid => agent.guid,
|
2014-05-31 23:36:33 -07:00
|
|
|
:options => agent.options
|
2014-06-08 23:37:15 -07:00
|
|
|
}.tap do |options|
|
|
|
|
options[:schedule] = agent.schedule if agent.can_be_scheduled?
|
|
|
|
options[:keep_events_for] = agent.keep_events_for if agent.can_create_events?
|
|
|
|
options[:propagate_immediately] = agent.propagate_immediately if agent.can_receive_events?
|
|
|
|
end
|
2014-05-31 23:36:33 -07:00
|
|
|
end
|
2014-08-20 03:03:29 -03:00
|
|
|
end
|