Module: RSpecTelemetry::Trace::Viewer::DetailLines

Defined in:
lib/rspec_telemetry/trace/viewer/detail_lines.rb

Class Method Summary collapse

Class Method Details

.children_lines(children) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rspec_telemetry/trace/viewer/detail_lines.rb', line 43

def self.children_lines(children)
  return [] if children.empty?

  total = children.sum { |e| (e.fields["self_duration_ms"] || e.fields["duration_ms"]).to_f }
  header = "ran #{children.size} factor#{children.size == 1 ? "y" : "ies"} (self #{Format.ms(total)}):"
  [""] + [header] + children.map { |event| "  #{Label.plain(event)}" }
end

.event_lines(event) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec_telemetry/trace/viewer/detail_lines.rb', line 51

def self.event_lines(event)
  lines = [Label.plain(event), ""]
  event.fields.each do |key, value|
    next if Document::INFRA_FIELDS.include?(key)

    lines.concat(field_lines(key, value))
  end

  lines
end

.example_lines(action, children, duration) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec_telemetry/trace/viewer/detail_lines.rb', line 26

def self.example_lines(action, children, duration)
  lines = ["EXAMPLE", "desc: #{action.label}"]
  lines << "at: #{action.source}" if action.source
  lines << "status: #{action.status}" if action.status
  took = action.duration_ms || duration
  lines << "took: #{Format.ms(took)}" if took
  lines.concat(exception_lines(action.exception))
  lines.concat(children_lines(children))
  lines
end

.exception_lines(exception) ⇒ Object



37
38
39
40
41
# File 'lib/rspec_telemetry/trace/viewer/detail_lines.rb', line 37

def self.exception_lines(exception)
  return [] if exception.nil?

  ["", "exception: #{exception["class"]}", "  #{exception["message"]}"]
end

.field_lines(key, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec_telemetry/trace/viewer/detail_lines.rb', line 62

def self.field_lines(key, value)
  case value
  when Hash
    ["#{key}:"] + value.map { |k, v| "  #{k}: #{Format.value(v)}" }
  when Array
    ["#{key}:"] + value.map { |v| "  - #{Format.value(v)}" }
  else
    ["#{key}: #{Format.value(value)}"]
  end
end

.for(entry, children: [], duration: nil, width: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec_telemetry/trace/viewer/detail_lines.rb', line 12

def self.for(entry, children: [], duration: nil, width: nil)
  return [] if entry.nil?

  lines = if entry.is_a?(Document::Action)
    example_lines(entry, children, duration)
  else
    event_lines(entry)
  end

  return lines if width.nil?

  lines.flat_map { |line| TuiTui::DisplayText.new(line).wrap(width, indent: "  ").map(&:to_s) }
end