Skip to content

Commit

Permalink
Merge pull request #3183 from mozilla/add-experiments-support, r=@chenba
Browse files Browse the repository at this point in the history


Connect with #3170

Unfortunately, I was not able to 100% move the base experiment in content-server over to our fxa-shared project. Please see the linked issue for the different things I tried.

To spare my sanity I opted to update the experiment file to typecript and pull over the same tests from content-server.

This should be enough for me to build out the experiment parts needed in the auth-server and finish #2595.
  • Loading branch information
vbudhram committed Oct 30, 2019
2 parents 96e07fa + c17e6c7 commit cff35c3
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 1 deletion.
84 changes: 84 additions & 0 deletions packages/fxa-shared/experiments/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import md5 = require('js-md5');

class BaseGroupingRule {
public name: string = '';
public deprecated: boolean = false;

/**
* Return a 32 bit hash of `key`
*
* @param {String} key
* @returns {Number}
*/
public hash(key: string): number {
// md5 returns 32 hex bytes, we want 32 bits. 32bits = 8 hex bytes.
const hash = md5(`${this.name}:${key}`).substr(0, 8);
return parseInt(hash, 16);
}

/**
* Return a number in the range [0,1], using `key` as a stable identifier.
* The same number is always returned for the same `key`.
*
* @param {String} key
* @returns {Number}
*/
public luckyNumber(key: string): number {
// hash returns a 32 bit value, divide by 2^32 to
// ensure the number is between 0 and 1.
return this.hash(key) / 0xffffffff;
}

/**
* Decide membership in a trial using `key` as a stable identifier.
*
* @param {Number} percent in the range [0,1]
* @param {String} key
* @returns {Boolean}
*/
public bernoulliTrial(percent: number, key: string): boolean {
return this.luckyNumber(key) <= percent;
}

/**
* Make a uniform choice amongst `choices` using `key` as a stable identifier.
*
* @param {String[]} choices
* @param {String} key
* @returns {String}
*/
public uniformChoice(choices: string[], key: string): string {
return choices[this.hash(key) % choices.length];
}

/**
* Use `subject` data to make a choice.
*
* @param {Object} subject data used to decide
*/
public choose(subject: object): string | boolean {
if (this.deprecated) {
throw new Error(`Experiment deprecated: ${this.name}`);
}
throw new Error('choose must be overridden');
}

/**
* Is this a test email?
*
* @param {String} email
* @returns {Boolean}
*/
public isTestEmail(email: string): boolean {
return (
/.+@softvision\.(com|ro)$/.test(email) ||
/.+@mozilla\.(com|org)$/.test(email)
);
}
}

export default BaseGroupingRule;
22 changes: 22 additions & 0 deletions packages/fxa-shared/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/fxa-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^3.5.3"
"typescript": "^3.5.3",
"underscore": "1.8.3",
"uuid": "^3.3.3"
},
"dependencies": {
"@types/js-md5": "^0.4.2",
"accept-language": "^2.0.17",
"ajv": "^6.10.2",
"bluebird": "^3.5.5",
"celebrate": "^10.0.1",
"cors": "^2.8.5",
"generic-pool": "^3.7.1",
"joi": "^14.3.1",
"js-md5": "^0.7.3",
"moment": "^2.24.0",
"node-uap": "git://github.com/vladikoff/node-uap.git#9cdd16247",
"redis": "^2.8.0"
Expand Down

0 comments on commit cff35c3

Please sign in to comment.