Class: TrailGuide::Participant
- Inherits:
-
Object
- Object
- TrailGuide::Participant
- Defined in:
- lib/trail_guide/participant.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #active_experiments(include_control = true) ⇒ Object
- #adapter ⇒ Object
- #calibrating_experiments ⇒ Object
- #cleanup_inactive_experiments! ⇒ Object
- #converted!(variant, checkpoint = nil, reset: false) ⇒ Object
- #converted?(experiment, checkpoint = nil) ⇒ Boolean
- #exit!(experiment) ⇒ Object
-
#initialize(context, adapter: nil) ⇒ Participant
constructor
A new instance of Participant.
- #participating!(variant) ⇒ Object
- #participating?(experiment, include_control = true) ⇒ Boolean
- #participating_in_active_experiments?(include_control = true) ⇒ Boolean
- #variant(experiment) ⇒ Object
Constructor Details
#initialize(context, adapter: nil) ⇒ Participant
Returns a new instance of Participant.
6 7 8 9 10 11 |
# File 'lib/trail_guide/participant.rb', line 6 def initialize(context, adapter: nil) @context = context @adapter = adapter.new(context) if adapter.present? cleanup_inactive_experiments! if TrailGuide.configuration.cleanup_participant_experiments == true end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/trail_guide/participant.rb', line 3 def context @context end |
Instance Method Details
#active_experiments(include_control = true) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/trail_guide/participant.rb', line 125 def active_experiments(include_control=true) return false if adapter.keys.empty? inactive = [] active = adapter.keys.map { |key| key.to_s.split(":").first.to_sym }.uniq.map do |key| experiment = TrailGuide.catalog.find(key) next unless experiment next unless experiment.configuration.sticky_assignment? if !experiment.started? && !experiment. inactive << key next else next unless !experiment.combined? && experiment.running? && participating?(experiment, include_control) [ experiment.experiment_name, adapter[experiment.storage_key] ] end end.compact.to_h if TrailGuide.configuration.cleanup_participant_experiments == :inline && !inactive.empty? adapter.keys.select do |key| inactive.include?(key.to_s.split(":").first.to_sym) end.each { |key| adapter.delete(key) } end return false if active.empty? return active end |
#adapter ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/trail_guide/participant.rb', line 13 def adapter # TODO move this case selection to Adapters::Participant (like Algorithms)? @adapter ||= begin config_adapter = TrailGuide.configuration.adapter case config_adapter when :cookie config_adapter = TrailGuide::Adapters::Participants::Cookie when :session config_adapter = TrailGuide::Adapters::Participants::Session when :redis config_adapter = TrailGuide::Adapters::Participants::Redis when :anonymous config_adapter = TrailGuide::Adapters::Participants::Anonymous when :multi config_adapter = TrailGuide::Adapters::Participants::Multi else config_adapter = config_adapter.constantize if config_adapter.is_a?(String) end config_adapter.new(context) rescue => e [TrailGuide.configuration.on_adapter_failover].flatten.compact.each do |callback| callback.call(config_adapter, e) end TrailGuide::Adapters::Participants::Anonymous.new(context) end end |
#calibrating_experiments ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/trail_guide/participant.rb', line 153 def return false if adapter.keys.empty? = adapter.keys.map { |key| key.to_s.split(":").first.to_sym }.uniq.map do |key| experiment = TrailGuide.catalog.find(key) next unless experiment && experiment. [ experiment.experiment_name, adapter[experiment.storage_key] ] end.compact.to_h return false if .empty? return end |
#cleanup_inactive_experiments! ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/trail_guide/participant.rb', line 176 def cleanup_inactive_experiments! return false if adapter.keys.empty? adapter.keys.each do |key| experiment_name = key.to_s.split(":").first.to_sym experiment = TrailGuide.catalog.find(experiment_name) if !experiment || (!experiment.started? && !experiment.) adapter.delete(key) end end return true end |
#converted!(variant, checkpoint = nil, reset: false) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/trail_guide/participant.rb', line 94 def converted!(variant, checkpoint=nil, reset: false) if checkpoint.nil? storage_key = "#{variant.experiment.storage_key}:converted" else storage_key = variant.experiment.goals.find { |g| g == checkpoint }.storage_key end if reset adapter.delete(variant.experiment.storage_key) adapter.delete(variant.storage_key) adapter.delete(storage_key) variant.experiment.goals.each do |goal| adapter.delete(goal.storage_key) end else adapter[storage_key] = Time.now.to_i end end |
#converted?(experiment, checkpoint = nil) ⇒ Boolean
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/trail_guide/participant.rb', line 60 def converted?(experiment, checkpoint=nil) variant = variant(experiment) return false unless experiment.started? || (experiment. && variant.try(:control?)) if experiment.goals.empty? raise InvalidGoalError, "You provided the checkpoint `#{checkpoint}` but the experiment `#{experiment.experiment_name}` does not have any goals defined." unless checkpoint.nil? storage_key = "#{experiment.storage_key}:converted" return false unless adapter.key?(storage_key) converted_at = Time.at(adapter[storage_key].to_i) (experiment. && variant.try(:control?)) || converted_at >= experiment.started_at elsif !checkpoint.nil? goal = experiment.goals.find { |g| g == checkpoint } raise InvalidGoalError, "Invalid goal checkpoint `#{checkpoint}` for experiment `#{experiment.experiment_name}`." if goal.nil? return false unless adapter.key?(goal.storage_key) converted_at = Time.at(adapter[goal.storage_key].to_i) (experiment. && variant.try(:control?)) || converted_at >= experiment.started_at else experiment.goals.each do |goal| next unless adapter.key?(goal.storage_key) converted_at = Time.at(adapter[goal.storage_key].to_i) return true if (experiment. && variant.try(:control?)) || converted_at >= experiment.started_at end return false end end |
#exit!(experiment) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/trail_guide/participant.rb', line 113 def exit!(experiment) chosen = variant(experiment) return true if chosen.nil? adapter.delete(experiment.storage_key) adapter.delete(chosen.storage_key) adapter.delete("#{experiment.storage_key}:converted") experiment.goals.each do |goal| adapter.delete(goal.storage_key) end return true end |
#participating!(variant) ⇒ Object
89 90 91 92 |
# File 'lib/trail_guide/participant.rb', line 89 def participating!(variant) adapter[variant.experiment.storage_key] = variant.name adapter[variant.storage_key] = Time.now.to_i end |
#participating?(experiment, include_control = true) ⇒ Boolean
53 54 55 56 57 58 |
# File 'lib/trail_guide/participant.rb', line 53 def participating?(experiment, include_control=true) var = variant(experiment) return false if var.nil? return false if !include_control && var.control? return true end |
#participating_in_active_experiments?(include_control = true) ⇒ Boolean
166 167 168 169 170 171 172 173 174 |
# File 'lib/trail_guide/participant.rb', line 166 def participating_in_active_experiments?(include_control=true) return false if adapter.keys.empty? adapter.keys.any? do |key| experiment_name = key.to_s.split(":").first.to_sym experiment = TrailGuide.catalog.find(experiment_name) experiment && experiment.configuration.sticky_assignment? && !experiment.combined? && experiment.running? && participating?(experiment, include_control) end end |
#variant(experiment) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/trail_guide/participant.rb', line 40 def variant(experiment) return nil unless experiment. || experiment.started? # TODO more efficient to stop checking if keys exist, and just return if the value is blank?? return nil unless adapter.key?(experiment.storage_key) varname = adapter[experiment.storage_key] variant = experiment.variants.find { |var| var == varname } return nil unless variant && adapter.key?(variant.storage_key) chosen_at = Time.at(adapter[variant.storage_key].to_i) started_at = experiment.started_at return variant if (variant.control? && experiment.) || (started_at && chosen_at >= started_at) end |