lib/verdict/experiment.rb in verdict-0.12.0 vs lib/verdict/experiment.rb in verdict-0.13.0
- old
+ new
@@ -50,10 +50,21 @@
@segmenter.instance_eval(&block)
@segmenter.verify!
return self
end
+ # Optional: Together with the "end timestamp", limits the experiment run timeline within
+ # the given time interval. When experiment is not scheduled, subject switch returns nil.
+ # This is useful when the experimenter requires the experiment to run in a strict timeline.
+ def schedule_start_timestamp(timestamp)
+ @schedule_start_timestamp = timestamp
+ end
+
+ def schedule_end_timestamp(timestamp)
+ @schedule_end_timestamp = timestamp
+ end
+
def rollout_percentage(percentage, rollout_group_name = :enabled)
groups(Verdict::Segmenters::RolloutSegmenter) do
group rollout_group_name, percentage
end
end
@@ -170,10 +181,11 @@
def remove_subject_assignment(subject)
@storage.remove_assignment(self, subject)
end
def switch(subject, context = nil)
+ return unless is_scheduled?
assign(subject, context).to_sym
end
def lookup(subject)
@storage.retrieve_assignment(self, subject)
@@ -237,17 +249,31 @@
end
def set_start_timestamp
@storage.store_start_timestamp(self, started_now = Time.now.utc)
started_now
+ rescue NotImplementedError
+ nil
end
def ensure_experiment_has_started
- @started_at ||= @storage.retrieve_start_timestamp(self) || set_start_timestamp
+ @started_at ||= started_at || set_start_timestamp
rescue Verdict::StorageError
@started_at ||= Time.now.utc
end
def nil_assignment(subject)
subject_assignment(subject, nil, nil)
+ end
+
+ private
+
+ def is_scheduled?
+ if @schedule_start_timestamp and @schedule_start_timestamp > Time.now
+ return false
+ end
+ if @schedule_end_timestamp and @schedule_end_timestamp < Time.now
+ return false
+ end
+ return true
end
end