This repository was archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexperiment.rb
More file actions
295 lines (241 loc) · 8.65 KB
/
Copy pathexperiment.rb
File metadata and controls
295 lines (241 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
class Verdict::Experiment
include Verdict::Metadata
attr_reader :handle, :qualifiers, :event_logger
def self.define(handle, *args, &block)
experiment = self.new(handle, *args, &block)
raise Verdict::ExperimentHandleNotUnique.new(experiment.handle) if Verdict.repository.has_key?(experiment.handle)
Verdict.repository[experiment.handle] = experiment
end
def initialize(handle, options = {}, &block)
@started_at = nil
@handle = handle.to_s
options = default_options.merge(options)
@qualifiers = Array(options[:qualifier] || options[:qualifiers])
@event_logger = options[:event_logger] || Verdict::EventLogger.new(Verdict.default_logger)
@storage = storage(options[:storage] || :memory)
@store_unqualified = options[:store_unqualified]
@segmenter = options[:segmenter]
@subject_type = options[:subject_type]
@disqualify_empty_identifier = options[:disqualify_empty_identifier]
@manual_assignment_timestamps = options[:manual_assignment_timestamps]
instance_eval(&block) if block_given?
end
def subject_type(type = nil)
return @subject_type if type.nil?
@subject_type = type
end
def store_unqualified?
@store_unqualified
end
def manual_assignment_timestamps?
@manual_assignment_timestamps
end
def group(handle)
segmenter.groups[handle.to_s]
end
def groups(segmenter_class = Verdict::Segmenters::FixedPercentageSegmenter, &block)
return segmenter.groups unless block_given?
@segmenter ||= segmenter_class.new(self)
@segmenter.instance_eval(&block)
@segmenter.verify!
return self
end
# Optional: Together with the "end_timestamp" and "stop_new_assignment_timestamp", limits the experiment run timeline within
# the given time interval.
#
# Timestamps definitions:
# start_timestamp: Experiment's start time. No assignments are made i.e. switch will return nil before this timestamp.
# stop_new_assignment_timestamp: Experiment's new assignment stop time. No new assignments are made
# i.e. switch returns nil for new assignments but the existing assignments are preserved.
# end_timestamp: Experiment's end time. No assignments are made i.e. switch returns nil after this timestamp.
#
# Experiment run timeline:
# start_timestamp -> (new assignments occur) -> stop_new_assignment_timestamp -> (no new assignments occur) -> end_timestamp
def schedule_start_timestamp(timestamp)
@schedule_start_timestamp = timestamp
end
def schedule_end_timestamp(timestamp)
@schedule_end_timestamp = timestamp
end
def schedule_stop_new_assignment_timestamp(timestamp)
@schedule_stop_new_assignment_timestamp = timestamp
end
def rollout_percentage(percentage, rollout_group_name = :enabled)
groups(Verdict::Segmenters::RolloutSegmenter) do
group rollout_group_name, percentage
end
end
def qualify(method_name = nil, &block)
if block_given?
@qualifiers << block
elsif method_name.nil?
raise ArgumentError, "no method nor blocked passed!"
elsif respond_to?(method_name, true)
@qualifiers << method(method_name).to_proc
else
raise ArgumentError, "No helper for #{method_name.inspect}"
end
end
def storage(storage = nil, options = {})
return @storage if storage.nil?
@store_unqualified = options[:store_unqualified] if options.has_key?(:store_unqualified)
@storage = case storage
when :memory; Verdict::Storage::MemoryStorage.new
when :none; Verdict::Storage::MockStorage.new
when Class; storage.new
else storage
end
end
def segmenter
raise Verdict::Error, "No groups defined for experiment #{@handle.inspect}." if @segmenter.nil?
@segmenter
end
def started_at
@started_at ||= @storage.retrieve_start_timestamp(self)
rescue Verdict::StorageError
nil
end
def started?
!@started_at.nil?
end
def group_handles
segmenter.groups.keys
end
def subject_assignment(subject, group, originally_created_at = nil, temporary = false)
Verdict::Assignment.new(self, subject, group, originally_created_at, temporary)
end
def subject_conversion(subject, goal, created_at = Time.now.utc)
Verdict::Conversion.new(self, subject, goal, created_at)
end
def convert(subject, goal)
identifier = retrieve_subject_identifier(subject)
conversion = subject_conversion(subject, goal)
event_logger.log_conversion(conversion)
segmenter.conversion_feedback(identifier, subject, conversion)
conversion
rescue Verdict::EmptySubjectIdentifier
raise unless disqualify_empty_identifier?
end
def assign(subject, context = nil)
previous_assignment = lookup(subject)
subject_identifier = retrieve_subject_identifier(subject)
assignment = if previous_assignment
previous_assignment
elsif subject_qualifies?(subject, context) && is_make_new_assignments?
group = segmenter.assign(subject_identifier, subject, context)
subject_assignment(subject, group, nil, group.nil?)
else
nil_assignment(subject)
end
store_assignment(assignment)
rescue Verdict::StorageError
nil_assignment(subject)
rescue Verdict::EmptySubjectIdentifier
if disqualify_empty_identifier?
nil_assignment(subject)
else
raise
end
end
def assign_manually(subject, group)
assignment = subject_assignment(subject, group)
if !assignment.qualified? && !store_unqualified?
raise Verdict::Error, "Unqualified subject assignments are not stored for this experiment, so manual disqualification is impossible. Consider setting :store_unqualified to true for this experiment."
end
store_assignment(assignment)
assignment
end
def disqualify_manually(subject)
assign_manually(subject, nil)
end
def store_assignment(assignment)
@storage.store_assignment(assignment) if should_store_assignment?(assignment)
event_logger.log_assignment(assignment)
assignment
end
def cleanup(options = {})
@storage.cleanup(self, options)
end
def remove_subject_assignment(subject)
@storage.remove_assignment(self, subject)
end
def switch(subject, context = nil)
return unless is_scheduled?
assign(subject, context).to_sym
end
def lookup(subject)
@storage.retrieve_assignment(self, subject)
end
def retrieve_subject_identifier(subject)
identifier = subject_identifier(subject).to_s
raise Verdict::EmptySubjectIdentifier, "Subject resolved to an empty identifier!" if identifier.empty?
identifier
end
def has_qualifier?
@qualifiers.any?
end
def everybody_qualifies?
!has_qualifier?
end
def as_json(options = {})
{
handle: handle,
has_qualifier: has_qualifier?,
groups: segmenter.groups.values.map { |group| group.as_json(options) },
metadata: metadata,
started_at: started_at.nil? ? nil : started_at.utc.strftime('%FT%TZ')
}.tap do |data|
data[:subject_type] = subject_type.to_s unless subject_type.nil?
end
end
def to_json(options = {})
as_json(options).to_json
end
def fetch_subject(subject_identifier)
raise NotImplementedError, "Fetching subjects based on identifier is not implemented for experiment #{@handle.inspect}."
end
def disqualify_empty_identifier?
@disqualify_empty_identifier
end
def subject_qualifies?(subject, context = nil)
ensure_experiment_has_started
everybody_qualifies? || @qualifiers.all? { |qualifier| qualifier.call(subject, context) }
end
protected
def default_options
{}
end
def should_store_assignment?(assignment)
assignment.permanent? && !assignment.returning? && (store_unqualified? || assignment.qualified?)
end
def subject_identifier(subject)
subject.respond_to?(:id) ? subject.id : subject.to_s
end
def set_start_timestamp
@storage.store_start_timestamp(self, started_now = Time.now.utc)
started_now
rescue NotImplementedError
nil
end
def ensure_experiment_has_started
@started_at ||= started_at || set_start_timestamp
rescue Verdict::StorageError
@started_at ||= Time.now.utc
end
def nil_assignment(subject)
Verdict::Assignment.new(self, subject, nil, nil)
end
private
def is_scheduled?
if @schedule_start_timestamp && @schedule_start_timestamp > Time.now
return false
end
if @schedule_end_timestamp && @schedule_end_timestamp <= Time.now
return false
end
return true
end
def is_make_new_assignments?
return !(@schedule_stop_new_assignment_timestamp && @schedule_stop_new_assignment_timestamp <= Time.now)
end
end