Class: Vanity::Adapters::RedisAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/vanity/adapters/redis_adapter.rb

Overview

Redis adapter.

Since:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RedisAdapter) initialize(options)

Returns a new instance of RedisAdapter

Since:

  • 1.4.0



31
32
33
34
35
36
# File 'lib/vanity/adapters/redis_adapter.rb', line 31

def initialize(options)
  @options = options.clone
  @options[:db] ||= @options[:database] || (@options[:path] && @options.delete(:path).split("/")[1].to_i)
  @options[:thread_safe] = true
  connect!
end

Instance Attribute Details

- (Object) redis (readonly)

Since:

  • 1.4.0



29
30
31
# File 'lib/vanity/adapters/redis_adapter.rb', line 29

def redis
  @redis
end

Instance Method Details

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

Since:

  • 1.4.0



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/vanity/adapters/redis_adapter.rb', line 185

def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false)
  call_redis_with_failover(experiment, alternative, identity, count, implicit) do
    if implicit
      @experiments.sadd "#{experiment}:alts:#{alternative}:participants", identity
    else
      participating = @experiments.sismember("#{experiment}:alts:#{alternative}:participants", identity)
    end
    @experiments.sadd "#{experiment}:alts:#{alternative}:converted", identity if implicit || participating
    @experiments.incrby "#{experiment}:alts:#{alternative}:conversions", count
  end
end

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

Since:

  • 1.4.0



157
158
159
160
161
# File 'lib/vanity/adapters/redis_adapter.rb', line 157

def ab_add_participant(experiment, alternative, identity)
  call_redis_with_failover(experiment, alternative, identity) do
    @experiments.sadd "#{experiment}:alts:#{alternative}:participants", identity
  end
end

- (Object) ab_assigned(experiment, identity)

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

Since:

  • 1.4.0



174
175
176
177
178
179
180
181
182
183
# File 'lib/vanity/adapters/redis_adapter.rb', line 174

def ab_assigned(experiment, identity)
  call_redis_with_failover do
    Vanity.playground.experiments[experiment].alternatives.each do |alternative|
      if @experiments.sismember "#{experiment}:alts:#{alternative.id}:participants", identity
        return alternative.id
      end
    end
    nil
  end
end

- (Object) ab_counts(experiment, alternative)

Since:

  • 1.4.0



130
131
132
133
134
135
136
# File 'lib/vanity/adapters/redis_adapter.rb', line 130

def ab_counts(experiment, alternative)
  {
    :participants => @experiments.scard("#{experiment}:alts:#{alternative}:participants").to_i,
          :converted    => @experiments.scard("#{experiment}:alts:#{alternative}:converted").to_i,
    :conversions  => @experiments["#{experiment}:alts:#{alternative}:conversions"].to_i
  }
end

- (Object) ab_get_outcome(experiment)

Since:

  • 1.4.0



197
198
199
200
# File 'lib/vanity/adapters/redis_adapter.rb', line 197

def ab_get_outcome(experiment)
  alternative = @experiments["#{experiment}:outcome"]
  alternative && alternative.to_i
end

- (Object) ab_not_showing(experiment, identity)

Since:

  • 1.4.0



151
152
153
154
155
# File 'lib/vanity/adapters/redis_adapter.rb', line 151

def ab_not_showing(experiment, identity)
  call_redis_with_failover do
    @experiments.del "#{experiment}:participant:#{identity}:show"
  end
end

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

Since:

  • 1.4.0



163
164
165
166
167
168
169
170
171
# File 'lib/vanity/adapters/redis_adapter.rb', line 163

def ab_seen(experiment, identity, alternative)
  call_redis_with_failover(experiment, identity, alternative) do
    if @experiments.sismember "#{experiment}:alts:#{alternative.id}:participants", identity
      alternative
    else
      nil
    end
  end
end

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

Since:

  • 1.4.0



202
203
204
# File 'lib/vanity/adapters/redis_adapter.rb', line 202

def ab_set_outcome(experiment, alternative = 0)
  @experiments.setnx "#{experiment}:outcome", alternative
end

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

Since:

  • 1.4.0



138
139
140
141
142
# File 'lib/vanity/adapters/redis_adapter.rb', line 138

def ab_show(experiment, identity, alternative)
  call_redis_with_failover do
    @experiments["#{experiment}:participant:#{identity}:show"] = alternative
  end
end

- (Object) ab_showing(experiment, identity)

Since:

  • 1.4.0



144
145
146
147
148
149
# File 'lib/vanity/adapters/redis_adapter.rb', line 144

def ab_showing(experiment, identity)
  call_redis_with_failover do
    alternative = @experiments["#{experiment}:participant:#{identity}:show"]
    alternative && alternative.to_i
  end
end

- (Boolean) active?

Returns:

  • (Boolean)

Since:

  • 1.4.0



38
39
40
# File 'lib/vanity/adapters/redis_adapter.rb', line 38

def active?
  !!@redis
end

- (Object) connect!

Since:

  • 1.4.0



