Comparing version 0.0.7 to 0.0.8
@@ -1,15 +0,20 @@ | ||
export declare type AppState = {}; | ||
export declare function initialize(appKey: string): Promise<AppState>; | ||
export declare type OptionValue = string; | ||
export declare function initialize(appKey: string): Promise<{}>; | ||
export declare function experiment(name: string): Experiment; | ||
export declare class Experiment { | ||
readonly name: String; | ||
value?: any; | ||
constructor(name: String); | ||
setValue(value: any): any; | ||
complete(score?: number): void; | ||
readonly name: string; | ||
readonly bestOption: any; | ||
readonly epsilon: number; | ||
payoff: number; | ||
pick?: OptionValue; | ||
options: OptionValue[]; | ||
key?: string; | ||
constructor(name: string, bestOption?: any, epsilon?: number); | ||
setValueAndStartExperiment(value: any): any; | ||
complete(payoff?: number): Promise<void>; | ||
flipCoin(): boolean; | ||
oneOf<T>(...options: T[]): T; | ||
oneOf<T extends OptionValue>(...options: T[]): T; | ||
} | ||
export declare function flipCoin(experimentName: string): boolean; | ||
export declare function oneOf<T>(experimentName: string, options: T[]): T; | ||
export declare function oneOf<T extends OptionValue>(experimentName: string, options: T[]): T; | ||
export declare function complete(score?: number): void; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var API = "https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod"; | ||
var DEBUG = true; | ||
function api(path) { | ||
return "https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod/" + path; | ||
} | ||
function outcomesUrl(appKey) { | ||
return "https://s3.us-east-2.amazonaws.com/autotune-outcomes/" + appKey + ".json"; | ||
} | ||
var state; | ||
@@ -10,32 +51,135 @@ function log() { | ||
} | ||
console.log.apply(console, ["AUTOTUNE"].concat(args)); | ||
if (DEBUG) { | ||
console.log.apply(console, ["AUTOTUNE"].concat(args)); | ||
} | ||
} | ||
function httpGetAsync(url, data, callback) { | ||
var xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.open("POST", url, true); | ||
xmlhttp.setRequestHeader("Content-Type", "application/json"); | ||
xmlhttp.onreadystatechange = function () { | ||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { | ||
callback(JSON.parse(xmlhttp.responseText)); | ||
function getOwnPropertyValues(x) { | ||
return Object.getOwnPropertyNames(x).map(function (n) { return x[n]; }); | ||
} | ||
function mapObject(x, f) { | ||
var result = {}; | ||
Object.getOwnPropertyNames(x).forEach(function (n) { return (result[n] = f(x[n], n)); }); | ||
return result; | ||
} | ||
function startExperiment(experiment) { | ||
var _this = this; | ||
if (state.startExperimentsTimer !== undefined) { | ||
clearTimeout(state.startExperimentsTimer); | ||
state.startExperimentsTimer = undefined; | ||
} | ||
state.queuedStartedExperiments[experiment.name] = experiment; | ||
state.startExperimentsTimer = window.setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var experiments, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
experiments = mapObject(state.queuedStartedExperiments, function (e) { return ({ | ||
options: e.options, | ||
pick: e.pick, | ||
}); }); | ||
log("Starting experiments", experiments); | ||
state.queuedStartedExperiments = {}; | ||
state.startExperimentsTimer = undefined; | ||
return [4, http("POST", api("/startExperiments"), { | ||
appKey: state.appKey, | ||
experiments: experiments, | ||
})]; | ||
case 1: | ||
response = _a.sent(); | ||
log("/startExperiments response", response); | ||
if (response !== undefined) { | ||
Object.getOwnPropertyNames(response.experiments).forEach(function (name) { | ||
var startedExperiment = state.experiments[name]; | ||
if (startedExperiment !== undefined) { | ||
startedExperiment.key = response.experiments[name].key; | ||
} | ||
}); | ||
} | ||
return [2]; | ||
} | ||
}); | ||
}); }, 100); | ||
} | ||
function completeExperiment(experiment) { | ||
var _this = this; | ||
if (state.completeExperimentsTimer !== undefined) { | ||
clearTimeout(state.completeExperimentsTimer); | ||
state.completeExperimentsTimer = undefined; | ||
} | ||
state.queuedCompletedExperiments[experiment.name] = experiment; | ||
state.completeExperimentsTimer = window.setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var experiments, experimentsByKey; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
experiments = getOwnPropertyValues(state.queuedCompletedExperiments); | ||
state.queuedCompletedExperiments = {}; | ||
state.completeExperimentsTimer = undefined; | ||
experimentsByKey = {}; | ||
experiments.forEach(function (e) { return (experimentsByKey[e.key] = { pick: e.pick, payoff: e.payoff }); }); | ||
log("Completing experiments", experimentsByKey); | ||
return [4, http("POST", api("/completeExperiments"), { | ||
appKey: state.appKey, | ||
experiments: experimentsByKey, | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
return [2]; | ||
} | ||
}); | ||
}); }, 100); | ||
} | ||
function http(method, url, data) { | ||
if (data === void 0) { data = undefined; } | ||
return new Promise(function (resolve, reject) { | ||
var request = new XMLHttpRequest(); | ||
request.open(method, url, true); | ||
request.setRequestHeader("Content-Type", "application/json"); | ||
request.onreadystatechange = function () { | ||
if (request.readyState == 4 && request.status == 200) { | ||
resolve(JSON.parse(request.responseText)); | ||
} | ||
}; | ||
if (data !== undefined) { | ||
request.send(JSON.stringify(data)); | ||
} | ||
}; | ||
xmlhttp.send(JSON.stringify(data)); | ||
else { | ||
request.send(); | ||
} | ||
}); | ||
} | ||
function initialize(appKey) { | ||
return new Promise(function (resolve) { | ||
state = { | ||
appKey: appKey, | ||
experiments: {}, | ||
defaultCompletions: {} | ||
}; | ||
var data = { | ||
appKey: "2e7c3598-62c0-4e90-9d5f-48921bd4ee82", | ||
experiments: { | ||
color: { options: ["red", "green", "blue"], pick: "blue" }, | ||
editor: { options: ["vi", "emacs"], pick: "emacs" } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var outcomes, e_1, experiments; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
log("Initialize", appKey); | ||
outcomes = {}; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4, http("GET", outcomesUrl(appKey))]; | ||
case 2: | ||
outcomes = _a.sent(); | ||
return [3, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
return [3, 4]; | ||
case 4: | ||
log("Got outcomes", outcomes); | ||
experiments = {}; | ||
Object.getOwnPropertyNames(outcomes).forEach(function (name) { | ||
var _a = outcomes[name], bestOption = _a.bestOption, epsilon = _a.epsilon; | ||
experiments[name] = new Experiment(name, bestOption, epsilon); | ||
}); | ||
state = { | ||
appKey: appKey, | ||
experiments: experiments, | ||
defaultCompletions: {}, | ||
queuedCompletedExperiments: {}, | ||
queuedStartedExperiments: {}, | ||
}; | ||
return [2, {}]; | ||
} | ||
}; | ||
httpGetAsync(API + "/startExperiment", data, function (result) { | ||
console.log("Experiment started", result); | ||
resolve({}); | ||
}); | ||
@@ -54,17 +198,28 @@ }); | ||
var Experiment = (function () { | ||
function Experiment(name) { | ||
function Experiment(name, bestOption, epsilon) { | ||
if (bestOption === void 0) { bestOption = undefined; } | ||
if (epsilon === void 0) { epsilon = 1; } | ||
this.name = name; | ||
this.bestOption = bestOption; | ||
this.epsilon = epsilon; | ||
} | ||
Experiment.prototype.setValue = function (value) { | ||
if (this.value === undefined) { | ||
this.value = value; | ||
Experiment.prototype.setValueAndStartExperiment = function (value) { | ||
if (this.pick === undefined) { | ||
this.pick = value; | ||
startExperiment(this); | ||
} | ||
return this.value; | ||
return this.pick; | ||
}; | ||
Experiment.prototype.complete = function (score) { | ||
if (score === void 0) { score = 1; } | ||
log("Completed " + this.name + " with score " + score); | ||
Experiment.prototype.complete = function (payoff) { | ||
if (payoff === void 0) { payoff = 1; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
this.payoff = payoff; | ||
completeExperiment(this); | ||
return [2]; | ||
}); | ||
}); | ||
}; | ||
Experiment.prototype.flipCoin = function () { | ||
return this.oneOf(true, false); | ||
return this.oneOf("true", "false") === "true"; | ||
}; | ||
@@ -76,4 +231,11 @@ Experiment.prototype.oneOf = function () { | ||
} | ||
var one = options[Math.floor(Math.random() * options.length)]; | ||
return this.setValue(one); | ||
this.options = options; | ||
var one; | ||
if (this.bestOption === undefined || Math.random() < this.epsilon) { | ||
one = options[Math.floor(Math.random() * options.length)]; | ||
} | ||
else { | ||
one = this.bestOption; | ||
} | ||
return this.setValueAndStartExperiment(one); | ||
}; | ||
@@ -80,0 +242,0 @@ return Experiment; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var state,API="https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod";function log(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.log.apply(console,["AUTOTUNE"].concat(e))}function httpGetAsync(e,t,n){var o=new XMLHttpRequest;o.open("POST",e,!0),o.setRequestHeader("Content-Type","application/json"),o.onreadystatechange=function(){4==o.readyState&&200==o.status&&n(JSON.parse(o.responseText))},o.send(JSON.stringify(t))}function initialize(e){return new Promise(function(t){state={appKey:e,experiments:{},defaultCompletions:{}};httpGetAsync(API+"/startExperiment",{appKey:"2e7c3598-62c0-4e90-9d5f-48921bd4ee82",experiments:{color:{options:["red","green","blue"],pick:"blue"},editor:{options:["vi","emacs"],pick:"emacs"}}},function(e){console.log("Experiment started",e),t({})})})}function experiment(e){var t=state.experiments[e];return void 0===t&&(t=state.experiments[e]=new Experiment(e)),t}exports.initialize=initialize,exports.experiment=experiment;var Experiment=function(){function e(e){this.name=e}return e.prototype.setValue=function(e){return void 0===this.value&&(this.value=e),this.value},e.prototype.complete=function(e){void 0===e&&(e=1),log("Completed "+this.name+" with score "+e)},e.prototype.flipCoin=function(){return this.oneOf(!0,!1)},e.prototype.oneOf=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=e[Math.floor(Math.random()*e.length)];return this.setValue(n)},e}();function flipCoin(e){var t=experiment(e);return(state.defaultCompletions[e]=t).flipCoin()}function oneOf(e,t){var n=experiment(e);return(state.defaultCompletions[e]=n).oneOf.apply(n,t)}function complete(t){void 0===t&&(t=1);var n=state.defaultCompletions;Object.getOwnPropertyNames(n).forEach(function(e){return n[e].complete(t)})}exports.Experiment=Experiment,exports.flipCoin=flipCoin,exports.oneOf=oneOf,exports.complete=complete; | ||
"use strict";var __awaiter=this&&this.__awaiter||function(o,s,a,p){return new(a||(a=Promise))(function(e,t){function n(e){try{i(p.next(e))}catch(e){t(e)}}function r(e){try{i(p.throw(e))}catch(e){t(e)}}function i(t){t.done?e(t.value):new a(function(e){e(t.value)}).then(n,r)}i((p=p.apply(o,s||[])).next())})},__generator=this&&this.__generator||function(n,r){var i,o,s,e,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return e={next:t(0),throw:t(1),return:t(2)},"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(i)throw new TypeError("Generator is already executing.");for(;a;)try{if(i=1,o&&(s=o[2&t[0]?"return":t[0]?"throw":"next"])&&!(s=s.call(o,t[1])).done)return s;switch(o=0,s&&(t=[0,s.value]),t[0]){case 0:case 1:s=t;break;case 4:return a.label++,{value:t[1],done:!1};case 5:a.label++,o=t[1],t=[0];continue;case 7:t=a.ops.pop(),a.trys.pop();continue;default:if(!(s=0<(s=a.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){a=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3])){a.label=t[1];break}if(6===t[0]&&a.label<s[1]){a.label=s[1],s=t;break}if(s&&a.label<s[2]){a.label=s[2],a.ops.push(t);break}s[2]&&a.ops.pop(),a.trys.pop();continue}t=r.call(n,a)}catch(e){t=[6,e],o=0}finally{i=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}};Object.defineProperty(exports,"__esModule",{value:!0});var state,DEBUG=!0;function api(e){return"https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod/"+e}function outcomesUrl(e){return"https://s3.us-east-2.amazonaws.com/autotune-outcomes/"+e+".json"}function log(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];DEBUG&&console.log.apply(console,["AUTOTUNE"].concat(e))}function getOwnPropertyValues(t){return Object.getOwnPropertyNames(t).map(function(e){return t[e]})}function mapObject(t,n){var r={};return Object.getOwnPropertyNames(t).forEach(function(e){return r[e]=n(t[e],e)}),r}function startExperiment(e){var t=this;void 0!==state.startExperimentsTimer&&(clearTimeout(state.startExperimentsTimer),state.startExperimentsTimer=void 0),state.queuedStartedExperiments[e.name]=e,state.startExperimentsTimer=window.setTimeout(function(){return __awaiter(t,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:return log("Starting experiments",t=mapObject(state.queuedStartedExperiments,function(e){return{options:e.options,pick:e.pick}})),state.queuedStartedExperiments={},state.startExperimentsTimer=void 0,[4,http("POST",api("/startExperiments"),{appKey:state.appKey,experiments:t})];case 1:return log("/startExperiments response",n=e.sent()),void 0!==n&&Object.getOwnPropertyNames(n.experiments).forEach(function(e){var t=state.experiments[e];void 0!==t&&(t.key=n.experiments[e].key)}),[2]}})})},100)}function completeExperiment(e){var t=this;void 0!==state.completeExperimentsTimer&&(clearTimeout(state.completeExperimentsTimer),state.completeExperimentsTimer=void 0),state.queuedCompletedExperiments[e.name]=e,state.completeExperimentsTimer=window.setTimeout(function(){return __awaiter(t,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:return t=getOwnPropertyValues(state.queuedCompletedExperiments),state.queuedCompletedExperiments={},state.completeExperimentsTimer=void 0,n={},t.forEach(function(e){return n[e.key]={pick:e.pick,payoff:e.payoff}}),log("Completing experiments",n),[4,http("POST",api("/completeExperiments"),{appKey:state.appKey,experiments:n})];case 1:return e.sent(),[2]}})})},100)}function http(r,i,o){return void 0===o&&(o=void 0),new Promise(function(e,t){var n=new XMLHttpRequest;n.open(r,i,!0),n.setRequestHeader("Content-Type","application/json"),n.onreadystatechange=function(){4==n.readyState&&200==n.status&&e(JSON.parse(n.responseText))},void 0!==o?n.send(JSON.stringify(o)):n.send()})}function initialize(t){return __awaiter(this,void 0,void 0,function(){var i,o;return __generator(this,function(e){switch(e.label){case 0:log("Initialize",t),i={},e.label=1;case 1:return e.trys.push([1,3,,4]),[4,http("GET",outcomesUrl(t))];case 2:return i=e.sent(),[3,4];case 3:return e.sent(),[3,4];case 4:return log("Got outcomes",i),o={},Object.getOwnPropertyNames(i).forEach(function(e){var t=i[e],n=t.bestOption,r=t.epsilon;o[e]=new Experiment(e,n,r)}),state={appKey:t,experiments:o,defaultCompletions:{},queuedCompletedExperiments:{},queuedStartedExperiments:{}},[2,{}]}})})}function experiment(e){var t=state.experiments[e];return void 0===t&&(t=state.experiments[e]=new Experiment(e)),t}exports.initialize=initialize,exports.experiment=experiment;var Experiment=function(){function e(e,t,n){void 0===t&&(t=void 0),void 0===n&&(n=1),this.name=e,this.bestOption=t,this.epsilon=n}return e.prototype.setValueAndStartExperiment=function(e){return void 0===this.pick&&(this.pick=e,startExperiment(this)),this.pick},e.prototype.complete=function(t){return void 0===t&&(t=1),__awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return this.payoff=t,completeExperiment(this),[2]})})},e.prototype.flipCoin=function(){return"true"===this.oneOf("true","false")},e.prototype.oneOf=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return this.options=t,e=void 0===this.bestOption||Math.random()<this.epsilon?t[Math.floor(Math.random()*t.length)]:this.bestOption,this.setValueAndStartExperiment(e)},e}();function flipCoin(e){var t=experiment(e);return(state.defaultCompletions[e]=t).flipCoin()}function oneOf(e,t){var n=experiment(e);return(state.defaultCompletions[e]=n).oneOf.apply(n,t)}function complete(t){void 0===t&&(t=1);var n=state.defaultCompletions;Object.getOwnPropertyNames(n).forEach(function(e){return n[e].complete(t)})}exports.Experiment=Experiment,exports.flipCoin=flipCoin,exports.oneOf=oneOf,exports.complete=complete; |
{ | ||
"name": "autotune", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Automatically tune JavaScript apps", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31557
281
1