forked from kamens/gae_bingo
/
api.py
172 lines (120 loc) · 5.75 KB
/
api.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
import logging
import os
from google.appengine.ext.webapp import RequestHandler
from .gae_bingo import choose_alternative, delete_experiment, resume_experiment, modulo_choose
from .cache import BingoCache
from .stats import describe_result_in_words
from .config import can_control_experiments, retrieve_identity
from .jsonify import jsonify
from .plots import get_experiment_timeline_data
from .identity import identity
class Experiments(RequestHandler):
def get(self):
if not can_control_experiments():
return
bingo_cache = BingoCache.get()
experiment_results = {}
for experiment_name in bingo_cache.experiments:
experiment = bingo_cache.get_experiment(experiment_name)
if experiment.canonical_name not in experiment_results:
experiment_results[experiment.canonical_name] = experiment
context = { "experiment_results": experiment_results.values() }
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(jsonify(context))
class ExperimentSummary(RequestHandler):
def get(self):
if not can_control_experiments():
return
bingo_cache = BingoCache.get()
canonical_name = self.request.get("canonical_name")
experiments, alternatives = bingo_cache.experiments_and_alternatives_from_canonical_name(canonical_name)
if not experiments:
raise Exception("No experiments matching canonical name: %s" % canonical_name)
experiments = sorted(experiments, key=lambda experiment: experiment.conversion_name)
context = {}
is_first = True
for experiment in experiments:
if "canonical_name" not in context:
context["canonical_name"] = experiment.canonical_name
if "live" not in context:
context["live"] = experiment.live
if "multiple_experiments" not in context:
context["multiple_experiments"] = len(experiments) > 1
if "experiments" not in context:
context["experiments"] = []
context["experiments"].append({
"conversion_name": experiment.conversion_name,
"experiment_name": experiment.name,
"is_first": is_first,
})
is_first = False
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(jsonify(context))
class ExperimentConversions(RequestHandler):
def get(self):
if not can_control_experiments():
return
bingo_cache = BingoCache.get()
experiment_name = self.request.get("experiment_name")
experiment = bingo_cache.get_experiment(experiment_name)
alternatives = bingo_cache.get_alternatives(experiment_name)
if not experiment or not alternatives:
raise Exception("No experiment matching name: %s" % canonical_name)
for alternative in alternatives:
alternative.live = experiment.live
alternative.is_short_circuited = (not experiment.live) and (experiment.short_circuit_content == alternative.content)
alternative.load_latest_counts()
context = {
"canonical_name": experiment.canonical_name,
"live": experiment.live,
"total_participants": reduce(lambda a, b: a + b, map(lambda alternative: alternative.participants, alternatives)),
"total_conversions": reduce(lambda a, b: a + b, map(lambda alternative: alternative.conversions, alternatives)),
"alternatives": alternatives,
"significance_test_results": describe_result_in_words(alternatives),
"y_axis_title": experiment.y_axis_title,
"timeline_series": get_experiment_timeline_data(experiment),
}
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(jsonify(context))
class ControlExperiment(RequestHandler):
def post(self):
if not can_control_experiments():
return
canonical_name = self.request.get("canonical_name")
action = self.request.get("action")
if not action or not canonical_name:
return
bingo_cache = BingoCache.get()
if action == "choose_alternative":
choose_alternative(canonical_name, int(self.request.get("alternative_number")))
elif action == "delete":
delete_experiment(canonical_name)
elif action == "resume":
resume_experiment(canonical_name)
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(jsonify(True))
class Alternatives(RequestHandler):
def get(self):
if not can_control_experiments():
return
query = self.request.get("query")
if query:
id = retrieve_identity(query)
else:
id = identity()
if not id:
raise Exception("Error getting identity for query: %s" % str(query))
bingo_cache = BingoCache.get()
chosen_alternatives = {}
for experiment_name in bingo_cache.experiments:
experiment = bingo_cache.get_experiment(experiment_name)
if experiment.canonical_name not in chosen_alternatives:
alternatives = bingo_cache.get_alternatives(experiment_name)
alternative = modulo_choose(experiment.hashable_name, alternatives, id)
chosen_alternatives[experiment.canonical_name] = str(alternative.content)
context = {
"identity": id,
"alternatives": chosen_alternatives,
}
self.response.headers["Content-Type"] = "application/json"
self.response.out.write(jsonify(context))