Class: Vanity::Adapters::MockAdapter

Inherits:
AbstractAdapter show all
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.

Since:

  • 1.4.0

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MockAdapter

Returns a new instance of MockAdapter.

Since:

  • 1.4.0



17
18
19
20
# File 'lib/vanity/adapters/mock_adapter.rb', line 17

def initialize(options)
  @metrics = @@metrics ||= {}
  @experiments = @@experiments ||= {}
end

Instance Method Details

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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Returns:

  • (Boolean)

Since:

  • 1.4.0



22
23
24
# File 'lib/vanity/adapters/mock_adapter.rb', line 22

def active?
  !!@metrics
end

#destroy_experiment(experiment) ⇒ Object

Since:

  • 1.4.0



152
153
154
# File 'lib/vanity/adapters/mock_adapter.rb', line 152

def destroy_experiment(experiment)
  @experiments.delete experiment
end

#destroy_metric(metric) ⇒ Object

Since:

  • 1.4.0



66
67
68
# File 'lib/vanity/adapters/mock_adapter.rb', line 66

def destroy_metric(metric)
  @metrics.delete metric
end

#disconnect!Object

Since:

  • 1.4.0



26
27
28
29
# File 'lib/vanity/adapters/mock_adapter.rb', line 26

def disconnect!
  @metrics = nil
  @experiments = nil
end

#flushdbObject

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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 –

Since:

  • 1.4.0



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

Returns:

  • (Boolean)

Since:

  • 1.4.0



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

Since:

  • 1.4.0



52
53
54
55
56
57
58
59
# File 'lib/vanity/adapters/mock_adapter.rb', line 52

def metric_track(metric, timestamp, identity, values)
  @metrics[metric] ||= {}
  current = @metrics[metric][timestamp.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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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

Since:

  • 1.4.0



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 –

Since:

  • 1.4.0



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

#to_sObject

Since:

  • 1.4.0



36
37
38
# File 'lib/vanity/adapters/mock_adapter.rb', line 36

def to_s
  "mock:/"
end