Merge pull request #4001 from overleaf/ab-hash-split-test

Add hashing to compute the user's percentile in a split test

GitOrigin-RevId: 94302cd1c0ab9e9075a506096b4f36ba50b9b7fa
This commit is contained in:
Alexandre Bourdin 2021-05-06 16:07:54 +02:00 committed by Copybot
parent 8faae673b4
commit 87b3654bff
3 changed files with 15 additions and 5 deletions

View file

@ -30,6 +30,7 @@ const MONTH_NAMES = [
'November',
'December',
]
const EXAMPLE_PROJECT_SPLITTEST_ID = 'example-project-v2'
async function createBlankProject(ownerId, projectName, attributes = {}) {
const isImport = attributes && attributes.overleaf
@ -72,7 +73,7 @@ async function createExampleProject(ownerId, projectName) {
const testSegmentation = SplitTestHandler.getTestSegmentation(
ownerId,
'example-project'
EXAMPLE_PROJECT_SPLITTEST_ID
)
if (testSegmentation.variant === 'example-frog') {
@ -84,7 +85,7 @@ async function createExampleProject(ownerId, projectName) {
if (testSegmentation.enabled) {
AnalyticsManager.recordEvent(ownerId, 'project-created', {
projectId: project._id,
splitTestId: 'example-project',
splitTestId: EXAMPLE_PROJECT_SPLITTEST_ID,
splitTestVariantId: testSegmentation.variant,
})
} else {

View file

@ -1,6 +1,6 @@
const Settings = require('settings-sharelatex')
const _ = require('lodash')
const { ObjectId } = require('mongodb')
const crypto = require('crypto')
const OError = require('@overleaf/o-error')
const ACTIVE_SPLIT_TESTS = []
@ -34,7 +34,7 @@ for (const splitTest of Settings.splitTests) {
function getTestSegmentation(userId, splitTestId) {
const splitTest = _.find(ACTIVE_SPLIT_TESTS, ['id', splitTestId])
if (splitTest) {
let userIdAsPercentile = (ObjectId(userId).getTimestamp() / 1000) % 100
let userIdAsPercentile = _getPercentile(userId, splitTestId)
for (const variant of splitTest.variants) {
if (userIdAsPercentile < variant.rolloutPercent) {
return {
@ -55,6 +55,15 @@ function getTestSegmentation(userId, splitTestId) {
}
}
function _getPercentile(userId, splitTestId) {
const hash = crypto
.createHash('md5')
.update(userId + splitTestId)
.digest('hex')
const hashPrefix = hash.substr(0, 8)
return Math.floor((parseInt(hashPrefix, 16) / 0xffffffff) * 100)
}
module.exports = {
getTestSegmentation,
}

View file

@ -238,7 +238,7 @@ module.exports = settings =
splitTests: [
{
id: 'example-project'
id: 'example-project-v2'
active: process.env['SPLITTEST_EXAMPLE_PROJECT_ACTIVE'] == 'true'
variants: [
{