-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
364 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.