Module: Legion::API::Routes::Workflow::WorkflowHelpers

Defined in:
lib/legion/api/workflow.rb

Instance Method Summary collapse

Instance Method Details

#build_relationship_graph(chain_id: nil, extension: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/api/workflow.rb', line 20

def build_relationship_graph(chain_id: nil, extension: nil)
  raw = Legion::Graph::Builder.build(chain_id: chain_id)

  nodes = raw[:nodes].map do |id, meta|
    ext = id.to_s.split('.').first
    { id: id, label: meta[:label], type: meta[:type], extension: ext }
  end

  edges = raw[:edges].map do |edge|
    { source: edge[:from], target: edge[:to], label: edge[:label] }
  end

  if extension
    node_ids = nodes.select { |n| n[:extension] == extension }.map { |n| n[:id] }
    nodes = nodes.select { |n| node_ids.include?(n[:id]) }
    edges = edges.select { |e| node_ids.include?(e[:source]) || node_ids.include?(e[:target]) }
  end

  { nodes: nodes, edges: edges }
rescue StandardError => e
  Legion::Logging.warn "Workflow#build_relationship_graph failed: #{e.message}" if defined?(Legion::Logging)
  { nodes: [], edges: [] }
end