Class: Profiler::Models::TimelineEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/models/timeline_event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, started_at:, finished_at:, payload: {}, category: nil) ⇒ TimelineEvent

Returns a new instance of TimelineEvent.



8
9
10
11
12
13
14
15
16
# File 'lib/profiler/models/timeline_event.rb', line 8

def initialize(name:, started_at:, finished_at:, payload: {}, category: nil)
  @name = name
  @started_at = started_at
  @finished_at = finished_at
  @duration = ((finished_at - started_at) * 1000).round(2) # milliseconds
  @payload = payload
  @category = category
  @children = []
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def category
  @category
end

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def children
  @children
end

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def duration
  @duration
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def finished_at
  @finished_at
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def payload
  @payload
end

#started_atObject (readonly)

Returns the value of attribute started_at.



6
7
8
# File 'lib/profiler/models/timeline_event.rb', line 6

def started_at
  @started_at
end

Instance Method Details

#add_child(event) ⇒ Object



18
19
20
# File 'lib/profiler/models/timeline_event.rb', line 18

def add_child(event)
  @children << event
end

#to_hObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/profiler/models/timeline_event.rb', line 22

def to_h
  h = {
    name: @name,
    started_at: @started_at,
    finished_at: @finished_at,
    duration: @duration,
    payload: @payload,
    children: @children.map(&:to_h)
  }
  h[:category] = @category if @category
  h
end

#to_json(*args) ⇒ Object



35
36
37
# File 'lib/profiler/models/timeline_event.rb', line 35

def to_json(*args)
  to_h.to_json(*args)
end