Class: ScoutApm::SlowItemSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scout_apm/slow_item_set.rb

Constant Summary collapse

DEFAULT_TOTAL =
10
DEFAULT_FAIR =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total = DEFAULT_TOTAL, fair = DEFAULT_FAIR) ⇒ SlowItemSet

Returns a new instance of SlowItemSet.



31
32
33
34
35
# File 'lib/scout_apm/slow_item_set.rb', line 31

def initialize(total=DEFAULT_TOTAL, fair=DEFAULT_FAIR)
  @total = total
  @fair = fair
  @items = []
end

Instance Attribute Details

#fairObject (readonly)

Returns the value of attribute fair.



29
30
31
# File 'lib/scout_apm/slow_item_set.rb', line 29

def fair
  @fair
end

#totalObject (readonly)

Returns the value of attribute total.



28
29
30
# File 'lib/scout_apm/slow_item_set.rb', line 28

def total
  @total
end

Instance Method Details

#<<(item) ⇒ Object



41
42
43
44
45
# File 'lib/scout_apm/slow_item_set.rb', line 41

def <<(item)
  return if attempt_append(item)
  attempt_to_evict
  attempt_append(item)
end

#attempt_append(item) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/scout_apm/slow_item_set.rb', line 51

def attempt_append(item)
  if empty_slot?
    @items.push(item)
    true
  else
    false
  end
end

#attempt_to_evictObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/scout_apm/slow_item_set.rb', line 60

def attempt_to_evict
  return if @items.length == 0

  overrepresented = @items.
    group_by { |item| unique_name_for(item) }.
    to_a.
    sort_by { |(_, items)| items.length }.
    last

  if overrepresented[1].length > fair
    fastest = overrepresented[1].sort_by { |item| item.total_call_time }.first
    @items.delete(fastest)
  end
end

#eachObject



37
38
39
# File 'lib/scout_apm/slow_item_set.rb', line 37

def each
  @items.each { |s| yield s }
end

#empty_slot?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/scout_apm/slow_item_set.rb', line 47

def empty_slot?
  @items.length < total
end

#unique_name_for(item) ⇒ Object

Determine this items’ “hash key”



76
77
78
# File 'lib/scout_apm/slow_item_set.rb', line 76

def unique_name_for(item)
  item.metric_name
end