forked from kamens/gae_bingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
303 lines (234 loc) · 10.6 KB
/
models.py
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
296
297
298
299
300
301
302
303
import datetime
import os
from google.appengine.ext import db
from google.appengine.api import memcache
import pickle_util
# If you use a datastore model to uniquely identify each user,
# let it inherit from this class, like so...
#
# class UserData(GAEBingoIdentityModel)
#
# ...this will let gae_bingo automatically take care of persisting ab_test
# identities from unregistered users to logged in users.
class GAEBingoIdentityModel(db.Model):
gae_bingo_identity = db.StringProperty()
class ConversionTypes():
# Binary conversions are counted at most once per user
Binary = "binary"
# Counting conversions increment each time
Counting = "counting"
@staticmethod
def get_all_as_list():
return [ConversionTypes.Binary, ConversionTypes.Counting]
def __setattr__(self, attr, value):
pass
class _GAEBingoExperiment(db.Model):
name = db.StringProperty()
# Not necessarily unique. Experiments "monkeys" and "monkeys (2)" both have
# canonical_name "monkeys"
canonical_name = db.StringProperty()
family_name = db.StringProperty()
conversion_name = db.StringProperty()
conversion_type = db.StringProperty(default=ConversionTypes.Binary, choices=set(ConversionTypes.get_all_as_list()))
# Experiments can be live (running), stopped (not running, not archived),
# or archived (not running, permanently archived).
# Stopped experiments aren't collecting data, but they exist and can be
# used to "short-circuit" an alternative by showing it to all users even
# before the code is appropriately modified to do so.
live = db.BooleanProperty(default = True)
archived = db.BooleanProperty(default = False)
dt_started = db.DateTimeProperty(auto_now_add = True)
short_circuit_pickled_content = db.BlobProperty()
@property
def stopped(self):
return not (self.archived or self.live)
@property
def short_circuit_content(self):
if self.short_circuit_pickled_content:
return pickle_util.load(self.short_circuit_pickled_content)
else:
return None
def set_short_circuit_content(self, value):
self.short_circuit_pickled_content = pickle_util.dump(value)
@property
def pretty_name(self):
return self.name.capitalize().replace("_", " ")
@property
def pretty_conversion_name(self):
return self.conversion_name.capitalize().replace("_", " ")
@property
def pretty_canonical_name(self):
return self.canonical_name.capitalize().replace("_", " ")
@property
def conversion_group(self):
group = "_".join(self.conversion_name.split("_")[:-1])
return group.capitalize().replace("_", " ")
@property
def hashable_name(self):
return self.family_name if self.family_name else self.canonical_name
@property
def age_desc(self):
if self.archived:
return "Ran %s UTC" % self.dt_started.strftime('%Y-%m-%d at %H:%M:%S')
days_running = (datetime.datetime.now() - self.dt_started).days
if days_running < 1:
return "Less than a day old"
else:
return "%s day%s old" % (days_running, ("" if days_running == 1 else "s"))
@property
def y_axis_title(self):
if self.conversion_type == ConversionTypes.Counting:
"Average Conversions per Participant"
else:
"Conversions (%)"
class _GAEBingoAlternative(db.Model):
number = db.IntegerProperty()
experiment_name = db.StringProperty()
pickled_content = db.BlobProperty()
conversions = db.IntegerProperty(default = 0)
participants = db.IntegerProperty(default = 0)
live = db.BooleanProperty(default = True)
archived = db.BooleanProperty(default = False)
weight = db.IntegerProperty(default = 1)
@staticmethod
def key_for_experiment_name_and_number(experiment_name, number):
return "_gae_alternative:%s:%s" % (experiment_name, number)
@property
def content(self):
return pickle_util.load(self.pickled_content)
@property
def pretty_content(self):
return str(self.content).capitalize()
@property
def conversion_rate(self):
if self.participants > 0:
return float(self.conversions) / float(self.participants)
return 0
@property
def pretty_conversion_rate(self):
return "%4.2f%%" % (self.conversion_rate * 100)
def key_for_self(self):
return _GAEBingoAlternative.key_for_experiment_name_and_number(self.experiment_name, self.number)
def increment_participants(self):
""" Increment a memcache.incr-backed counter to keep track of participants in a scalable fashion.
It's possible that the cached _GAEBingoAlternative entities will fall a bit behind
due to concurrency issues, but the memcache.incr'd version should stay up-to-date and
be persisted.
Returns:
True if participants was successfully incremented, False otherwise."
"""
participants = memcache.incr("%s:participants" % self.key_for_self(), initial_value=self.participants)
if participants is None:
# Memcache may be down and returning None for incr. Don't update the model in this case.
return False
self.participants = participants
return True
def increment_conversions(self):
""" Increment a memcache.incr-backed counter to keep track of conversions in a scalable fashion.
It's possible that the cached _GAEBingoAlternative entities will fall a bit behind
due to concurrency issues, but the memcache.incr'd version should stay up-to-date and
be persisted.
Returns:
True if conversions was successfully incremented, False otherwise.
"""
conversions = memcache.incr("%s:conversions" % self.key_for_self(), initial_value=self.conversions)
if conversions is None:
# Memcache may be down and returning None for incr. Don't update the model in this case.
return False
self.conversions = conversions
return True
def latest_participants_count(self):
return max(self.participants, long(memcache.get("%s:participants" % self.key_for_self()) or 0))
def latest_conversions_count(self):
return max(self.conversions, long(memcache.get("%s:conversions" % self.key_for_self()) or 0))
def reset_counts(self):
memcache.delete_multi(["%s:participants" % self.key_for_self(), "%s:conversions" % self.key_for_self()])
def load_latest_counts(self):
# When persisting to datastore, we want to store the most recent value we've got
self.participants = self.latest_participants_count()
self.conversions = self.latest_conversions_count()
class _GAEBingoSnapshotLog(db.Model):
alternative_number = db.IntegerProperty()
conversions = db.IntegerProperty(default = 0)
participants = db.IntegerProperty(default = 0)
time_recorded = db.DateTimeProperty(auto_now_add = True)
class _GAEBingoExperimentNotes(db.Model):
"""Notes and list of emotions associated w/ results of an experiment."""
# arbitrary user-supplied notes
notes = db.TextProperty()
# list of choices from selection of emotions, such as "happy" and "surprised"
pickled_emotions = db.BlobProperty()
@staticmethod
def key_for_experiment(experiment):
"""Return the key for this experiment's notes."""
return "_gae_bingo_notes:%s" % experiment.name
@staticmethod
def get_for_experiment(experiment):
"""Return GAEBingoExperimentNotes, if it exists, for the experiment."""
return _GAEBingoExperimentNotes.get_by_key_name(
_GAEBingoExperimentNotes.key_for_experiment(experiment),
parent=experiment)
@staticmethod
def save(experiment, notes, emotions):
"""Save notes and emo list, associating with specified experiment."""
notes = _GAEBingoExperimentNotes(
key_name = _GAEBingoExperimentNotes.key_for_experiment(experiment),
parent = experiment,
notes = notes,
pickled_emotions = pickle_util.dump(emotions))
notes.put()
@property
def emotions(self):
"""Return unpickled list of emotions tied to these notes."""
if self.pickled_emotions:
return pickle_util.load(self.pickled_emotions)
else:
return None
class _GAEBingoIdentityRecord(db.Model):
identity = db.StringProperty()
pickled = db.BlobProperty()
@staticmethod
def key_for_identity(identity):
return "_gae_bingo_identity_record:%s" % identity
@staticmethod
def load(identity):
gae_bingo_identity_record = _GAEBingoIdentityRecord.get_by_key_name(_GAEBingoIdentityRecord.key_for_identity(identity))
if gae_bingo_identity_record:
return pickle_util.load(gae_bingo_identity_record.pickled)
return None
def create_experiment_and_alternatives(experiment_name, canonical_name, alternative_params = None, conversion_name = None, conversion_type = ConversionTypes.Binary, family_name = None):
if not experiment_name:
raise Exception("gae_bingo experiments must be named.")
conversion_name = conversion_name or experiment_name
if not alternative_params:
# Default to simple True/False testing
alternative_params = [True, False]
# Generate a random key name for this experiment so it doesn't collide with
# any past experiments of the same name. All other entities, such as
# alternatives, snapshots, and notes, will then use this entity as their
# parent.
experiment = _GAEBingoExperiment(
key_name = "%s:%s" % (
experiment_name, os.urandom(8).encode("hex")),
name = experiment_name,
canonical_name = canonical_name,
family_name = family_name,
conversion_name = conversion_name,
conversion_type = conversion_type,
live = True,
)
alternatives = []
is_dict = type(alternative_params) == dict
for i, content in enumerate(alternative_params):
alternatives.append(
_GAEBingoAlternative(
key_name = _GAEBingoAlternative.key_for_experiment_name_and_number(experiment_name, i),
parent = experiment,
experiment_name = experiment.name,
number = i,
pickled_content = pickle_util.dump(content),
live = True,
weight = alternative_params[content] if is_dict else 1,
)
)
return experiment, alternatives