Module: Legion::Graph::Builder

Defined in:
lib/legion/graph/builder.rb

Class Method Summary collapse

Class Method Details

.build(chain_id: nil, worker_id: nil, limit: 100) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/graph/builder.rb', line 7

def build(chain_id: nil, worker_id: nil, limit: 100) # rubocop:disable Lint/UnusedMethodArgument
  Legion::Logging.debug "[Graph::Builder] build chain_id=#{chain_id} limit=#{limit}" if defined?(Legion::Logging)
  return { nodes: {}, edges: [] } unless db_available?

  ds = Legion::Data.connection[:relationships].limit(limit)
  ds = ds.where(chain_id: chain_id) if chain_id

  nodes = {}
  edges = []

  ds.each do |rel|
    trigger = rel[:trigger] || "node_#{rel[:id]}_from"
    action  = rel[:action] || "node_#{rel[:id]}_to"

    nodes[trigger] ||= { label: trigger, type: 'trigger' }
    nodes[action]  ||= { label: action, type: 'action' }
    edges << {
      from:     trigger,
      to:       action,
      label:    rel[:runner_function] || '',
      chain_id: rel[:chain_id]
    }
  end

  Legion::Logging.debug "[Graph::Builder] built nodes=#{nodes.size} edges=#{edges.size}" if defined?(Legion::Logging)
  { nodes: nodes, edges: edges }
end