Class: Vanity::Adapters::MockAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- Vanity::Adapters::MockAdapter
- Defined in:
- lib/vanity/adapters/mock_adapter.rb
Overview
Mock adapter. You can use this when running in test environment, staging, wherever you don’t care for collecting metrics. Doesn’t store anything.
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
- #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) ⇒ MockAdapter
constructor
A new instance of MockAdapter.
- #is_experiment_completed?(experiment) ⇒ Boolean
- #metric_track(metric, timestamp, identity, values) ⇒ Object
- #metric_values(metric, from, to) ⇒ Object
- #reconnect! ⇒ Object
- #set_experiment_completed_at(experiment, time) ⇒ Object
-
#set_experiment_created_at(experiment, time) ⇒ Object
– Experiments –.
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ MockAdapter
Returns a new instance of MockAdapter.
17 18 19 20 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 17 def initialize() @metrics = @@metrics ||= {} @experiments = @@experiments ||= {} end |
Instance Method Details
#ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 126 def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) @experiments[experiment] ||= {} @experiments[experiment][:alternatives] ||= {} alt = @experiments[experiment][:alternatives][alternative] ||= {} alt[:participants] ||= Set.new alt[:converted] ||= Set.new alt[:conversions] ||= 0 if implicit alt[:participants] << identity else participating = alt[:participants].include?(identity) end alt[:converted] << identity if implicit || participating alt[:conversions] += count end |
#ab_add_participant(experiment, alternative, identity) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 118 def ab_add_participant(experiment, alternative, identity) @experiments[experiment] ||= {} @experiments[experiment][:alternatives] ||= {} alt = @experiments[experiment][:alternatives][alternative] ||= {} alt[:participants] ||= Set.new alt[:participants] << identity end |
#ab_counts(experiment, alternative) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 95 def ab_counts(experiment, alternative) @experiments[experiment] ||= {} @experiments[experiment][:alternatives] ||= {} alt = @experiments[experiment][:alternatives][alternative] ||= {} { :participants => alt[:participants] ? alt[:participants].size : 0, :converted => alt[:converted] ? alt[:converted].size : 0, :conversions => alt[:conversions] || 0 } end |
#ab_get_outcome(experiment) ⇒ Object
142 143 144 145 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 142 def ab_get_outcome(experiment) @experiments[experiment] ||= {} @experiments[experiment][:outcome] end |
#ab_not_showing(experiment, identity) ⇒ Object
114 115 116 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 114 def ab_not_showing(experiment, identity) @experiments[experiment][:showing].delete identity if @experiments[experiment] && @experiments[experiment][:showing] end |
#ab_set_outcome(experiment, alternative = 0) ⇒ Object
147 148 149 150 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 147 def ab_set_outcome(experiment, alternative = 0) @experiments[experiment] ||= {} @experiments[experiment][:outcome] = alternative end |
#ab_show(experiment, identity, alternative) ⇒ Object
104 105 106 107 108 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 104 def ab_show(experiment, identity, alternative) @experiments[experiment] ||= {} @experiments[experiment][:showing] ||= {} @experiments[experiment][:showing][identity] = alternative end |
#ab_showing(experiment, identity) ⇒ Object
110 111 112 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 110 def ab_showing(experiment, identity) @experiments[experiment] && @experiments[experiment][:showing] && @experiments[experiment][:showing][identity] end |
#active? ⇒ Boolean
22 23 24 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 22 def active? !!@metrics end |
#destroy_experiment(experiment) ⇒ Object
152 153 154 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 152 def destroy_experiment(experiment) @experiments.delete experiment end |
#destroy_metric(metric) ⇒ Object
66 67 68 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 66 def destroy_metric(metric) @metrics.delete metric end |
#disconnect! ⇒ Object
26 27 28 29 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 26 def disconnect! @metrics = nil @experiments = nil end |
#flushdb ⇒ Object
40 41 42 43 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 40 def flushdb @metrics.clear @experiments.clear end |
#get_experiment_completed_at(experiment) ⇒ Object
87 88 89 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 87 def get_experiment_completed_at(experiment) @experiments[experiment] && @experiments[experiment][:completed_at] end |
#get_experiment_created_at(experiment) ⇒ Object
78 79 80 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 78 def get_experiment_created_at(experiment) @experiments[experiment] && @experiments[experiment][:created_at] end |
#get_metric_last_update_at(metric) ⇒ Object
– Metrics –
48 49 50 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 48 def get_metric_last_update_at(metric) @metrics[metric] && @metrics[metric][:last_update_at] end |
#is_experiment_completed?(experiment) ⇒ Boolean
91 92 93 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 91 def is_experiment_completed?(experiment) @experiments[experiment] && @experiments[experiment][:completed_at] end |
#metric_track(metric, timestamp, identity, values) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 52 def metric_track(metric, , identity, values) @metrics[metric] ||= {} current = @metrics[metric][.to_date] ||= [] values.each_with_index do |v,i| current[i] = (current[i] || 0) + v || 0 end @metrics[metric][:last_update_at] = Time.now end |
#metric_values(metric, from, to) ⇒ Object
61 62 63 64 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 61 def metric_values(metric, from, to) hash = @metrics[metric] || {} (from.to_date..to.to_date).map { |date| hash[date] || [] } end |
#reconnect! ⇒ Object
31 32 33 34 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 31 def reconnect! @metrics = @@metrics @experiments = @@experiments end |
#set_experiment_completed_at(experiment, time) ⇒ Object
82 83 84 85 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 82 def set_experiment_completed_at(experiment, time) @experiments[experiment] ||= {} @experiments[experiment][:completed_at] ||= time end |
#set_experiment_created_at(experiment, time) ⇒ Object
– Experiments –
73 74 75 76 |
# File 'lib/vanity/adapters/mock_adapter.rb', line 73 def set_experiment_created_at(experiment, time) @experiments[experiment] ||= {} @experiments[experiment][:created_at] ||= time end |