-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathexperiment.js
213 lines (178 loc) · 5.3 KB
/
experiment.js
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
import { shallowCopy, extend, isObject, forEach, map, trimTrailingWhitespace, getParameterByName } from './lib/utils';
export default function provideExperiment(Assignment) {
class Experiment {
constructor(inputs) {
this.inputs = inputs;
this._exposureLogged = false;
this._salt = null;
this._inExperiment = true;
this._autoExposureLog = true;
this.setup();
if (!this.name) {
throw "setup() must set an experiment name via this.setName()";
}
this._assignment = new Assignment(this.getSalt());
this._assigned = false;
}
/* default implementation of fetching the range of experiment parameters that this experiment can take */
getDefaultParamNames() {
var assignmentFxn = this.assign.toString();
var possibleKeys = assignmentFxn.split('.set(');
possibleKeys.splice(0, 1); //remove first index since it'll have the function definitions
return map(possibleKeys, (val) => {
var str = trimTrailingWhitespace(val.split(',')[0]);
return str.substr(1, str.length-2); //remove string chars
});
}
requireAssignment() {
if (!this._assigned) {
this._assign();
}
}
requireExposureLogging(paramName) {
if (this.shouldLogExposure(paramName)) {
this.logExposure();
}
}
_assign() {
this.configureLogger();
var assignVal = this.assign(this._assignment, this.inputs);
if (assignVal || assignVal === undefined) {
this._inExperiment = true;
} else {
this._inExperiment = false;
}
this._assigned = true;
}
setup() {
throw "IMPLEMENT setup";
}
inExperiment() {
return this._inExperiment;
}
addOverride(key, value) {
this._assignment.addOverride(key, value);
}
setOverrides(value) {
this._assignment.setOverrides(value);
var o = this._assignment.getOverrides();
var self = this;
forEach(Object.keys(o), function(key) {
if (self.inputs[key] !== undefined) {
self.inputs[key] = o[key];
}
});
}
setLocalOverride(name) {
var experimentName = getParameterByName('experimentOverride');
var overrideValue = getParameterByName(name);
if (experimentName === this.name && overrideValue) {
this.addOverride(name, overrideValue);
}
}
getSalt() {
if (this._salt) {
return this._salt;
} else {
return this.name;
}
}
setSalt(value) {
this._salt = value;
if (this._assignment) {
this._assignment.experimentSalt = value;
}
}
getName() {
return this.name;
}
assign(params, args) {
throw "IMPLEMENT assign";
}
/*
This function should return a list of the possible parameter names that the assignment procedure may assign.
You can optionally override this function to always return this.getDefaultParamNames()
which will analyze your program at runtime to determine what the range of possible experimental parameters are.
Otherwise, simply return a fixed list of the experimental parameters that your assignment procedure may assign.
*/
getParamNames() {
throw "IMPLEMENT getParamNames";
}
shouldFetchExperimentParameter(name) {
const experimentalParams = this.getParamNames();
return experimentalParams.indexOf(name) >= 0;
}
setName(value) {
var re = /\s+/g;
this.name = value.replace(re, '-');
if (this._assignment) {
this._assignment.experimentSalt = this.getSalt();
}
}
__asBlob(extras={}) {
var d = {
'name': this.getName(),
'time': new Date().getTime() / 1000,
'salt': this.getSalt(),
'inputs': this.inputs,
'params': this._assignment.getParams()
};
extend(d, extras);
return d;
}
setAutoExposureLogging(value) {
this._autoExposureLog = value;
}
getParams() {
this.requireAssignment();
this.requireExposureLogging();
return this._assignment.getParams();
}
get(name, def) {
this.requireAssignment();
this.requireExposureLogging(name);
this.setLocalOverride(name);
return this._assignment.get(name, def);
}
toString() {
this.requireAssignment();
this.requireExposureLogging();
return JSON.stringify(this.__asBlob());
}
logExposure(extras) {
if (!this.inExperiment()) {
return;
}
this._exposureLogged = true;
this.logEvent('exposure', extras);
}
shouldLogExposure(paramName) {
if (paramName !== undefined && !this.shouldFetchExperimentParameter(paramName)) {
return false;
}
return this._autoExposureLog && !this.previouslyLogged();
}
logEvent(eventType, extras) {
if (!this.inExperiment()) {
return;
}
var extraPayload;
if (extras) {
extraPayload = { 'event': eventType, 'extra_data': shallowCopy(extras)};
} else {
extraPayload = { 'event': eventType };
}
this.log(this.__asBlob(extraPayload));
}
configureLogger() {
throw "IMPLEMENT configureLogger";
}
log(data) {
throw "IMPLEMENT log";
}
previouslyLogged() {
throw "IMPLEMENT previouslyLogged";
}
}
return Experiment;
}