Class: Vanity::Adapters::RedisAdapter

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

Overview

Redis adapter.

Since:

  • 1.4.0

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RedisAdapter

Returns a new instance of RedisAdapter.

Since:

  • 1.4.0



17
18
19
20
21
22
# File 'lib/vanity/adapters/redis_adapter.rb', line 17

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

Instance Method Details

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

Since:

  • 1.4.0



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

def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false)
  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

#ab_add_participant(experiment, alternative, identity) ⇒ Object

Since:

  • 1.4.0



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

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

#ab_counts(experiment, alternative) ⇒ Object

Since:

  • 1.4.0



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

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

#ab_get_outcome(experiment) ⇒ Object

Since:

  • 1.4.0



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

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

#ab_not_showing(experiment, identity) ⇒ Object

Since:

  • 1.4.0



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

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

#ab_set_outcome(experiment, alternative = 0) ⇒ Object

Since:

  • 1.4.0



142
143
144
# File 'lib/vanity/adapters/redis_adapter.rb', line 142

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

#ab_show(experiment, identity, alternative) ⇒ Object

Since:

  • 1.4.0



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

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

#ab_showing(experiment, identity) ⇒ Object

Since:

  • 1.4.0



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

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

#active?Boolean

Returns:

  • (Boolean)

Since:

  • 1.4.0



24
25
26
# File 'lib/vanity/adapters/redis_adapter.rb', line 24

def active?
  !!@redis
end

#connect!Object

Since:

  • 1.4.0



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

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

#destroy_experiment(experiment) ⇒ Object

Since:

  • 1.4.0



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

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

#destroy_metric(metric) ⇒ Object

Since:

  • 1.4.0



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

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

#disconnect!Object

Since:

  • 1.4.0



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

def disconnect!
  @redis.quit rescue nil if @redis
  @redis = nil
end

#flushdbObject

Since:

  • 1.4.0



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

def flushdb
  @redis.flushdb
end

#get_experiment_completed_at(experiment) ⇒ Object

Since:

  • 1.4.0



95
96
97
98
# File 'lib/vanity/adapters/redis_adapter.rb', line 95

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

#get_experiment_created_at(experiment) ⇒ Object

Since:

  • 1.4.0



86
87
88
89
# File 'lib/vanity/adapters/redis_adapter.rb', line 86

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

#get_metric_last_update_at(metric) ⇒ Object

– Metrics –

Since:

  • 1.4.0



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

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

#is_experiment_completed?(experiment) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 1.4.0



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

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

#metric_track(metric, timestamp, identity, values) ⇒ Object

Since:

  • 1.4.0



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

def metric_track(metric, timestamp, identity, values)
  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

#metric_values(metric, from, to) ⇒ Object

Since:

  • 1.4.0



70
71
72
73
# File 'lib/vanity/adapters/redis_adapter.rb', line 70

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

#reconnect!Object

Since:

  • 1.4.0



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

def reconnect!
  disconnect!
  connect!
end

#redisObject

Since:

  • 1.4.0



48
49
50
# File 'lib/vanity/adapters/redis_adapter.rb', line 48

def redis
  @redis
end

#set_experiment_completed_at(experiment, time) ⇒ Object

Since:

  • 1.4.0



91
92
93
# File 'lib/vanity/adapters/redis_adapter.rb', line 91

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

#set_experiment_created_at(experiment, time) ⇒ Object

– Experiments –

Since:

  • 1.4.0



82
83
84
# File 'lib/vanity/adapters/redis_adapter.rb', line 82

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

#to_sObject

Since:

  • 1.4.0



44
45
46
# File 'lib/vanity/adapters/redis_adapter.rb', line 44

def to_s
  @redis.id
end