Class: Vanity::Adapters::RedisAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- Vanity::Adapters::RedisAdapter
- Defined in:
- lib/vanity/adapters/redis_adapter.rb
Overview
Redis adapter.
Instance Method Summary collapse
- #ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) ⇒ Object
- #ab_add_participant(experiment, alternative, identity) ⇒ Object
- #ab_counts(experiment, alternative) ⇒ Object
- #ab_get_outcome(experiment) ⇒ Object
- #ab_not_showing(experiment, identity) ⇒ Object
- #ab_set_outcome(experiment, alternative = 0) ⇒ Object
- #ab_show(experiment, identity, alternative) ⇒ Object
- #ab_showing(experiment, identity) ⇒ Object
- #active? ⇒ Boolean
- #connect! ⇒ Object
- #destroy_experiment(experiment) ⇒ Object
- #destroy_metric(metric) ⇒ Object
- #disconnect! ⇒ Object
- #flushdb ⇒ Object
- #get_experiment_completed_at(experiment) ⇒ Object
- #get_experiment_created_at(experiment) ⇒ Object
-
#get_metric_last_update_at(metric) ⇒ Object
– Metrics –.
-
#initialize(options) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
- #is_experiment_completed?(experiment) ⇒ Boolean
- #metric_track(metric, timestamp, identity, values) ⇒ Object
- #metric_values(metric, from, to) ⇒ Object
- #reconnect! ⇒ Object
- #redis ⇒ Object
- #set_experiment_completed_at(experiment, time) ⇒ Object
-
#set_experiment_created_at(experiment, time) ⇒ Object
– Experiments –.
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
17 18 19 20 21 22 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 17 def initialize() @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
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
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
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
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
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
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
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
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
24 25 26 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 24 def active? !!@redis end |
#connect! ⇒ Object
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
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
75 76 77 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 75 def destroy_metric(metric) @metrics.del *@metrics.keys("#{metric}:*") end |
#disconnect! ⇒ Object
28 29 30 31 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 28 def disconnect! @redis.quit rescue nil if @redis @redis = nil end |
#flushdb ⇒ Object
52 53 54 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 52 def flushdb @redis.flushdb end |
#get_experiment_completed_at(experiment) ⇒ Object
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
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 –
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
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
63 64 65 66 67 68 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 63 def metric_track(metric, , identity, values) values.each_with_index do |v,i| @metrics.incrby "#{metric}:#{.to_date}:value:#{i}", v end @metrics["#{metric}:last_update_at"] = Time.now.to_i end |
#metric_values(metric, from, to) ⇒ Object
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
33 34 35 36 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 33 def reconnect! disconnect! connect! end |
#redis ⇒ Object
48 49 50 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 48 def redis @redis end |
#set_experiment_completed_at(experiment, time) ⇒ Object
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 –
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_s ⇒ Object
44 45 46 |
# File 'lib/vanity/adapters/redis_adapter.rb', line 44 def to_s @redis.id end |