/
index.ts
executable file
·634 lines (544 loc) · 21.3 KB
/
index.ts
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import * as fs from "fs";
import * as request from "request-promise-native";
import * as yargs from "yargs";
import * as updateNotifier from "update-notifier";
import { clientUrl, outcomesUrl } from "../schemas/ClientAPI";
import { CreateAppKeyRequest, CreateAppKeyResponse, GetCountsRequest } from "../common/CLIAPI";
import { User, Application } from "./Query";
import chalk from "chalk";
import { table, getBorderCharacters } from "table";
import * as moment from "moment";
import {
tryGetUserInfo,
setUserInfo,
getUserInfo,
getIDToken,
cognito,
clientID,
authenticate
} from "./Authentication";
import { Outcome, Tree } from "../common/ClientConfig";
import { Count, ExperimentCounts } from "../common/ExperimentCounts";
import { lookupBestOption } from "../client/DecisionTree";
const treeify = require("treeify");
const { red, yellow, blue, magenta, cyan, dim, bold } = chalk;
const star = yellow("★");
async function cmdSignup(_args: yargs.Arguments, email: string, password: string): Promise<void> {
let response;
try {
response = await cognito
.signUp({
ClientId: clientID,
Username: email,
Password: password,
UserAttributes: [{ Name: "email", Value: email }]
})
.promise();
} catch (e) {
if (e.code === "UsernameExistsException") {
console.error(`An account already exists for ${email}`);
} else {
console.error(`Could not sign up: ${e.message}`);
}
return;
}
if (response.UserConfirmed) {
console.log("User creation confirmed");
} else {
console.log(`Confirmation code sent to ${response.CodeDeliveryDetails!.Destination}`);
console.log('Please use the "confirm" command with the code');
}
await setUserInfo({ username: email, password });
}
async function createDemoApp() {
const appKey = await createApp("My sample autotune app");
console.log("");
console.log("We've created a sample app for you to start with! Add this script tag:");
console.log("");
console.log(` <script src="${clientUrl(appKey)}"></script>`);
console.log("");
console.log("Then create an experiment:");
console.log("");
console.log(` <autotune>`);
console.log(` <h1>The glass is half full</h1>`);
console.log(` <h1>The glass is half empty</h1>`);
console.log(` </autotune>`);
console.log("");
console.log("Finally, add the autotune attribute to links that you want users to click:");
console.log("");
console.log(` <a href="/buy" autotune>Buy now</a>`);
console.log("");
}
async function cmdConfirm(_args: yargs.Arguments, email: string | undefined, code: string): Promise<void> {
let password: string | undefined = undefined;
if (email === undefined) {
const userInfo = await getUserInfo();
email = userInfo.username;
password = userInfo.password;
}
await cognito
.confirmSignUp({
ClientId: clientID,
Username: email,
ConfirmationCode: code
})
.promise();
console.log("User creation confirmed");
if (password !== undefined) {
await authenticateWithPassword(email, password);
await createDemoApp();
} else {
console.log("Please login to start using autotune");
}
}
async function authenticateWithPassword(email: string, password: string): Promise<void> {
await authenticate(email, "USER_PASSWORD_AUTH", {
USERNAME: email,
PASSWORD: password
});
}
async function cmdLogin(_args: yargs.Arguments, email: string, password: string): Promise<void> {
await authenticateWithPassword(email, password);
console.log("You're now logged in.");
}
async function makeAuthHeaders(): Promise<{ [name: string]: string }> {
return {
"Cache-Control": "no-cache",
"Content-Type": "application/json",
Authorization: await getIDToken()
};
}
async function requestWithAuth<T>(endpoint: string, body: T): Promise<any> {
let url: string;
if (endpoint.startsWith("https://")) {
url = endpoint;
} else {
url = "https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod/" + endpoint;
}
const requestOptions = {
method: "POST",
url,
headers: await makeAuthHeaders(),
body,
json: true
};
return await request(requestOptions).promise();
}
async function createApp(name: string): Promise<string> {
const body: CreateAppKeyRequest = { name };
const result: CreateAppKeyResponse = await requestWithAuth("createAppKey", body);
return result.appKey;
}
async function cmdCreateApp(_args: yargs.Arguments, name: string): Promise<void> {
const appKey = await createApp(name);
console.log(`✓ Created app '${magenta(name)}' with key ${bold(appKey)}`);
console.log();
console.log("Add this code to your web page:");
console.log();
console.log(
[blue("<script"), " ", yellow("src"), blue("="), red(`"${clientUrl(appKey)}"`), blue("></script>")].join("")
);
}
const graphQLQueryAll =
"query {\n viewer {\n username\n applications {\n name\n key\n experiments {\n name\n started\n options {\n name\n completed\n payoff\n }\n }\n }\n }\n}";
const graphQLQueryApplication =
"query($key: String!) {\n getApplication(key: $key) {\n name\n key\n experiments {\n name\n started\n options {\n name\n completed\n payoff\n }\n }\n }\n}";
async function queryGraphQL<T>(
query: string,
queryName: string,
variables?: { [name: string]: any } | undefined
): Promise<T> {
const body = { query, variables };
const result = await requestWithAuth(
"https://wnzihyd3zndg3i3zdo6ijwslze.appsync-api.us-west-2.amazonaws.com/graphql",
body
);
if (result.errors !== undefined) {
throw new Error(`Errors from server: ${JSON.stringify(result)}`);
}
if (result.data === null) {
throw new Error(`No data returned from server`);
}
return result.data[queryName] as T;
}
async function getApp(keyOrName: string): Promise<Application | undefined> {
const isUUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(keyOrName);
if (isUUID) {
const app = await queryGraphQL<Application | null>(graphQLQueryApplication, "getApplication", {
key: keyOrName
});
if (app !== null) return app;
}
// If the it's a UUID it could still be a name - the user might use
// UUIDs as app names.
const user = await queryGraphQL<User>(graphQLQueryAll, "viewer");
return user.applications.find(a => a.name === keyOrName);
}
async function listApps(_args: yargs.Arguments): Promise<void> {
const user = await queryGraphQL<User>(graphQLQueryAll, "viewer");
const apps = user.applications
.sort((a, b) => a.name.localeCompare(b.name))
.map(app => [magenta(app.name), app.key]);
logTable([[bold("App Name"), bold("Key")], ...apps]);
}
async function cmdListExperiments(_args: yargs.Arguments, appKey: string): Promise<void> {
const app = await getApp(appKey);
if (app === undefined) {
throw new Error("Application not found");
}
if (app.experiments.length === 0) {
console.log(
"Experiments for app not found. Data is processed in batches and may take a while to be ready. Please try again later."
);
return;
}
for (const experiment of app.experiments) {
const rows: string[][] = [];
const ago = moment(experiment.started).fromNow();
rows.push([bold(magenta(experiment.name)), `Since ${ago}`, "Conversion"]);
const options = experiment.options
.map(o => ({
...o,
result: o.payoff / o.completed
}))
.sort((a, b) => b.result - a.result);
let first = true;
for (const o of options) {
const conversionRate = (Math.floor(o.result * 1000) / 10).toString();
const conversion = first ? bold(conversionRate) : conversionRate;
const name = first ? bold(o.name + " " + star) : o.name;
rows.push([name, dim(`${o.completed} instances`), `${conversion}%`]);
first = false;
}
logTable(rows);
}
}
type PrintTree = string | { [name: string]: PrintTree };
function makePrintTree(outcome: Outcome): PrintTree {
const options = outcome.options;
function convert(t: Tree): any {
if (t.best !== undefined) {
return `${bold(options[t.best])} - epsilon: ${t.eps}`;
}
const { op, neg } = t.op === "lt" ? { op: "<", neg: ">=" } : { op: "=", neg: "!=" };
const result: PrintTree = {};
result[`${t.at} ${op} ${t.v}`] = convert(t.l!);
result[`${t.at} ${neg} ${t.v}`] = convert(t.r!);
return result;
}
return convert(outcome.tree);
}
async function getOutcomesForApp(app: Application): Promise<{ [key: string]: Outcome }> {
const requestOptions = {
method: "GET",
url: outcomesUrl(app.key),
json: true
};
return await request(requestOptions).promise();
}
async function getOutcomesForAppKeyOrFilename(appKey: string): Promise<{ [key: string]: Outcome }> {
let outcomes: { [key: string]: Outcome };
let app: Application | undefined = undefined;
try {
app = await getApp(appKey);
} catch {}
if (app !== undefined) {
outcomes = await getOutcomesForApp(app);
} else if (fs.existsSync(appKey)) {
outcomes = JSON.parse(fs.readFileSync(appKey, "utf-8"));
} else {
throw new Error("Application not found");
}
return outcomes;
}
async function cmdShowTrees(appKey: string): Promise<void> {
const outcomes = await getOutcomesForAppKeyOrFilename(appKey);
for (const experimentKey of Object.getOwnPropertyNames(outcomes)) {
console.log(bold(magenta(experimentKey)));
console.log(treeify.asTree(makePrintTree(outcomes[experimentKey]), true));
}
}
type PickedCounts = {
[experiment: string]: {
[pick: string]: {
best: Count;
others: { [pick: string]: Count };
};
};
};
function relativePayoffForCount(c: Count): number {
return c.payoff / c.completed;
}
type PickAlternative = {
option: string;
payoff: number;
};
type PickStatistics = {
pick: string;
audience: number;
payoff: number;
alternatives: PickAlternative[];
};
type AppStatistics = {
experiments: { [name: string]: PickStatistics[] };
};
function makeAppStatistics(
outcomes: { [key: string]: Outcome },
counts: { [key: string]: ExperimentCounts }
): AppStatistics {
const countsByPick: PickedCounts = {};
for (const experiment of Object.getOwnPropertyNames(counts)) {
const outcome = outcomes[experiment];
if (outcome === undefined) continue;
const cts = counts[experiment];
const optionNames = new Set<string>();
for (const d of cts.data) {
for (const option of Object.getOwnPropertyNames(d.counts)) {
optionNames.add(option);
}
}
countsByPick[experiment] = {};
for (const d of cts.data) {
const pick = lookupBestOption(d.clientContext as any, outcome).option;
if (pick === undefined) {
console.log("no best option found");
continue;
}
let c = countsByPick[experiment][pick];
if (c === undefined) {
c = { best: { completed: 0, payoff: 0 }, others: {} };
countsByPick[experiment][pick] = c;
}
const othersCount = (option: string): Count => {
if (c.others[option] === undefined) {
c.others[option] = { completed: 0, payoff: 0 };
}
return c.others[option];
};
const pickCounts = d.counts[pick];
if (pickCounts === undefined || pickCounts === null) {
const keys = Object.getOwnPropertyNames(d.counts);
let totalPayoff = 0;
for (const k of keys) {
const ck = d.counts[k];
if (ck === null) continue;
totalPayoff += ck.payoff;
}
if (totalPayoff === 0) {
continue;
}
// console.log(`we have counts for ${JSON.stringify(keys)}`);
// console.log(`best pick ${pick} is not in counts for ${contextString}`);
continue;
// throw new Error(`best pick ${pick} is not in counts for ${contextString}`);
}
const relativePayoffForPick = relativePayoffForCount(pickCounts);
c.best.completed += pickCounts.completed;
c.best.payoff += pickCounts.payoff;
const optionsSet = new Set<string>();
let minPayoff = relativePayoffForPick;
for (const option of Object.getOwnPropertyNames(d.counts)) {
const countsForOption = d.counts[option];
if (countsForOption === null) continue;
optionsSet.add(option);
const relativePayoff = relativePayoffForCount(countsForOption);
/*
console.log(
`best: ${option === pick} ${countsForOption.completed} in ${contextString} - ${relativePayoff *
100} ${option}`
);
*/
if (option === pick) {
continue;
}
/*
if (relativePayoff > relativePayoffForPick) {
throw new Error("non-pick has higher payoff than pick");
}
*/
const oc = othersCount(option);
oc.completed += pickCounts.completed;
oc.payoff += relativePayoff * pickCounts.completed;
if (relativePayoff < minPayoff) {
minPayoff = relativePayoff;
}
}
// This is maybe stupid. If we don't have any information
// for an option and a particular context we count it as the
// same as the worst option.
for (const option of Array.from(optionNames)) {
if (optionsSet.has(option) || option === pick) {
continue;
}
const oc = othersCount(option);
oc.completed += pickCounts.completed;
oc.payoff += minPayoff * pickCounts.completed;
}
}
}
const appStatistics: AppStatistics = { experiments: {} };
for (const experiment of Object.getOwnPropertyNames(countsByPick)) {
const stats: PickStatistics[] = [];
let totalCompleted = 0;
for (const pick of Object.getOwnPropertyNames(countsByPick[experiment])) {
totalCompleted += countsByPick[experiment][pick].best.completed;
}
for (const pick of Object.getOwnPropertyNames(countsByPick[experiment])) {
const cts = countsByPick[experiment][pick];
const alternatives: PickAlternative[] = [];
for (const option of Object.getOwnPropertyNames(cts.others)) {
const otherCount = cts.others[option];
if (otherCount.completed !== cts.best.completed) {
throw new Error("we counted completed incorrectly");
}
alternatives.push({ option, payoff: relativePayoffForCount(otherCount) });
}
alternatives.sort((a, b) => b.payoff - a.payoff);
const audience = cts.best.completed / totalCompleted;
const payoff = relativePayoffForCount(cts.best);
stats.push({ pick, audience, payoff, alternatives });
}
stats.sort((a, b) => b.audience - a.audience);
appStatistics.experiments[experiment] = stats;
}
return appStatistics;
}
function formatPercentage(p: number, decimalPlaces: number): string {
return `${(p * 100).toFixed(decimalPlaces)}%`;
}
async function getCountsForApp(app: Application): Promise<{ [key: string]: ExperimentCounts }> {
const body: GetCountsRequest = { appKey: app.key };
const result = await requestWithAuth("getCounts", body);
return result as { [key: string]: ExperimentCounts };
}
async function cmdShowStats(appKey: string, alternatives: boolean): Promise<void> {
const app = await getApp(appKey);
if (app === undefined) {
throw new Error(`App ${appKey} does not exist`);
}
const outcomes = await getOutcomesForApp(app);
const counts = await getCountsForApp(app);
const stats = makeAppStatistics(outcomes, counts);
for (const experiment of Object.getOwnPropertyNames(stats.experiments)) {
const rows: string[][] = [[bold("Option"), bold("Audience"), bold("Payoff")]];
for (const pickStat of stats.experiments[experiment]) {
rows.push([
bold(pickStat.pick + " " + star),
formatPercentage(pickStat.audience, 2),
formatPercentage(pickStat.payoff, 2)
]);
if (!alternatives) {
continue;
}
for (const altStat of pickStat.alternatives) {
rows.push([altStat.option, "", formatPercentage(altStat.payoff, 2)]);
}
}
console.log(bold(magenta(experiment)));
logTable(rows);
}
}
async function graphQL(_args: yargs.Arguments): Promise<void> {
console.log(JSON.stringify(await queryGraphQL<User>(graphQLQueryAll, "viewer"), undefined, 4));
}
function notifyIfCLIUpdatesAvailable() {
try {
const pkg = require("../../package.json");
const notifier = updateNotifier({ pkg });
notifier.notify();
} catch (e) {}
}
async function main(): Promise<void> {
let didSomething = false;
function cmd(p: Promise<void>): void {
p.catch(e => {
console.error(e);
process.exit(1);
});
didSomething = true;
}
const argv = yargs
.usage(`Usage: ${cyan("$0")} <command> ${dim("[options]")}`)
.command(
"signup <email> <password>",
dim("Create a new account"),
ya => ya.positional("email", { type: "string" }).positional("password", { type: "string" }),
args => cmd(cmdSignup(args, args.email, args.password))
)
.command(
"confirm <code>",
dim("Confirm a new account"),
ya => ya.positional("email", { type: "string" }).positional("code", { type: "string" }),
args => cmd(cmdConfirm(args, args.email, args.code))
)
.command(
"login <email> <password>",
dim("Login"),
ya => ya.positional("email", { type: "string" }).positional("password", { type: "string" }),
args => cmd(cmdLogin(args, args.email, args.password))
)
.command(
"new <name>",
dim("Create a new app"),
ya => ya.positional("name", { type: "string" }),
args => cmd(cmdCreateApp(args, args.name))
)
.command(
"ls [key|name]",
dim("List apps or experiments for an app"),
ya => ya.positional("key", { type: "string" }),
args => {
if (args.key !== undefined) {
cmd(cmdListExperiments(args, args.key));
} else {
cmd(listApps(args));
}
}
)
.command(
"trees <key|name|filename>",
dim("Show decision trees for experiments in an app"),
ya => ya.positional("key", { type: "string" }),
args => cmd(cmdShowTrees(args.key))
)
.command(
"stats [--alternatives] <key|name>",
dim("Show statistics"),
ya => ya.positional("key", { type: "string" }).boolean("alternatives"),
args => cmd(cmdShowStats(args.key, args.alternatives))
)
.command("graphql", false, {}, args => cmd(graphQL(args)))
.wrap(yargs.terminalWidth()).argv;
if (!didSomething || argv.help) {
if ((await tryGetUserInfo()) === undefined) {
console.error("You're not logged in to autotune.");
console.error("");
console.error(" Create account: tune signup EMAIL PASSWORD");
console.error(" Log in: tune login EMAIL PASSWORD");
console.error("");
}
yargs.showHelp();
}
notifyIfCLIUpdatesAvailable();
}
function removeWhitespace(data: any): any {
if (Array.isArray(data)) {
return data.map(removeWhitespace);
}
if (typeof data === "string") {
return data.replace(/\s+/g, " ").trim();
}
return data;
}
function logTable(rows: any[], style: "void" | "norc" = "norc") {
rows = removeWhitespace(rows);
console.log(
table(rows, {
border: getBorderCharacters(style),
columnDefault: {
truncate: 80
}
})
);
}
main().catch(e => console.error(e));