Class: ASTNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ast_node.rb

Constant Summary collapse

DEFAULT_GRAPH_PROPERTIES =
{"hideLegend" => "false"}
METRIC_PROC_FUNCTIONS =

An array of proc functions to be applied on compile time, the first argument is the metric node and the second, the current string its result will be assigned to the compiled string

[ Proc.new { |metric,aux| metric.properties[:is_negative] ? aux : "cactiStyle(#{aux})" } ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data) ⇒ ASTNode

Returns a new instance of ASTNode.



20
21
22
23
24
25
26
27
28
# File 'lib/ast_node.rb', line 20

def initialize(raw_data)
  @root_node = nil
  @raw_data = raw_data
  @children = []
  @properties = {'graph_period' => "seconds","category" => "other"}
  @graph_properties = DEFAULT_GRAPH_PROPERTIES
  @graph_properties[:colorList] = default_colors
  @parent = nil
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/ast_node.rb', line 5

def children
  @children
end

#graph_propertiesObject

Returns the value of attribute graph_properties.



5
6
7
# File 'lib/ast_node.rb', line 5

def graph_properties
  @graph_properties
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/ast_node.rb', line 5

def parent
  @parent
end

#propertiesObject

Returns the value of attribute properties.



5
6
7
# File 'lib/ast_node.rb', line 5

def properties
  @properties
end

#root_nodeObject

Returns the value of attribute root_node.



5
6
7
# File 'lib/ast_node.rb', line 5

def root_node
  @root_node
end

Instance Method Details

#add_child(child) ⇒ Object

Add a child to the node



43
44
45
46
47
48
49
50
51
52
# File 'lib/ast_node.rb', line 43

def add_child(child)
  if self.root?
    child.root_node = self
  else
    child.root_node = self.root_node
  end

  child.parent = self
  children << child
end

#children_of_class(klass) ⇒ Object



38
39
40
# File 'lib/ast_node.rb', line 38

def children_of_class(klass)
  children.select { |i| i.is_a? klass }
end

#compileObject



54
55
56
57
58
59
# File 'lib/ast_node.rb', line 54

def compile
  # The compilation is done twice cause there are certain cases where is necessary, a better implementation would control whether this is needed
  # or not, but given the small impact I just do it twice
  children.map{|i| i.compile} if children
  children.map{|i| i.compile} if children
end

#config=(config) ⇒ Object



30
31
32
# File 'lib/ast_node.rb', line 30

def config=(config)
  self.properties.merge!(config)
end

#default_colorsObject



13
14
15
16
17
18
# File 'lib/ast_node.rb', line 13

def default_colors
  %w(#00CC00 #0066B3 #FF8000 #FFCC00 #330099 #990099 #CCFF00 #FF0000 #808080
     #008F00 #00487D #B35A00 #B38F00         #6B006B #8FB300 #B30000 #BEBEBE
     #80FF80 #80C9FF #FFC080 #FFE680 #AA80FF #EE00CC #FF8080
     #666600 #FFBFFF #00FFCC #CC6699 #999900)
end

#process_variables(properties) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ast_node.rb', line 66

def process_variables(properties)
  [:vtitle,:title].each do |key|
    aux = properties[key]
    properties[key].scan(/\$\{(.*)\}/).each do 
      if self.properties.has_key? $1
        aux.gsub!(/\$\{#{$1}\}/,self.properties[$1])
      end
    end if properties[key]
    properties[key] = aux

  end
end

#properties_to_urlObject

Returns the global properties as url values



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ast_node.rb', line 80

def properties_to_url
  
  # Color List initialization
  aux_graph_properties = self.graph_properties.clone
  process_variables(aux_graph_properties)
  aux_graph_properties[:colorList] = aux_graph_properties[:colorList].join(",") if aux_graph_properties[:colorList]

  # Change of the base stuff
  if  self.properties[:base]
    aux_graph_properties[:yMax] = aux_graph_properties[:yMax].to_f / self.properties[:base] 
    aux_graph_properties[:yMin] = aux_graph_properties[:yMin].to_f / self.properties[:base]
    aux_graph_properties.delete :yMax 
    aux_graph_properties.delete :yMin
  end

  aux = aux_graph_properties.map{|i,j| "#{i}=#{URI.escape(j.to_s.gsub('%','percent'))}"}.join("&")
  return aux
end

#root?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ast_node.rb', line 34

def root?
  @root_node == nil
end

#targetsObject

Returns the FieldDeclaration Nodes



62
63
64
# File 'lib/ast_node.rb', line 62

def targets
  children_of_class(FieldDeclarationNode)
end

#to_gdashObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ast_node.rb', line 105

def to_gdash
  output = ""
  self.compile
  self.graph_properties.each do |k,v| 
    output += k.to_s + "\t\t" + '"' + v.to_s + '"' + "\n" unless k == :colorList || k == :yMin || k== :yMax
  end
  count = 0
  targets.each do |tg|
    metric_alias = tg.properties.delete(:alias)
    tg.children.delete_if { |i| i.class == LabelFieldPropertyNode}
    output += "field :#{tg.metric.split(".").last.to_sym},:alias => '#{metric_alias}', :data => \"#{tg.compile}\"\n"
    count += 1
  end
  return output
end

#urlObject

This returns the url field of the graph after compiling it



100
101
102
103
# File 'lib/ast_node.rb', line 100

def url
  self.compile
  url = "#{properties[:endpoint]}/render/?width=586&height=308&#{properties_to_url}&target=" + URI.escape(targets.map{|i| i.compile}.compact.join("&target="))
end