Class: Trailblazer::Developer::Trace::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/trailblazer/developer/trace/node.rb

Overview

Datastructure representing a trace.

Direct Known Subclasses

Incomplete

Defined Under Namespace

Classes: Incomplete

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#levelObject

Returns the value of attribute level

Returns:

  • (Object)

    the current value of level



15
16
17
# File 'lib/trailblazer/developer/trace/node.rb', line 15

def level
  @level
end

#snapshot_afterObject

Returns the value of attribute snapshot_after

Returns:

  • (Object)

    the current value of snapshot_after



15
16
17
# File 'lib/trailblazer/developer/trace/node.rb', line 15

def snapshot_after
  @snapshot_after
end

#snapshot_beforeObject

Returns the value of attribute snapshot_before

Returns:

  • (Object)

    the current value of snapshot_before



15
16
17
# File 'lib/trailblazer/developer/trace/node.rb', line 15

def snapshot_before
  @snapshot_before
end

#taskObject

Returns the value of attribute task

Returns:

  • (Object)

    the current value of task



15
16
17
# File 'lib/trailblazer/developer/trace/node.rb', line 15

def task
  @task
end

Class Method Details

.node_and_instructions_for(snapshot_before, descendants, level:) ⇒ Object

Called per snapshot_before “process_branch”

  1. Find, for snapshot_before, the matching snapshot_after in the stack

  2. Extract snapshots inbetween those two. These are min. 1 level deeper in!

  3. Run process_siblings for 2.



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
# File 'lib/trailblazer/developer/trace/node.rb', line 60

def self.node_and_instructions_for(snapshot_before, descendants, level:)
  # Find closing snapshot for this branch.
  snapshot_after = descendants.find do |snapshot|
    snapshot.is_a?(Snapshot::After) && snapshot.data[:snapshot_before] == snapshot_before
  end

  if snapshot_after
    snapshot_after_index = descendants.index(snapshot_after)

    instructions =
      if snapshot_after_index == 0 # E.g. before/Start, after/Start
        [
          [level, descendants[1..-1]]
        ]
      else
        nested_instructions = [
          # instruction to go through the remaining, behind this tuple.
          [
            level,
            descendants[(snapshot_after_index + 1)..-1]
          ],
          # instruction to go through all snapshots between this current tuple.
          [
            level + 1,
            descendants[0..snapshot_after_index - 1], # "new descendants"
          ],
        ]
      end

    node = new(level, snapshot_before.task, snapshot_before, snapshot_after)
  else # incomplete
    instructions = [
      [level + 1, descendants]
    ]

    node = Incomplete.new(level, snapshot_before.task, snapshot_before, nil)
  end

  return node, instructions
end

.pop_from_instructions!(instructions) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/trailblazer/developer/trace/node.rb', line 19

def self.pop_from_instructions!(instructions)
  while (level, remaining_snapshots = instructions.pop)
    next if level.nil?
    next if remaining_snapshots.empty?

    return level, remaining_snapshots
  end

  false
end

.process_instructions(instructions) ⇒ Object

def self.BLA(instructions)

instructions.collect do |(level, remaining_snapshots)|
  [
    level,
    remaining_snapshots.collect { |snap| [snap.class, snap.task] }
  ]
end

end



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trailblazer/developer/trace/node.rb', line 39

def self.process_instructions(instructions) # FIXME: mutating argument
  nodes = []

  while (level, remaining_snapshots = pop_from_instructions!(instructions))
    raise unless remaining_snapshots[0].is_a?(Snapshot::Before) # DISCUSS: remove assertion?

    node, new_instructions = node_and_instructions_for(remaining_snapshots[0], remaining_snapshots[1..-1], level: level)
    # pp BLA(new_instructions)

    nodes << node

    instructions += new_instructions
  end

  return nodes
end