Class: AWSEdges::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-edges/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_data, aws_data) ⇒ Graph

Returns a new instance of Graph.



16
17
18
19
# File 'lib/aws-edges/graph.rb', line 16

def initialize(config_data, aws_data)
  @config = config_data
  @aws = aws_data
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



14
15
16
# File 'lib/aws-edges/graph.rb', line 14

def format
  @format
end

Instance Method Details

#assign_color_attribute(color, edge) ⇒ Object

TODO: assign colors all eg: edge_attribs << orange



130
131
132
# File 'lib/aws-edges/graph.rb', line 130

def assign_color_attribute(color, edge)
  return "#{color} << node(\"#{edge}\");"
end

#assign_shape_attribute(shape, edge) ⇒ Object

TODO: assign shapes all eg: edge_attribs << triangle



137
138
139
# File 'lib/aws-edges/graph.rb', line 137

def assign_shape_attribute(shape, edge)
  return "#{shape} << node(\"#{edge}\");"
end

#create_graphObject

Main method for creating graphviz digraphs from the parsed config file



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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) ⇒ Object

Dynamically create graphviz digraph statements based on the config file input



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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) ⇒ Object

Generate the graphviz digraph statement for edges



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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