Class: Vanity::Adapters::ActiveRecordAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- Vanity::Adapters::ActiveRecordAdapter
- Defined in:
- lib/vanity/adapters/active_record_adapter.rb
Overview
ActiveRecord adapter
Defined Under Namespace
Classes: VanityConversion, VanityExperiment, VanityMetric, VanityMetricValue, VanityParticipant, VanityRecord
Instance Method Summary collapse
-
#ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) ⇒ Object
Records a conversion in this experiment for the given alternative.
-
#ab_add_participant(experiment, alternative, identity) ⇒ Object
Records a participant in this experiment for the given alternative.
-
#ab_assigned(experiment, identity) ⇒ Object
Returns the participant’s seen alternative in this experiment, if it exists.
-
#ab_counts(experiment, alternative) ⇒ Object
Returns counts for given A/B experiment and alternative (by index).
-
#ab_get_outcome(experiment) ⇒ Object
Returns the outcome of this experiment (if set), the index of a particular alternative.
-
#ab_not_showing(experiment, identity) ⇒ Object
Cancels previously set association between identity and alternative.
-
#ab_seen(experiment, identity, alternative) ⇒ Object
Determines if a participant already has seen this alternative in this experiment.
-
#ab_set_outcome(experiment, alternative = 0) ⇒ Object
Sets the outcome of this experiment to a particular alternative.
-
#ab_show(experiment, identity, alternative) ⇒ Object
Pick particular alternative (by index) to show to this particular participant (by identity).
-
#ab_showing(experiment, identity) ⇒ Object
Indicates which alternative to show to this participant.
- #active? ⇒ Boolean
-
#destroy_experiment(experiment) ⇒ Object
Deletes all information about this experiment.
- #destroy_metric(metric) ⇒ Object
- #disconnect! ⇒ Object
-
#experiment_persisted?(experiment) ⇒ Boolean
– Experiments –.
- #flushdb ⇒ Object
- #get_experiment_completed_at(experiment) ⇒ Object
-
#get_experiment_created_at(experiment) ⇒ Object
Return when experiment was created.
-
#get_metric_last_update_at(metric) ⇒ Object
– Metrics –.
-
#initialize(options) ⇒ ActiveRecordAdapter
constructor
A new instance of ActiveRecordAdapter.
-
#is_experiment_completed?(experiment) ⇒ Boolean
Returns true if experiment completed.
- #is_experiment_enabled?(experiment) ⇒ Boolean
- #metric_track(metric, timestamp, identity, values) ⇒ Object
- #metric_values(metric, from, to) ⇒ Object
- #reconnect! ⇒ Object
- #set_experiment_completed_at(experiment, time) ⇒ Object
-
#set_experiment_created_at(experiment, time) ⇒ Object
Store when experiment was created (do not write over existing value).
- #set_experiment_enabled(experiment, enabled) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ ActiveRecordAdapter
Returns a new instance of ActiveRecordAdapter.
104 105 106 107 108 109 110 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 104 def initialize() @options = .inject({}) { |h,kv| h[kv.first.to_s] = kv.last ; h } if @options["active_record_adapter"] && (@options["active_record_adapter"] != "default") @options["adapter"] = @options["active_record_adapter"] VanityRecord.establish_connection(@options) end end |
Instance Method Details
#ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) ⇒ Object
Records a conversion in this experiment for the given alternative. Associates a value with the conversion (default to 1). If implicit is true, add participant if not already recorded for this experiment. If implicit is false (default), only add conversion if participant previously recorded as participating in this experiment.
274 275 276 277 278 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 274 def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) participant = VanityParticipant.retrieve(experiment, identity, false) VanityParticipant.retrieve(experiment, identity, implicit, :converted => alternative) VanityExperiment.retrieve(experiment).increment_conversion(alternative, count) end |
#ab_add_participant(experiment, alternative, identity) ⇒ Object
Records a participant in this experiment for the given alternative.
253 254 255 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 253 def ab_add_participant(experiment, alternative, identity) VanityParticipant.retrieve(experiment, identity, true, :seen => alternative) end |
#ab_assigned(experiment, identity) ⇒ Object
Returns the participant’s seen alternative in this experiment, if it exists
264 265 266 267 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 264 def ab_assigned(experiment, identity) participant = VanityParticipant.retrieve(experiment, identity, false) participant && participant.seen end |
#ab_counts(experiment, alternative) ⇒ Object
Returns counts for given A/B experiment and alternative (by index). Returns hash with values for the keys :participants, :converted and :conversions.
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 221 def ab_counts(experiment, alternative) record = VanityExperiment.retrieve(experiment) participants = VanityParticipant.where(:experiment_id => experiment.to_s, :seen => alternative).count converted = VanityParticipant.where(:experiment_id => experiment.to_s, :converted => alternative).count conversions = record.vanity_conversions.where(:alternative => alternative).sum(:conversions) { :participants => participants, :converted => converted, :conversions => conversions } end |
#ab_get_outcome(experiment) ⇒ Object
Returns the outcome of this experiment (if set), the index of a particular alternative.
282 283 284 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 282 def ab_get_outcome(experiment) VanityExperiment.retrieve(experiment).outcome end |
#ab_not_showing(experiment, identity) ⇒ Object
Cancels previously set association between identity and alternative. See #ab_show.
248 249 250 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 248 def ab_not_showing(experiment, identity) VanityParticipant.retrieve(experiment, identity, true, :shown => nil) end |
#ab_seen(experiment, identity, alternative) ⇒ Object
Determines if a participant already has seen this alternative in this experiment.
258 259 260 261 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 258 def ab_seen(experiment, identity, alternative) participant = VanityParticipant.retrieve(experiment, identity, false) participant && participant.seen == alternative.id end |
#ab_set_outcome(experiment, alternative = 0) ⇒ Object
Sets the outcome of this experiment to a particular alternative.
287 288 289 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 287 def ab_set_outcome(experiment, alternative = 0) VanityExperiment.retrieve(experiment).update_attribute(:outcome, alternative) end |
#ab_show(experiment, identity, alternative) ⇒ Object
Pick particular alternative (by index) to show to this particular participant (by identity).
236 237 238 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 236 def ab_show(experiment, identity, alternative) VanityParticipant.retrieve(experiment, identity, true, :shown => alternative) end |
#ab_showing(experiment, identity) ⇒ Object
Indicates which alternative to show to this participant. See #ab_show.
241 242 243 244 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 241 def ab_showing(experiment, identity) participant = VanityParticipant.retrieve(experiment, identity, false) participant && participant.shown end |
#active? ⇒ Boolean
112 113 114 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 112 def active? VanityRecord.connected? && VanityRecord.connection.active? end |
#destroy_experiment(experiment) ⇒ Object
Deletes all information about this experiment.
292 293 294 295 296 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 292 def destroy_experiment(experiment) VanityParticipant.delete_all(:experiment_id => experiment.to_s) record = VanityExperiment.find_by_experiment_id(experiment.to_s) record && record.destroy end |
#destroy_metric(metric) ⇒ Object
166 167 168 169 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 166 def destroy_metric(metric) record = VanityMetric.find_by_metric_id(metric.to_s) record && record.destroy end |
#disconnect! ⇒ Object
116 117 118 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 116 def disconnect! VanityRecord.connection.disconnect! if active? end |
#experiment_persisted?(experiment) ⇒ Boolean
– Experiments –
174 175 176 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 174 def experiment_persisted?(experiment) VanityExperiment.find_by_experiment_id(experiment.to_s).present? end |
#flushdb ⇒ Object
124 125 126 127 128 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 124 def flushdb [VanityExperiment, VanityMetric, VanityParticipant, VanityMetricValue, VanityConversion].each do |klass| klass.delete_all end end |
#get_experiment_completed_at(experiment) ⇒ Object
196 197 198 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 196 def get_experiment_completed_at(experiment) VanityExperiment.retrieve(experiment).completed_at end |
#get_experiment_created_at(experiment) ⇒ Object
Return when experiment was created.
187 188 189 190 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 187 def get_experiment_created_at(experiment) record = VanityExperiment.retrieve(experiment) record && record.created_at end |
#get_metric_last_update_at(metric) ⇒ Object
– Metrics –
133 134 135 136 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 133 def get_metric_last_update_at(metric) record = VanityMetric.find_by_metric_id(metric.to_s) record && record.updated_at end |
#is_experiment_completed?(experiment) ⇒ Boolean
Returns true if experiment completed.
201 202 203 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 201 def is_experiment_completed?(experiment) !!VanityExperiment.retrieve(experiment).completed_at end |
#is_experiment_enabled?(experiment) ⇒ Boolean
209 210 211 212 213 214 215 216 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 209 def is_experiment_enabled?(experiment) record = VanityExperiment.retrieve(experiment) if Vanity.configuration.experiments_start_enabled record.enabled != false else record.enabled == true end end |
#metric_track(metric, timestamp, identity, values) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 138 def metric_track(metric, , identity, values) record = VanityMetric.retrieve(metric) values.each_with_index do |value, index| record.vanity_metric_values.create(:date => .to_date.to_s, :index => index, :value => value) end record.touch_with_grace_period record.save end |
#metric_values(metric, from, to) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 149 def metric_values(metric, from, to) connection = VanityMetric.connection record = VanityMetric.retrieve(metric) dates = (from.to_date..to.to_date).map(&:to_s) conditions = [connection.quote_column_name('date') + ' BETWEEN ? AND ?', from.to_date, to.to_date] order = "#{connection.quote_column_name('date')}" select = "sum(#{connection.quote_column_name('value')}) AS value, #{connection.quote_column_name('date')}" group_by = "#{connection.quote_column_name('date')}" values = record.vanity_metric_values.select(select).where(conditions).group(group_by) dates.map do |date| value = values.detect{|v| v.date == date } [(value && value.value) || 0] end end |
#reconnect! ⇒ Object
120 121 122 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 120 def reconnect! VanityRecord.connection.reconnect! end |
#set_experiment_completed_at(experiment, time) ⇒ Object
192 193 194 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 192 def set_experiment_completed_at(experiment, time) VanityExperiment.retrieve(experiment).update_attribute(:completed_at, time) end |
#set_experiment_created_at(experiment, time) ⇒ Object
Store when experiment was created (do not write over existing value).
179 180 181 182 183 184 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 179 def set_experiment_created_at(experiment, time) record = VanityExperiment.find_by_experiment_id(experiment.to_s) || VanityExperiment.new(:experiment_id => experiment.to_s) record.created_at ||= time record.save end |
#set_experiment_enabled(experiment, enabled) ⇒ Object
205 206 207 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 205 def set_experiment_enabled(experiment, enabled) VanityExperiment.retrieve(experiment).update_attribute(:enabled, enabled) end |
#to_s ⇒ Object
298 299 300 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 298 def to_s @options.to_s end |