Class: RakeGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/azkaban-rb/visualization.rb

Defined Under Namespace

Classes: DataEdge, DataNode, Edge, Node, TaskEdge, TaskNode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaces = nil) ⇒ RakeGraph

Returns a new instance of RakeGraph.



6
7
8
9
10
11
12
13
14
15
# File 'lib/azkaban-rb/visualization.rb', line 6

def initialize(namespaces = nil)
  @namespaces = namespaces
  @tasks = {}
  Rake.application.tasks.find_all{ |task| (not task.job.nil?)}.each do |task|
    tasks[RakeGraph.task_name(task)] = task if (task.job.read_locks.size + task.job.write_locks.size) > 0
  end
  @nodes = {}
  @edges = []
  construct_graph()
end

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



4
5
6
# File 'lib/azkaban-rb/visualization.rb', line 4

def tasks
  @tasks
end

Class Method Details

.data_name(name) ⇒ Object



23
24
25
26
# File 'lib/azkaban-rb/visualization.rb', line 23

def RakeGraph.data_name(name)
  name = "DATA"+name.gsub(/[^0-9a-z ]/i, '')
  return name
end

.task_name(task) ⇒ Object



17
18
19
20
21
# File 'lib/azkaban-rb/visualization.rb', line 17

def RakeGraph.task_name(task)
  task_name = "TASK#{task}"
  task_name = task_name.gsub(/[^0-9a-z ]/i, '')
  return task_name
end

Instance Method Details

#add_edges(g) ⇒ Object



200
201
202
203
204
# File 'lib/azkaban-rb/visualization.rb', line 200

def add_edges(g)
  @edges.each do |edge|
    (g[edge.source]>>g[edge.dest])[:style => edge.style]
  end
end

#add_nodes(g) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/azkaban-rb/visualization.rb', line 190

def add_nodes(g)
  @nodes.each do |name, node|
    g[name] [:label => @label_block.nil? ? node.label : @label_block.call(node), 
      :shape => @shape_block.nil? ? node.shape : @shape_block.call(node),
      :fillcolor => @fillcolor_block.nil? ? node.fillcolor : @fillcolor_block.call(node),
      :style => :filled, 
      :fontcolor => @fontcolor_block.nil? ? node.fontcolor : @fontcolor_block.call(node)]
  end
end

#construct_graphObject



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
# File 'lib/azkaban-rb/visualization.rb', line 42

def construct_graph()
  # first add all of the task nodes
  @tasks.each do |task_name, task|
    next unless task_in_namespace(task)        
    node = TaskNode.new(task)
    @nodes[node.name] = node
  end
  
  # now add all of the edges and data nodes    
  data_nodes = {}
  @nodes.each do |name, node|
    task = node.task
    # find all prereq tasks
    # task.prerequisites.each do |prereq|
    #   prereq = find_prereq(task, prereq)
    #   next unless @nodes.has_key?(prereq)
    #   @edges << TaskEdge.new(prereq, node.name)
    # end
    # find all data reads
    task.job.read_locks.each do |read_lock|
      data_name = RakeGraph.data_name(read_lock)
      data_nodes[data_name] = DataNode.new(read_lock) unless data_nodes.has_key? data_name
      @edges << DataEdge.new(data_name, node.name)
    end
    # find all data writes
    task.job.write_locks.each do |write_lock|
      data_name = RakeGraph.data_name(write_lock)
      data_nodes[data_name] = DataNode.new(write_lock) unless data_nodes.has_key? data_name
      @edges << DataEdge.new(node.name, data_name)
    end
  end    
  data_nodes.each do |key, value|
    @nodes[key] = value
  end
end

#find_prereq(task, prereq) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/azkaban-rb/visualization.rb', line 33

def find_prereq(task, prereq)    
  scopes = Array.new(task.scope)
  while prereq.start_with? '^'
    scopes.pop
    prereq.slice!(0)
  end
  return RakeGraph.task_name(scopes.join('')+prereq)
end

#set_fillcolor(&block) ⇒ Object



210
211
212
# File 'lib/azkaban-rb/visualization.rb', line 210

def set_fillcolor(&block)
  @fillcolor_block = block
end

#set_fontcolor(&block) ⇒ Object



214
215
216
# File 'lib/azkaban-rb/visualization.rb', line 214

def set_fontcolor(&block)
  @fontcolor_block = block
end

#set_label(&block) ⇒ Object



206
207
208
# File 'lib/azkaban-rb/visualization.rb', line 206

def set_label(&block)
  @label_block = block
end

#set_shape(&block) ⇒ Object



218
219
220
# File 'lib/azkaban-rb/visualization.rb', line 218

def set_shape(&block)
  @shape_block = block
end

#task_in_namespace(task) ⇒ Object



28
29
30
31
# File 'lib/azkaban-rb/visualization.rb', line 28

def task_in_namespace(task)
  return true if @namespaces.nil? or @namespaces.size == 0
  return (task.scope & @namespaces).size > 0
end

#visualize(name, output_file) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/azkaban-rb/visualization.rb', line 182

def visualize(name, output_file)
  g = GraphvizR.new name
  g.graph[:label => name]
  add_nodes(g)
  add_edges(g)
  g.output output_file
end