Class: RailsObservatory::EventCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rails_observatory/models/event_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ EventCollection

Returns a new instance of EventCollection.



11
12
13
# File 'lib/rails_observatory/models/event_collection.rb', line 11

def initialize(events)
  @events = events
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



6
7
8
# File 'lib/rails_observatory/models/event_collection.rb', line 6

def events
  @events
end

Instance Method Details

#eachObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_observatory/models/event_collection.rb', line 27

def each
  decorate_events unless @decorated
  iterating_set = @events
  if @without
    iterating_set = iterating_set.reject { _1['name'].in?(@without) }
  end
  if @only
    iterating_set = iterating_set.select { _1['name'].in?(@only) }
  end
  iterating_set.then(&method(:decorate_with_relative_time)).each { yield _1 }
end

#only(*names) ⇒ Object



21
22
23
24
25
# File 'lib/rails_observatory/models/event_collection.rb', line 21

def only(*names)
  copy = self.clone
  copy.instance_exec { @only = names }
  copy
end

#self_time_by_libraryObject



68
69
70
71
72
73
# File 'lib/rails_observatory/models/event_collection.rb', line 68

def self_time_by_library
  each_with_object(Hash.new(0)) do |event, hash|
    library = event['name'].split('.').last
    hash[library] += event['self_time']
  end
end

#to_seriesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_observatory/models/event_collection.rb', line 39

def to_series
  all_events = to_a
  min, max = all_events.minmax_by { _1['depth'] }.pluck('depth')

  category_primer = (min..max).map do |depth|
    {
      x: depth.to_s,
      y: nil,
      event_self_time: 0,
    }
  end

  grouped_events = all_events.group_by { _1['name'].split('.').last }.sort_by { _1.first }
  grouped_events.map do |name, events|
    {
      name: name,
      data: category_primer + events.map do |ev|
        {
          x: ev['depth'].to_s,
          y: [ev['relative_start_at'], ev['relative_end_at']],
          event_self_time: ev['self_time'],
          event_name: ev['name'].split('.').first,
          start_at: ev['start_at'],
        }
      end
    }
  end
end

#without(*names) ⇒ Object



15
16
17
18
19
# File 'lib/rails_observatory/models/event_collection.rb', line 15

def without(*names)
  copy = self.clone
  copy.instance_exec { @without = names }
  copy
end