class AWSEdges::Graph

Attributes

format[R]

Public Class Methods

new(config_data, aws_data) click to toggle source
# File lib/aws-edges/graph.rb, line 16
def initialize(config_data, aws_data)
  @config = config_data
  @aws = aws_data
end

Public Instance Methods

assign_color_attribute(color, edge) click to toggle source

TODO: assign colors all eg: edge_attribs << orange

# File lib/aws-edges/graph.rb, line 130
def assign_color_attribute(color, edge)
  return "#{color} << node(\"#{edge}\");"
end
assign_shape_attribute(shape, edge) click to toggle source

TODO: assign shapes all eg: edge_attribs << triangle

# File lib/aws-edges/graph.rb, line 137
def assign_shape_attribute(shape, edge)
  return "#{shape} << node(\"#{edge}\");"
end
create_graph() click to toggle source

Main method for creating graphviz digraphs from the parsed config file

# File lib/aws-edges/graph.rb, line 167
def create_graph()
  ##
  # default to png output format
  # for a full list: http://www.graphviz.org/doc/info/output.html
  format = @config.delete('save_as')
  format = "png" unless format
  graph_name = @config.delete('name').gsub(/\s+/,'_')
  rotate_layout = true if @config['rotate'] == true
  cmds = generate_graph_statements(@config)
  begin
  digraph do
    node_attribs << filled
    rotate if rotate_layout
    boxes
    eval cmds
    save "#{graph_name}", "#{format}"  
  end
  rescue Exception => e
    puts "Failed to create the graph: #{e}"
    exit 1
  end
end
generate_graph_statements(config) click to toggle source

Dynamically create graphviz digraph statements based on the config file input

# File lib/aws-edges/graph.rb, line 144
def generate_graph_statements(config)
  cmd_string = ""
  config.each do |key,value|
    if key == "cluster"
      cmd_string += "cluster \"#{value['label']}\" do " 
      cmd_string += "label \"#{value['label']}\";"
      cmd_string += generate_graph_statements(value)
      cmd_string += " end;"
    elsif key == "edges"
      value.each do |e|
        cmd_string += map_edges(
          e['from'], e['from_color'], e['from_shape'], 
          e['to'], e['to_color'], e['to_shape']
        )
      end
    end
  end
  cmd_string
end
map_edges(from, from_color, from_shape, to, to_color, to_shape) click to toggle source

Generate the graphviz digraph statement for edges

# File lib/aws-edges/graph.rb, line 24
def map_edges(from, from_color, from_shape, to, to_color, to_shape)
  cmd_string = ""  
  from_prefix, from_node = $1, $2 if from =~ /^(\w+?)_(.+)/
  to_prefix, to_node = $1, $2 if to =~ /^(\w+?)_(.+)/
  @aws[:"#{from_prefix}"].each do |node|
        if from_node.include?('-')
          (parent, child) = from_node.split('-')
          node[:"#{parent}"].each do |i|
            cmd_string += " edge " + '"' + 
            i[:"#{child}"].to_s + '","' +
            node[:"#{to_node}"].to_s + '";'

            unless from_color.nil?
              unless from_color.empty?
                cmd_string += assign_color_attribute(from_color, i[:"#{child}"])
              end
            end

            unless from_shape.nil?
              unless from_shape.empty?
                cmd_string += assign_shape_attribute(from_shape, i[:"#{child}"])
              end
            end

            unless to_color.nil?
              unless to_color.empty?
                cmd_string += assign_color_attribute(to_color, node[:"#{to_node}"]) 
              end
            end

            unless to_shape.nil?
              unless to_shape.empty?
                cmd_string += assign_shape_attribute(to_shape, node[:"#{to_node}"]) 
              end
            end

          end unless node[:"#{parent}"].nil?
        elsif to_node.include?('-')
          (parent, child) = to_node.split('-')
          node[:"#{parent}"].each do |i|
            cmd_string += " edge " + '"' + 
            node[:"#{from_node}"].to_s + '","' +
            i[:"#{child}"].to_s + '";'

            unless from_color.nil?
              unless from_color.empty?
                cmd_string += assign_color_attribute(from_color, node[:"#{from_node}"])
              end
            end

            unless from_shape.nil?
              unless from_shape.empty?
                cmd_string += assign_shape_attribute(from_shape, node[:"#{from_node}"])
              end
            end

            unless to_color.nil?
              unless to_color.empty?
                cmd_string += assign_color_attribute(to_color, i[:"#{child}"]) 
              end
            end

            unless to_shape.nil?
              unless to_shape.empty?
                cmd_string += assign_shape_attribute(to_shape, i[:"#{child}"]) 
              end
            end

          end unless node[:"#{parent}"].nil?
        else
          cmd_string += " edge " + '"' + 
          node[:"#{from_node}"].to_s + '","' + 
          node[:"#{to_node}"].to_s + '";'

          unless from_color.nil?
            unless from_color.empty?
              cmd_string += assign_color_attribute(from_color, node[:"#{from_node}"])
            end
          end

          unless from_shape.nil?
            unless from_shape.empty?
              cmd_string += assign_shape_attribute(from_shape, node[:"#{from_node}"])
            end
          end

          unless to_color.nil?
            unless to_color.empty?
              cmd_string += assign_color_attribute(to_color, node[:"#{to_node}"]) 
            end
          end

          unless to_shape.nil?
            unless to_shape.empty?
              cmd_string += assign_shape_attribute(to_shape, node[:"#{to_node}"]) 
            end
          end

        end
  end
  cmd_string
end