Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring default priority value at midpoint of range #39

Merged
merged 10 commits into from
Apr 29, 2024
36 changes: 34 additions & 2 deletions lib/delayed/priority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def names=(names)

@ranges = nil
@alerts = nil
@names = names&.sort_by(&:last)&.to_h&.transform_values { |v| new(v) }
@names = names_to_default_priority(names)
end

def alerts=(alerts)
Expand All @@ -82,6 +82,15 @@ def alerts=(alerts)
@alerts = alerts&.sort_by { |k, _| names.keys.index(k) }&.to_h
end

def assign_at_midpoint?
@assign_at_midpoint || false
end

def assign_at_midpoint=(assign_at_midpoint=false)
tkoar marked this conversation as resolved.
Show resolved Hide resolved
@assign_at_midpoint = assign_at_midpoint
@assign_at_midpoint
tkoar marked this conversation as resolved.
Show resolved Hide resolved
end

def ranges
@ranges ||= names.zip(names.except(names.keys.first)).each_with_object({}) do |((name, lower), (_, upper)), obj|
obj[name] = (lower...(upper || Float::INFINITY))
Expand All @@ -91,7 +100,7 @@ def ranges
private

def default_names
@default_names ||= DEFAULT_NAMES.transform_values { |v| new(v) }
@default_names ||= names_to_default_priority(DEFAULT_NAMES)
end

def default_alerts
Expand All @@ -109,6 +118,29 @@ def method_missing(method_name, *args)
super
end
end

def names_to_default_priority(names)
return unless names

names_to_priority = {}

if assign_at_midpoint?
sorted_priorities_by_name = names&.sort_by(&:last)
sorted_priorities_by_name.each.with_index do |(name, priority_value), index|
if assign_at_midpoint?
(_, next_priority_value) = sorted_priorities_by_name[index+1] || [nil, priority_value + 10]
midpoint = priority_value + ((next_priority_value - priority_value).to_f/2.0).ceil
names_to_priority[name] = new(midpoint)
else
names_to_priority[name] = new(priority_value)
end
end
else
names_to_priority = names.transform_values { |v| new(v) }
end

names_to_priority.sort_by(&:last).to_h
end
end

attr_reader :value
Expand Down
23 changes: 23 additions & 0 deletions spec/delayed/priority_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
RSpec.describe Delayed::Priority do
let(:custom_names) { nil }
let(:custom_alerts) { nil }
let(:assign_at_midpoint) { nil }

around do |example|
described_class.assign_at_midpoint = assign_at_midpoint
described_class.names = custom_names
described_class.alerts = custom_alerts
example.run
Expand Down Expand Up @@ -43,6 +45,17 @@
expect(described_class.reporting).to eq 30
end

context 'when assign_at_midpoint is set to true' do
let(:assign_at_midpoint) { true }

it 'returns the midpoint value' do
expect(described_class.interactive).to eq 5
expect(described_class.user_visible).to eq 15
expect(described_class.eventual).to eq 25
expect(described_class.reporting).to eq 35
end
tkoar marked this conversation as resolved.
Show resolved Hide resolved
end

context 'when customized to high, medium, low' do
let(:custom_names) { { high: 0, medium: 100, low: 500 } }

Expand Down Expand Up @@ -81,6 +94,16 @@
)
end
end

context 'when assign_at_midpoint is set to true' do
let(:assign_at_midpoint) { true }

it 'returns the midpoint value' do
expect(described_class.high).to eq 50
expect(described_class.medium).to eq 300
expect(described_class.low).to eq 505
end
end
end
end

Expand Down
Loading