Class: Quilt::Performance::Navigation

Inherits:
Object
  • Object
show all
Defined in:
lib/quilt_rails/performance/navigation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start:, time_to_complete:, target:, result:, events: [], metadata: {}) ⇒ Navigation

Returns a new instance of Navigation.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/quilt_rails/performance/navigation.rb', line 32

def initialize(
  start:,
  time_to_complete:,
  target:,
  result:,
  events: [],
  metadata: {}
)
  @start = start
  @time_to_complete = time_to_complete
  @target = target
  @result = result
  @events = events
  @metadata = 
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



12
13
14
# File 'lib/quilt_rails/performance/navigation.rb', line 12

def connection
  @connection
end

#eventsObject

Returns the value of attribute events.



9
10
11
# File 'lib/quilt_rails/performance/navigation.rb', line 9

def events
  @events
end

#metadataObject

Returns the value of attribute metadata.



11
12
13
# File 'lib/quilt_rails/performance/navigation.rb', line 11

def 
  @metadata
end

#resultObject

Returns the value of attribute result.



10
11
12
# File 'lib/quilt_rails/performance/navigation.rb', line 10

def result
  @result
end

#startObject

Returns the value of attribute start.



6
7
8
# File 'lib/quilt_rails/performance/navigation.rb', line 6

def start
  @start
end

#targetObject

Returns the value of attribute target.



8
9
10
# File 'lib/quilt_rails/performance/navigation.rb', line 8

def target
  @target
end

#time_to_completeObject

Returns the value of attribute time_to_complete.



7
8
9
# File 'lib/quilt_rails/performance/navigation.rb', line 7

def time_to_complete
  @time_to_complete
end

Class Method Details

.from_params(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/quilt_rails/performance/navigation.rb', line 14

def self.from_params(params)
  params.transform_keys! { |key| key.underscore.to_sym }
  params.require(:details)

  attributes = {
    start: params[:details][:start],
    time_to_complete: params[:details][:duration],
    target: params[:details][:target],
    result: params[:details][:result],
    events: (params[:details][:events] || []).map do |event|
      Event.from_params(event)
    end,
    metadata: NavigationMetadata.from_params(params[:metadata] || {}),
  }

  Navigation.new(**attributes)
end

Instance Method Details

#cache_effectivenessObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/quilt_rails/performance/navigation.rb', line 89

def cache_effectiveness
  events = events_with_size

  if events.empty? || events.any? { |event| event..size.nil? }
    return nil
  end

  cached_events = events.select do |event|
    # this is not actually checking the size of an array,
    # there is no EventMetadata#any? method, so this check is being tripped erroneously.
    # rubocop:disable Style/ZeroLengthPredicate
    event..size == 0
    # rubocop:enable
  end
  cached_events.length / events.length
end

#events_by_type(target_type) ⇒ Object



48
49
50
51
52
# File 'lib/quilt_rails/performance/navigation.rb', line 48

def events_by_type(target_type)
  events.select do |event|
    event.type == target_type
  end
end

#events_with_sizeObject



60
61
62
63
64
# File 'lib/quilt_rails/performance/navigation.rb', line 60

def events_with_size
  resource_events.select do |event|
    event.has_metadata? && event..has_size?
  end
end

#resource_eventsObject



54
55
56
57
58
# File 'lib/quilt_rails/performance/navigation.rb', line 54

def resource_events
  style_events = events_by_type(Event::TYPE[:style_download])
  script_events = events_by_type(Event::TYPE[:script_download])
  style_events.concat(script_events)
end

#time_to_usableObject



66
67
68
69
70
71
# File 'lib/quilt_rails/performance/navigation.rb', line 66

def time_to_usable
  usable_event = events_by_type(Event::TYPE[:usable]).first
  return usable_event.start - start unless usable_event.nil?

  time_to_complete
end

#total_download_sizeObject



73
74
75
76
77
78
79
# File 'lib/quilt_rails/performance/navigation.rb', line 73

def total_download_size
  events_with_size.reduce(nil) do |total, current|
    current_size = current..size
    return current_size + total unless total.nil?
    current_size
  end
end

#total_duration_by_event_type(type) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/quilt_rails/performance/navigation.rb', line 81

def total_duration_by_event_type(type)
  events = events_by_type(type)

  events.reduce(0) do |total, current_event|
    total + (current_event.duration || 0)
  end
end