Skip to content

Commit 43b08a5

Browse files
authored
Merge pull request #703 from splitrb/check-if-alternatives-have-valid-data
Do not throw error if alternativas have data that can lead to negative numbers for probability calculation
2 parents 637304e + cfd61ac commit 43b08a5

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

lib/split/experiment.rb

+9
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,16 @@ def load_from_redis
270270
set_alternatives_and_options(options)
271271
end
272272

273+
def can_calculate_winning_alternatives?
274+
self.alternatives.all? do |alternative|
275+
alternative.participant_count >= 0 &&
276+
(alternative.participant_count >= alternative.completed_count)
277+
end
278+
end
279+
273280
def calc_winning_alternatives
281+
return unless can_calculate_winning_alternatives?
282+
274283
# Cache the winning alternatives so we recalculate them once per the specified interval.
275284
intervals_since_epoch =
276285
Time.now.utc.to_i / Split.configuration.winning_alternative_recalculation_interval

spec/dashboard_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,16 @@ def link(color)
279279

280280
expect(last_response.body).to include("<small>Unknown</small>")
281281
end
282+
283+
it "should be explode with experiments with invalid data" do
284+
red_link.participant_count = 1
285+
red_link.set_completed_count(10)
286+
287+
blue_link.participant_count = 3
288+
blue_link.set_completed_count(2)
289+
290+
get "/"
291+
292+
expect(last_response).to be_ok
293+
end
282294
end

spec/experiment_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,17 @@ def same_but_different_alternative
576576
expect(p_goal1).not_to be_within(0.04).of(p_goal2)
577577
end
578578

579+
it "should not calculate when data is not valid for beta distribution" do
580+
experiment = Split::ExperimentCatalog.find_or_create("scientists", "einstein", "bohr")
581+
582+
experiment.alternatives.each do |alternative|
583+
alternative.participant_count = 9
584+
alternative.set_completed_count(10)
585+
end
586+
587+
expect { experiment.calc_winning_alternatives }.to_not raise_error
588+
end
589+
579590
it "should return nil and not re-calculate probabilities if they have already been calculated today" do
580591
experiment = Split::ExperimentCatalog.find_or_create({ "link_color3" => ["purchase", "refund"] }, "blue", "red", "green")
581592
expect(experiment.calc_winning_alternatives).not_to be nil

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require "split"
1212
require "ostruct"
1313
require "yaml"
14+
require "pry"
1415

1516
Dir["./spec/support/*.rb"].each { |f| require f }
1617

0 commit comments

Comments
 (0)