Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,15 @@ the experiment name:
ab_finished(:my_metric)
```

You can also create a new metric by instantiating and saving a new Metric object.
You can also create a new metric by instantiating and saving a new Metric
object. A metric groups one or more experiments (`Split::Experiment` objects)
under a name and is persisted to Redis:

```ruby
Split::Metric.new(:my_metric)
Split::Metric.save
signup = Split::ExperimentCatalog.find_or_create("signup_form", "control", "blue")
checkout = Split::ExperimentCatalog.find_or_create("checkout_flow", "control", "one_page")

Split::Metric.new(name: :conversion, experiments: [signup, checkout]).save
```

#### Goals
Expand Down
16 changes: 15 additions & 1 deletion lib/split/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def cleanup_old_versions!(experiment)

def active_experiments
experiments_by_key = keys_without_finished(user.keys).each_with_object({}) do |key, memo|
memo[key] = Metric.possible_experiments(key_without_version(key))
memo[key] = experiments_for(key_without_version(key))
end

names = experiments_by_key.values.flatten.map(&:name).uniq
Expand Down Expand Up @@ -95,5 +95,19 @@ def keys_without_finished(keys)
def key_without_version(key)
key.split(/\:\d(?!\:)/)[0]
end

def experiments_for(name)
if metrics_defined?
Metric.possible_experiments(name)
else
Array(Experiment.find(name))
end
end

def metrics_defined?
return @metrics_defined if defined?(@metrics_defined)
@metrics_defined =
Split.configuration.metrics.any? || Split.redis.exists?(:metrics)
end
end
end
12 changes: 12 additions & 0 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@
expect(Split.redis).to receive(:hmget).with(:experiment_winner, any_args).once.and_call_original
@subject.active_experiments
end

it "resolves experiments directly, without per-key metric lookups, when no metrics are defined" do
expect(Split::Metric).not_to receive(:possible_experiments)
expect(@subject.active_experiments).to eq("active" => "red")
end

it "still honors a metric persisted only in Redis" do
Split::Metric.new(name: :conversion, experiments: [Split::ExperimentCatalog.find("active")]).save

expect(Split::Metric).to receive(:possible_experiments).at_least(:once).and_call_original
expect(@subject.active_experiments).to eq("active" => "red")
end
end

context "allows user to be loaded from adapter" do
Expand Down
Loading