Skip to content

Commit

Permalink
Merge pull request #703 from splitrb/check-if-alternatives-have-valid…
Browse files Browse the repository at this point in the history
…-data

Do not throw error if alternativas have data that can lead to negative numbers for probability calculation
  • Loading branch information
andrehjr authored Jan 22, 2023
2 parents 637304e + cfd61ac commit 43b08a5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/split/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,16 @@ def load_from_redis
set_alternatives_and_options(options)
end

def can_calculate_winning_alternatives?
self.alternatives.all? do |alternative|
alternative.participant_count >= 0 &&
(alternative.participant_count >= alternative.completed_count)
end
end

def calc_winning_alternatives
return unless can_calculate_winning_alternatives?

# Cache the winning alternatives so we recalculate them once per the specified interval.
intervals_since_epoch =
Time.now.utc.to_i / Split.configuration.winning_alternative_recalculation_interval
Expand Down
12 changes: 12 additions & 0 deletions spec/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,16 @@ def link(color)

expect(last_response.body).to include("<small>Unknown</small>")
end

it "should be explode with experiments with invalid data" do
red_link.participant_count = 1
red_link.set_completed_count(10)

blue_link.participant_count = 3
blue_link.set_completed_count(2)

get "/"

expect(last_response).to be_ok
end
end
11 changes: 11 additions & 0 deletions spec/experiment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,17 @@ def same_but_different_alternative
expect(p_goal1).not_to be_within(0.04).of(p_goal2)
end

it "should not calculate when data is not valid for beta distribution" do
experiment = Split::ExperimentCatalog.find_or_create("scientists", "einstein", "bohr")

experiment.alternatives.each do |alternative|
alternative.participant_count = 9
alternative.set_completed_count(10)
end

expect { experiment.calc_winning_alternatives }.to_not raise_error
end

it "should return nil and not re-calculate probabilities if they have already been calculated today" do
experiment = Split::ExperimentCatalog.find_or_create({ "link_color3" => ["purchase", "refund"] }, "blue", "red", "green")
expect(experiment.calc_winning_alternatives).not_to be nil
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "split"
require "ostruct"
require "yaml"
require "pry"

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

Expand Down

0 comments on commit 43b08a5

Please sign in to comment.