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

rubocop:todo Lint/MissingSuper

Since:

  • 1.4.0


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

def initialize(_options) # rubocop:todo Lint/MissingSuper
  @metrics = @@metrics ||= {} # rubocop:todo Style/ClassVars
  @experiments = @@experiments ||= {} # rubocop:todo Style/ClassVars
end

Instance Method Details

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

Since:

  • 1.4.0


152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vanity/adapters/mock_adapter.rb', line 152

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


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

def ab_add_participant(experiment, alternative, identity)
  alt = alternative(experiment, alternative)
  alt[:participants] ||= Set.new
  alt[:participants] << identity
end

#ab_assigned(experiment, identity) ⇒ Object

Since:

  • 1.4.0


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

def ab_assigned(experiment, identity)
  alternatives_for(experiment).each do |alt_id, alt_state|
    return alt_id if alt_state[:participants].include?(identity)
  end

  nil
end

#ab_counts(experiment, alternative) ⇒ Object

Since:

  • 1.4.0


111
112
113
114
115
116
# File 'lib/vanity/adapters/mock_adapter.rb', line 111

def ab_counts(experiment, alternative)
  alt = alternative(experiment, 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


166
167
168
169
# File 'lib/vanity/adapters/mock_adapter.rb', line 166

def ab_get_outcome(experiment)
  @experiments[experiment] ||= {}
  @experiments[experiment][:outcome]
end

#ab_not_showing(experiment, identity) ⇒ Object

Since:

  • 1.4.0


128
129
130
# File 'lib/vanity/adapters/mock_adapter.rb', line 128

def ab_not_showing(experiment, identity)
  @experiments[experiment][:showing].delete identity if @experiments[experiment] && @experiments[experiment][:showing]
end

#ab_seen(experiment, identity, alternative_or_id) ⇒ Object

Since:

  • 1.4.0


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

def ab_seen(experiment, identity, alternative_or_id)
  with_ab_seen_deprecation(experiment, identity, alternative_or_id) do |expt, ident, alt_id|
    alt_id if ab_assigned(expt, ident) == alt_id
  end
end

#ab_set_outcome(experiment, alternative = 0) ⇒ Object

Since:

  • 1.4.0


171
172
173
174
# File 'lib/vanity/adapters/mock_adapter.rb', line 171

def ab_set_outcome(experiment, alternative = 0)
  @experiments[experiment] ||= {}
  @experiments[experiment][:outcome] = alternative
end

#ab_show(experiment, identity, alternative) ⇒ Object

Since:

  • 1.4.0


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

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


124
125
126
# File 'lib/vanity/adapters/mock_adapter.rb', line 124

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


176
177
178
# File 'lib/vanity/adapters/mock_adapter.rb', line 176

def destroy_experiment(experiment)
  @experiments.delete experiment
end

#destroy_metric(metric) ⇒ Object

Since:

  • 1.4.0


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

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

#experiment_persisted?(experiment) ⇒ Boolean

– Experiments –

Returns:

  • (Boolean)

Since:

  • 1.4.0


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

def experiment_persisted?(experiment)
  !!@experiments[experiment]
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


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

def get_experiment_completed_at(experiment)
  @experiments[experiment] && @experiments[experiment][:completed_at]
end

#get_experiment_created_at(experiment) ⇒ Object

Since:

  • 1.4.0


80
81
82
# File 'lib/vanity/adapters/mock_adapter.rb', line 80

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


47
48
49
# File 'lib/vanity/adapters/mock_adapter.rb', line 47

def get_metric_last_update_at(metric)
  @metrics[metric] && @metrics[metric][:last_update_at]
end

#is_experiment_completed?(experiment) ⇒ Boolean

rubocop:todo Naming/PredicateName

Returns:

  • (Boolean)

Since:

  • 1.4.0


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

def is_experiment_completed?(experiment) # rubocop:todo Naming/PredicateName
  @experiments[experiment] && @experiments[experiment][:completed_at]
end

#is_experiment_enabled?(experiment) ⇒ Boolean

rubocop:todo Naming/PredicateName

Returns:

  • (Boolean)

Since:

  • 1.4.0


102
103
104
105
106
107
108
109
# File 'lib/vanity/adapters/mock_adapter.rb', line 102

def is_experiment_enabled?(experiment) # rubocop:todo Naming/PredicateName
  record = @experiments[experiment]
  if Vanity.configuration.experiments_start_enabled
    record.nil? || record[:enabled] != false
  else
    record && record[:enabled] == true
  end
end

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

Since:

  • 1.4.0


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

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


60
61
62
63
# File 'lib/vanity/adapters/mock_adapter.rb', line 60

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


84
85
86
87
# File 'lib/vanity/adapters/mock_adapter.rb', line 84

def set_experiment_completed_at(experiment, time)
  @experiments[experiment] ||= {}
  @experiments[experiment][:completed_at] ||= time
end

#set_experiment_created_at(experiment, time) ⇒ Object

Since:

  • 1.4.0


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

def set_experiment_created_at(experiment, time)
  @experiments[experiment] ||= {}
  @experiments[experiment][:created_at] ||= time
end

#set_experiment_enabled(experiment, enabled) ⇒ Object

Since:

  • 1.4.0


97
98
99
100
# File 'lib/vanity/adapters/mock_adapter.rb', line 97

def set_experiment_enabled(experiment, enabled)
  @experiments[experiment] ||= {}
  @experiments[experiment][:enabled] = enabled
end

#to_sObject

Since:

  • 1.4.0


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

def to_s
  "mock:/"
end