class Quilt::Performance::Navigation

Attributes

connection[RW]
events[RW]
metadata[RW]
result[RW]
start[RW]
target[RW]
time_to_complete[RW]

Public Class Methods

from_params(params) click to toggle source
# 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
new( start:, time_to_complete:, target:, result:, events: [], metadata: {} ) click to toggle source
# 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 = metadata
end

Public Instance Methods

cache_effectiveness() click to toggle source
# File lib/quilt_rails/performance/navigation.rb, line 89
def cache_effectiveness
  events = events_with_size

  if events.empty? || events.any? { |event| event.metadata.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.metadata.size == 0
    # rubocop:enable
  end
  cached_events.length / events.length
end
events_by_type(target_type) click to toggle source
# 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_size() click to toggle source
# File lib/quilt_rails/performance/navigation.rb, line 60
def events_with_size
  resource_events.select do |event|
    event.has_metadata? && event.metadata.has_size?
  end
end
resource_events() click to toggle source
# 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_usable() click to toggle source
# 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_size() click to toggle source
# File lib/quilt_rails/performance/navigation.rb, line 73
def total_download_size
  events_with_size.reduce(nil) do |total, current|
    current_size = current.metadata.size
    return current_size + total unless total.nil?
    current_size
  end
end
total_duration_by_event_type(type) click to toggle source
# 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