58
59
60
61
62
# File 'lib/vanity/adapters/redis_adapter.rb', line 58

def connect!
  @redis = @options[:redis] || Redis.new(@options)
  @metrics = Redis::Namespace.new("vanity:metrics", :redis=>redis)
  @experiments = Redis::Namespace.new("vanity:experiments", :redis=>redis)
end

- (Object) destroy_experiment(experiment)

Since:

  • 1.4.0



206
207
208
209
210
# File 'lib/vanity/adapters/redis_adapter.rb', line 206

def destroy_experiment(experiment)
  @experiments.del "#{experiment}:outcome", "#{experiment}:created_at", "#{experiment}:completed_at"
  alternatives = @experiments.keys("#{experiment}:alts:*")
  @experiments.del *alternatives unless alternatives.empty?
end

- (Object) destroy_metric(metric)

Since:

  • 1.4.0



93
94
95
# File 'lib/vanity/adapters/redis_adapter.rb', line 93

def destroy_metric(metric)
  @metrics.del *@metrics.keys("#{metric}:*")
end

- (Object) disconnect!

Since:

  • 1.4.0



42
43
44
45
46
47
48
49
50
51
# File 'lib/vanity/adapters/redis_adapter.rb', line 42

def disconnect!
  if redis
    begin
      redis.client.disconnect
    rescue Exception => e
      warn("Error while disconnecting from redis: #{e.message}")
    end
  end
  @redis = nil
end

- (Boolean) experiment_persisted?(experiment)

– Experiments –

Returns:

  • (Boolean)

Since:

  • 1.4.0



100
101
102
# File 'lib/vanity/adapters/redis_adapter.rb', line 100

def experiment_persisted?(experiment)
  !!@experiments["#{experiment}:created_at"]
end

- (Object) flushdb

Since:

  • 1.4.0



68
69
70
# File 'lib/vanity/adapters/redis_adapter.rb', line 68

def flushdb
  @redis.flushdb
end

- (Object) get_experiment_completed_at(experiment)

Since:

  • 1.4.0



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

def get_experiment_completed_at(experiment)
  completed_at = @experiments["#{experiment}:completed_at"]
  completed_at && Time.at(completed_at.to_i)
end

- (Object) get_experiment_created_at(experiment)

Since:

  • 1.4.0



110
111
112
113
# File 'lib/vanity/adapters/redis_adapter.rb', line 110

def get_experiment_created_at(experiment)
  created_at = @experiments["#{experiment}:created_at"]
  created_at && Time.at(created_at.to_i)
end

- (Object) get_metric_last_update_at(metric)

– Metrics –

Since:

  • 1.4.0



74
75
76
77
# File 'lib/vanity/adapters/redis_adapter.rb', line 74

def get_metric_last_update_at(metric)
  last_update_at = @metrics["#{metric}:last_update_at"]
  last_update_at && Time.at(last_update_at.to_i)
end

- (Boolean) is_experiment_completed?(experiment)

Returns:

  • (Boolean)

Since:

  • 1.4.0



124
125
126
127
128
# File 'lib/vanity/adapters/redis_adapter.rb', line 124

def is_experiment_completed?(experiment)
  call_redis_with_failover do
    @experiments.exists("#{experiment}:completed_at")
  end
end

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

Since:

  • 1.4.0



79
80
81
82
83
84
85
86
# File 'lib/vanity/adapters/redis_adapter.rb', line 79

def metric_track(metric, timestamp, identity, values)
  call_redis_with_failover(metric, timestamp, identity, values) do
    values.each_with_index do |v,i|
      @metrics.incrby "#{metric}:#{timestamp.to_date}:value:#{i}", v
    end
    @metrics["#{metric}:last_update_at"] = Time.now.to_i
  end
end

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

Since:

  • 1.4.0



88
89
90
91
# File 'lib/vanity/adapters/redis_adapter.rb', line 88

def metric_values(metric, from, to)
  single = @metrics.mget(*(from.to_date..to.to_date).map { |date| "#{metric}:#{date}:value:0" }) || []
  single.map { |v| [v] }
end

- (Object) reconnect!

Since:

  • 1.4.0



53
54
55
56
# File 'lib/vanity/adapters/redis_adapter.rb', line 53

def reconnect!
  disconnect!
  connect!
end

- (Object) set_experiment_completed_at(experiment, time)

Since:

  • 1.4.0



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

def set_experiment_completed_at(experiment, time)
  @experiments.setnx "#{experiment}:completed_at", time.to_i
end

- (Object) set_experiment_created_at(experiment, time)

Since:

  • 1.4.0



104
105
106
107
108
# File 'lib/vanity/adapters/redis_adapter.rb', line 104

def set_experiment_created_at(experiment, time)
  call_redis_with_failover do
    @experiments.setnx "#{experiment}:created_at", time.to_i
  end
end

- (Object) to_s

Since:

  • 1.4.0



64
65
66
# File 'lib/vanity/adapters/redis_adapter.rb', line 64

def to_s
  redis.id
end