class Vanity::Adapters::ActiveRecordAdapter

ActiveRecord adapter

Public Class Methods

new(options) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 124
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

Public Instance Methods

ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) click to toggle source

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.

# File lib/vanity/adapters/active_record_adapter.rb, line 296
def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false)
  participant = VanityParticipant.retrieve(experiment, identity, false)
  VanityParticipant.retrieve(experiment, identity, implicit, :converted => alternative, :seen => alternative)
  VanityExperiment.retrieve(experiment).increment_conversion(alternative, count)
end
ab_add_participant(experiment, alternative, identity) click to toggle source

Records a participant in this experiment for the given alternative.

# File lib/vanity/adapters/active_record_adapter.rb, line 273
def ab_add_participant(experiment, alternative, identity)
  VanityParticipant.retrieve(experiment, identity, true, :seen => alternative)
end
ab_assigned(experiment, identity) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 286
def ab_assigned(experiment, identity)
  participant = VanityParticipant.retrieve(experiment, identity, false)
  participant && participant.seen
end
ab_counts(experiment, alternative) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 241
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) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 304
def ab_get_outcome(experiment)
  VanityExperiment.retrieve(experiment).outcome
end
ab_not_showing(experiment, identity) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 268
def ab_not_showing(experiment, identity)
  VanityParticipant.retrieve(experiment, identity, true, :shown => nil)
end
ab_seen(experiment, identity, alternative_or_id) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 278
def ab_seen(experiment, identity, alternative_or_id)
  with_ab_seen_deprecation(experiment, identity, alternative_or_id) do |expt, ident, alt_id|
    participant = VanityParticipant.retrieve(expt, ident, false)
    participant && participant.seen == alt_id
  end
end
ab_set_outcome(experiment, alternative = 0) click to toggle source

Sets the outcome of this experiment to a particular alternative.

# File lib/vanity/adapters/active_record_adapter.rb, line 309
def ab_set_outcome(experiment, alternative = 0)
  VanityExperiment.retrieve(experiment).update_attribute(:outcome, alternative)
end
ab_show(experiment, identity, alternative) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 256
def ab_show(experiment, identity, alternative)
  VanityParticipant.retrieve(experiment, identity, true, :shown => alternative)
end
ab_showing(experiment, identity) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 261
def ab_showing(experiment, identity)
  participant = VanityParticipant.retrieve(experiment, identity, false)
  participant && participant.shown
end
active?() click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 132
def active?
  VanityRecord.connected? && VanityRecord.connection.active?
end
destroy_experiment(experiment) click to toggle source

Deletes all information about this experiment.

# File lib/vanity/adapters/active_record_adapter.rb, line 314
def destroy_experiment(experiment)
  VanityParticipant.where(:experiment_id => experiment.to_s).delete_all
  record = VanityExperiment.find_by_experiment_id(experiment.to_s)
  record && record.destroy
end
destroy_metric(metric) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 186
def destroy_metric(metric)
  record = VanityMetric.find_by_metric_id(metric.to_s)
  record && record.destroy
end
disconnect!() click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 136
def disconnect!
  VanityRecord.connection.disconnect! if active?
end
experiment_persisted?(experiment) click to toggle source

– Experiments –

# File lib/vanity/adapters/active_record_adapter.rb, line 194
def experiment_persisted?(experiment)
  VanityExperiment.find_by_experiment_id(experiment.to_s).present?
end
flushdb() click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 144
def flushdb
  [VanityExperiment, VanityMetric, VanityParticipant, VanityMetricValue, VanityConversion].each do |klass|
    klass.delete_all
  end
end
get_experiment_completed_at(experiment) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 216
def get_experiment_completed_at(experiment)
  VanityExperiment.retrieve(experiment).completed_at
end
get_experiment_created_at(experiment) click to toggle source

Return when experiment was created.

# File lib/vanity/adapters/active_record_adapter.rb, line 207
def get_experiment_created_at(experiment)
  record = VanityExperiment.retrieve(experiment)
  record && record.created_at
end
get_metric_last_update_at(metric) click to toggle source

– Metrics –

# File lib/vanity/adapters/active_record_adapter.rb, line 153
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) click to toggle source

Returns true if experiment completed.

# File lib/vanity/adapters/active_record_adapter.rb, line 221
def is_experiment_completed?(experiment)
  !!VanityExperiment.retrieve(experiment).completed_at
end
is_experiment_enabled?(experiment) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 229
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) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 158
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
metric_values(metric, from, to) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 169
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!() click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 140
def reconnect!
  VanityRecord.connection.reconnect!
end
set_experiment_completed_at(experiment, time) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 212
def set_experiment_completed_at(experiment, time)
  VanityExperiment.retrieve(experiment).update_attribute(:completed_at, time)
end
set_experiment_created_at(experiment, time) click to toggle source

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

# File lib/vanity/adapters/active_record_adapter.rb, line 199
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) click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 225
def set_experiment_enabled(experiment, enabled)
  VanityExperiment.retrieve(experiment).update_attribute(:enabled, enabled)
end
to_s() click to toggle source
# File lib/vanity/adapters/active_record_adapter.rb, line 320
def to_s
  @options.to_s
end