Class: Qtrix::Matrix::QueuePrioritizer
- Inherits:
-
Object
- Object
- Qtrix::Matrix::QueuePrioritizer
- Defined in:
- lib/qtrix/matrix/queue_prioritizer.rb
Overview
Maintains current prioritization of queues based on the state of all rows in the matrix.
Instance Attribute Summary collapse
-
#all_entries ⇒ Object
readonly
Returns the value of attribute all_entries.
-
#desired_distribution ⇒ Object
readonly
Returns the value of attribute desired_distribution.
-
#heads ⇒ Object
readonly
Returns the value of attribute heads.
Instance Method Summary collapse
- #by_priority_of_tuple(context = {}) ⇒ Object
- #current_priority_of(queue) ⇒ Object
- #current_priority_queue ⇒ Object
- #entries_for(queue) ⇒ Object
- #has_appeared_at_head_of_row?(queue_name) ⇒ Boolean
-
#initialize(desired_distribution, heads, all_entries) ⇒ QueuePrioritizer
constructor
A new instance of QueuePrioritizer.
- #normal_priority_for(queue) ⇒ Object
- #queue_priority_tuples_from(dist) ⇒ Object
- #starting_priority_for(queue) ⇒ Object
- #sum_of(entries) ⇒ Object
- #to_simple_queues_from(prioritized_queue) ⇒ Object
Constructor Details
#initialize(desired_distribution, heads, all_entries) ⇒ QueuePrioritizer
Returns a new instance of QueuePrioritizer.
13 14 15 16 17 18 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 13 def initialize(desired_distribution, heads, all_entries) @desired_distribution = desired_distribution @heads = heads @all_entries = all_entries @new_head_picked = false end |
Instance Attribute Details
#all_entries ⇒ Object (readonly)
Returns the value of attribute all_entries.
11 12 13 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 11 def all_entries @all_entries end |
#desired_distribution ⇒ Object (readonly)
Returns the value of attribute desired_distribution.
11 12 13 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 11 def desired_distribution @desired_distribution end |
#heads ⇒ Object (readonly)
Returns the value of attribute heads.
11 12 13 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 11 def heads @heads end |
Instance Method Details
#by_priority_of_tuple(context = {}) ⇒ Object
32 33 34 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 32 def by_priority_of_tuple(context={}) lambda {|i, j| j[1] <=> i[1]} end |
#current_priority_of(queue) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 40 def current_priority_of(queue) if has_appeared_at_head_of_row?(queue.name) || @new_head_picked normal_priority_for queue else @new_head_picked = true starting_priority_for queue end end |
#current_priority_queue ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 20 def current_priority_queue # Not a true priority queue, since we are sorting after all elements # are inserted queue = queue_priority_tuples_from desired_distribution prioritized_queue = queue.sort &by_priority_of_tuple to_simple_queues_from prioritized_queue end |
#entries_for(queue) ⇒ Object
65 66 67 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 65 def entries_for(queue) all_entries.select{|entry| entry.queue == queue.name} end |
#has_appeared_at_head_of_row?(queue_name) ⇒ Boolean
49 50 51 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 49 def has_appeared_at_head_of_row?(queue_name) heads.include?(queue_name) end |
#normal_priority_for(queue) ⇒ Object
53 54 55 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 53 def normal_priority_for(queue) queue.resource_percentage / (1 + sum_of(entries_for(queue))) end |
#queue_priority_tuples_from(dist) ⇒ Object
28 29 30 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 28 def queue_priority_tuples_from(dist) dist.map{|queue| [queue, current_priority_of(queue)]} end |
#starting_priority_for(queue) ⇒ Object
57 58 59 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 57 def starting_priority_for(queue) queue.resource_percentage * 1000 end |
#sum_of(entries) ⇒ Object
61 62 63 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 61 def sum_of(entries) entries.inject(0) {|memo, e| memo += e.value} end |
#to_simple_queues_from(prioritized_queue) ⇒ Object
36 37 38 |
# File 'lib/qtrix/matrix/queue_prioritizer.rb', line 36 def to_simple_queues_from(prioritized_queue) prioritized_queue.map {|tuple| tuple[0]} end |