Class: Vanity::Adapters::ActiveRecordAdapter

Inherits:
AbstractAdapter show all
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)

Constructor Details

- (ActiveRecordAdapter) initialize(options)

Returns a new instance of ActiveRecordAdapter



107
108
109
110
111
112
113
# File 'lib/vanity/adapters/active_record_adapter.rb', line 107

def initialize(options)
  @options = 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

- (Object) ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false)

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.



264
265
266
267
268
# File 'lib/vanity/adapters/active_record_adapter.rb', line 264

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

- (Object) ab_add_participant(experiment, alternative, identity)

Records a participant in this experiment for the given alternative.



243
244
245
# File 'lib/vanity/adapters/active_record_adapter.rb', line 243

def ab_add_participant(experiment, alternative, identity)
  VanityParticipant.retrieve(experiment, identity, true, :seen => alternative)
end

- (Object) ab_assigned(experiment, identity)

Returns the participant's seen alternative in this experiment, if it exists



254
255
256
257
# File 'lib/vanity/adapters/active_record_adapter.rb', line 254

def ab_assigned(experiment, identity)
  participant = VanityParticipant.retrieve(experiment, identity, false)
  participant && participant.seen
end

- (Object) ab_counts(experiment, alternative)

Returns counts for given A/B experiment and alternative (by index). Returns hash with values for the keys :participants, :converted and :conversions.



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/vanity/adapters/active_record_adapter.rb', line 211

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

- (Object) ab_get_outcome(experiment)

Returns the outcome of this experiment (if set), the index of a particular alternative.



272
273
274
# File 'lib/vanity/adapters/active_record_adapter.rb', line 272

def ab_get_outcome(experiment)
  VanityExperiment.retrieve(experiment).outcome
end

- (Object) ab_not_showing(experiment, identity)

Cancels previously set association between identity and alternative. See #ab_show.



238
239
240
# File 'lib/vanity/adapters/active_record_adapter.rb', line 238

def ab_not_showing(experiment, identity)
  VanityParticipant.retrieve(experiment, identity, true, :shown => nil)
end

- (Object) ab_seen(experiment, identity, alternative)

Determines if a participant already has seen this alternative in this experiment.



248
249
250
251
# File 'lib/vanity/adapters/active_record_adapter.rb', line 248

def ab_seen(experiment, identity, alternative)
  participant = VanityParticipant.retrieve(experiment, identity, false)
  participant && participant.seen == alternative.id
end

- (Object) ab_set_outcome(experiment, alternative = 0)

Sets the outcome of this experiment to a particular alternative.



277
278
279
# File 'lib/vanity/adapters/active_record_adapter.rb', line 277

def ab_set_outcome(experiment, alternative = 0)
  VanityExperiment.retrieve(experiment).update_attribute(:outcome, alternative)
end

- (Object) ab_show(experiment, identity, alternative)

Pick particular alternative (by index) to show to this particular participant (by identity).



226
227
228
# File 'lib/vanity/adapters/active_record_adapter.rb', line 226

def ab_show(experiment, identity, alternative)
  VanityParticipant.retrieve(experiment, identity, true, :shown => alternative)
end

- (Object) ab_showing(experiment, identity)

Indicates which alternative to show to this participant. See #ab_show.



231
232
233
234
# File 'lib/vanity/adapters/active_record_adapter.rb', line 231

def ab_showing(experiment, identity)
  participant = VanityParticipant.retrieve(experiment, identity, false)
  participant && participant.shown
end

- (Boolean) active?

Returns:

  • (Boolean)


115
116
117
# File 'lib/vanity/adapters/active_record_adapter.rb', line 115

def active?
  VanityRecord.connected?
end

- (Object) destroy_experiment(experiment)

Deletes all information about this experiment.



282
283
284
285
286
# File 'lib/vanity/adapters/active_record_adapter.rb', line 282

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

- (Object) destroy_metric(metric)



169
170
171
172
# File 'lib/vanity/adapters/active_record_adapter.rb', line 169

def destroy_metric(metric)
  record = VanityMetric.find_by_metric_id(metric.to_s)
  record && record.destroy
end

- (Object) disconnect!



119
120
121
# File 'lib/vanity/adapters/active_record_adapter.rb', line 119

def disconnect!
  VanityRecord.connection.disconnect! if active?
end

- (Boolean) experiment_persisted?(experiment)

– Experiments –

Returns:

  • (Boolean)


177
178
179
# File 'lib/vanity/adapters/active_record_adapter.rb', line 177

def experiment_persisted?(experiment)
  VanityExperiment.find_by_experiment_id(experiment.to_s).present?
end

- (Object) flushdb



127
128
129
130
131
# File 'lib/vanity/adapters/active_record_adapter.rb', line 127

def flushdb
  [VanityExperiment, VanityMetric, VanityParticipant, VanityMetricValue, VanityConversion].each do |klass|
    klass.delete_all
  end
end

- (Object) get_experiment_completed_at(experiment)



199
200
201
# File 'lib/vanity/adapters/active_record_adapter.rb', line 199

def get_experiment_completed_at(experiment)
  VanityExperiment.retrieve(experiment).completed_at
end

- (Object) get_experiment_created_at(experiment)

Return when experiment was created.



190
191
192
193
# File 'lib/vanity/adapters/active_record_adapter.rb', line 190

def get_experiment_created_at(experiment)
  record = VanityExperiment.retrieve(experiment)
  record && record.created_at
end

- (Object) get_metric_last_update_at(metric)

– Metrics –



136
137
138
139
# File 'lib/vanity/adapters/active_record_adapter.rb', line 136

def get_metric_last_update_at(metric)
  record = VanityMetric.find_by_metric_id(metric.to_s)
  record && record.updated_at
end

- (Boolean) is_experiment_completed?(experiment)

Returns true if experiment completed.

Returns:

  • (Boolean)


204
205
206
# File 'lib/vanity/adapters/active_record_adapter.rb', line 204

def is_experiment_completed?(experiment)
  !!VanityExperiment.retrieve(experiment).completed_at
end

- (Object) metric_track(metric, timestamp, identity, values)



141
142
143
144
145
146
147
148
149
150
# File 'lib/vanity/adapters/active_record_adapter.rb', line 141

def metric_track(metric, timestamp, identity, values)
  record = VanityMetric.retrieve(metric)

  values.each_with_index do |value, index|
    record.vanity_metric_values.create(:date => timestamp.to_date.to_s, :index => index, :value => value)
  end

  record.touch_with_grace_period
  record.save
end

- (Object) metric_values(metric, from, to)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vanity/adapters/active_record_adapter.rb', line 152

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

- (Object) reconnect!



123
124
125
# File 'lib/vanity/adapters/active_record_adapter.rb', line 123

def reconnect!
  VanityRecord.connection.reconnect!
end

- (Object) set_experiment_completed_at(experiment, time)



195
196
197
# File 'lib/vanity/adapters/active_record_adapter.rb', line 195

def set_experiment_completed_at(experiment, time)
  VanityExperiment.retrieve(experiment).update_attribute(:completed_at, time)
end

- (Object) set_experiment_created_at(experiment, time)

Store when experiment was created (do not write over existing value).



182
183
184
185
186
187
# File 'lib/vanity/adapters/active_record_adapter.rb', line 182

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

- (Object) to_s



288
289
290
# File 'lib/vanity/adapters/active_record_adapter.rb', line 288

def to_s
  @options.to_s
end