Class: RSpecTelemetry::Trace::Viewer::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_telemetry/trace/viewer/document.rb

Overview

Incrementally folds an NDJSON trace into examples and their child events.

Defined Under Namespace

Classes: Action, Event

Constant Summary collapse

FAILED_STATUSES =

The single source of truth for which statuses count as failures.

%w[failed error].freeze
INFRA_FIELDS =

Hidden from generic labels/details; the original fields remain on each event.

%w[type seq op action wall_ms t timestamp monotonic_time pid thread_id].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 39

def initialize
  @entries = []
  @actions_by_seq = {}
  @action_by_example = {}
  @current_action = nil
  @seq = -1
  @t0 = nil
  @started = false
  @ended = false
  @status = nil
end

Instance Attribute Details

#end_wall_msObject (readonly)

Returns the value of attribute end_wall_ms.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def end_wall_ms
  @end_wall_ms
end

#entriesObject (readonly)

Returns the value of attribute entries.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def entries
  @entries
end

#example_countObject (readonly)

Returns the value of attribute example_count.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def example_count
  @example_count
end

#failed_actionObject (readonly)

Returns the value of attribute failed_action.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def failed_action
  @failed_action
end

#failure_countObject (readonly)

Returns the value of attribute failure_count.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def failure_count
  @failure_count
end

#levelObject (readonly)

Returns the value of attribute level.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def level
  @level
end

#metadataObject (readonly)

Returns the value of attribute metadata.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def 
  @metadata
end

#pending_countObject (readonly)

Returns the value of attribute pending_count.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def pending_count
  @pending_count
end

#versionObject (readonly)

Returns the value of attribute version.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def version
  @version
end

#wall_timeObject (readonly)

Returns the value of attribute wall_time.



22
23
24
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 22

def wall_time
  @wall_time
end

Class Method Details

.from_lines(lines) ⇒ Object



35
36
37
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 35

def self.from_lines(lines)
  lines.each_with_object(new) { |line, doc| doc.apply(line) }
end

Instance Method Details

#action(seq) ⇒ Object



81
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 81

def action(seq) = @actions_by_seq[seq]

#actionsObject



79
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 79

def actions = @entries.grep(Action)

#apply(line) ⇒ Object

Unknown future event types are kept as generic events instead of dropped.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 52

def apply(line)
  parsed = parse(line)
  return self unless parsed.is_a?(Hash)

  @started = true
  set_origin(parsed)

  case parsed["type"]
  when nil
    self
  when "example.started"
    open_action(parsed)
  when "example.finished"
    close_action(parsed)
  when "factory_bot.run_factory"
    record("factory", parsed)
  when "suite.finished"
    finish(parsed)
  else
    record(parsed["type"], parsed)
  end

  self
end

#eventsObject



77
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 77

def events = @entries.grep(Event)

#events_for(action_seq) ⇒ Object



83
84
85
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 83

def events_for(action_seq)
  @entries.select { |entry| entry.is_a?(Event) && entry.action == action_seq }
end

#pending?Boolean

Returns:

  • (Boolean)


87
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 87

def pending? = @started && !@ended

#statusObject



89
90
91
92
93
94
# File 'lib/rspec_telemetry/trace/viewer/document.rb', line 89

def status
  return @status if @status
  return "ok" if @ended

  "pending"
end