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, VanitySchema
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_chosen(experiment, identity) ⇒ Object
Indicates which alternative has been picked for this participant.
-
#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_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
- #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
-
#initialize(options) ⇒ ActiveRecordAdapter
constructor
A new instance of ActiveRecordAdapter.
-
#is_experiment_completed?(experiment) ⇒ Boolean
Returns true if experiment completed.
- #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).
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ ActiveRecordAdapter
Returns a new instance of ActiveRecordAdapter.
84 85 86 87 88 89 90 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 84 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 particpant if not already recorded for this experiment. If implicit is false (default), only add conversion is participant previously recorded as participating in this experiment.
234 235 236 237 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 234 def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = 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.
219 220 221 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 219 def ab_add_participant(experiment, alternative, identity) VanityParticipant.retrieve(experiment, identity, true, :seen => alternative) end |
#ab_chosen(experiment, identity) ⇒ Object
Indicates which alternative has been picked for this participant. See #ab_add_participant.
224 225 226 227 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 224 def ab_chosen(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.
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 187 def ab_counts(experiment, alternative) record = VanityExperiment.retrieve(experiment) participants = record.vanity_participants.count(:conditions => {:seen => alternative}) converted = record.vanity_participants.count(:conditions => {:converted => alternative}) conversions = record.vanity_conversions.sum(:conversions, :conditions => {:alternative => alternative}) { :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.
241 242 243 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 241 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.
214 215 216 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 214 def ab_not_showing(experiment, identity) VanityParticipant.retrieve(experiment, identity, true, :shown => nil) end |
#ab_set_outcome(experiment, alternative = 0) ⇒ Object
Sets the outcome of this experiment to a particular alternative.
246 247 248 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 246 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).
202 203 204 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 202 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.
207 208 209 210 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 207 def ab_showing(experiment, identity) participant = VanityParticipant.retrieve(experiment, identity, false) participant && participant.shown end |
#active? ⇒ Boolean
92 93 94 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 92 def active? VanityRecord.connected? end |
#destroy_experiment(experiment) ⇒ Object
Deletes all information about this experiment.
251 252 253 254 255 256 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 251 def destroy_experiment(experiment) if record = VanityExperiment.find_by_experiment_id(experiment.to_s) record.vanity_participants.delete_all record.destroy end end |
#destroy_metric(metric) ⇒ Object
148 149 150 151 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 148 def destroy_metric(metric) record = VanityMetric.find_by_metric_id(metric.to_s) record && record.destroy end |
#disconnect! ⇒ Object
96 97 98 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 96 def disconnect! VanityRecord.connection.disconnect! if active? end |
#flushdb ⇒ Object
104 105 106 107 108 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 104 def flushdb [VanityExperiment, VanityMetric, VanityParticipant, VanityMetricValue, VanityConversion].each do |klass| klass.delete_all end end |
#get_experiment_completed_at(experiment) ⇒ Object
175 176 177 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 175 def get_experiment_completed_at(experiment) VanityExperiment.retrieve(experiment).completed_at end |
#get_experiment_created_at(experiment) ⇒ Object
Return when experiment was created.
166 167 168 169 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 166 def get_experiment_created_at(experiment) record = VanityExperiment.retrieve(experiment) record && record.created_at end |
#get_metric_last_update_at(metric) ⇒ Object
110 111 112 113 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 110 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.
180 181 182 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 180 def is_experiment_completed?(experiment) !!VanityExperiment.retrieve(experiment).completed_at end |
#metric_track(metric, timestamp, identity, values) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 115 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.updated_at = Time.now record.save end |
#metric_values(metric, from, to) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 126 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') + ' IN (?)', dates] 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.all( :select => select, :conditions => conditions, :order => order, :group => group_by ) dates.map do |date| value = values.detect{|v| v.date == date } [(value && value.value) || 0] end end |
#reconnect! ⇒ Object
100 101 102 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 100 def reconnect! VanityRecord.connection.reconnect! end |
#set_experiment_completed_at(experiment, time) ⇒ Object
171 172 173 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 171 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).
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 154 def set_experiment_created_at(experiment, time) begin record = VanityExperiment.find_by_experiment_id(experiment.to_s) || VanityExperiment.new(:experiment_id => experiment.to_s) record.created_at ||= time record.save rescue Exception => e puts "Make sure Vanity migrations have been run, ignoring #{e}" end end |
#to_s ⇒ Object
258 259 260 |
# File 'lib/vanity/adapters/active_record_adapter.rb', line 258 def to_s @options.to_s end |