Merge pull request #5329 from overleaf/ab-split-test-version-createdat
Add a createdAt field to split test versions GitOrigin-RevId: d7cf8f6af0a6f1102e9e85f283df32c73a51fda4
This commit is contained in:
@@ -10,6 +10,12 @@ const splitTestCache = require('./SplitTestCache')
|
||||
const DEFAULT_VARIANT = 'default'
|
||||
const ALPHA_PHASE = 'alpha'
|
||||
const BETA_PHASE = 'beta'
|
||||
const DEFAULT_ASSIGNMENT = {
|
||||
variant: DEFAULT_VARIANT,
|
||||
analytics: {
|
||||
segmentation: {},
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assignment of a user to a split test.
|
||||
@@ -37,7 +43,7 @@ const BETA_PHASE = 'beta'
|
||||
*/
|
||||
async function getAssignment(userId, splitTestName, options) {
|
||||
if (!userId) {
|
||||
return { variant: 'default', analytics: { segmentation: {} } }
|
||||
return DEFAULT_ASSIGNMENT
|
||||
}
|
||||
const analyticsId = await UserAnalyticsIdCache.get(userId)
|
||||
return _getAssignment(analyticsId, userId, undefined, splitTestName, options)
|
||||
@@ -72,6 +78,31 @@ async function getAssignmentForSession(session, splitTestName, options) {
|
||||
return _getAssignment(analyticsId, userId, session, splitTestName, options)
|
||||
}
|
||||
|
||||
async function assignInLocalsContext(res, userId, splitTestName, options) {
|
||||
const assignment = await getAssignment(userId, splitTestName, options)
|
||||
if (!res.locals.splitTestVariants) {
|
||||
res.locals.splitTestVariants = {}
|
||||
}
|
||||
res.locals.splitTestVariants[splitTestName] = assignment.variant
|
||||
}
|
||||
|
||||
async function assignInLocalsContextForSession(
|
||||
res,
|
||||
session,
|
||||
splitTestName,
|
||||
options
|
||||
) {
|
||||
const assignment = await getAssignmentForSession(
|
||||
session,
|
||||
splitTestName,
|
||||
options
|
||||
)
|
||||
if (!res.locals.splitTestVariants) {
|
||||
res.locals.splitTestVariants = {}
|
||||
}
|
||||
res.locals.splitTestVariants[splitTestName] = assignment.variant
|
||||
}
|
||||
|
||||
async function _getAssignment(
|
||||
analyticsId,
|
||||
userId,
|
||||
@@ -79,6 +110,9 @@ async function _getAssignment(
|
||||
splitTestName,
|
||||
options
|
||||
) {
|
||||
if (!analyticsId && !userId) {
|
||||
return DEFAULT_ASSIGNMENT
|
||||
}
|
||||
const splitTest = await splitTestCache.get(splitTestName)
|
||||
if (splitTest) {
|
||||
const currentVersion = splitTest.getCurrentVersion()
|
||||
@@ -118,37 +152,7 @@ async function _getAssignment(
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
variant: DEFAULT_VARIANT,
|
||||
analytics: {
|
||||
segmentation: {},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async function assignInLocalsContext(res, userId, splitTestName, options) {
|
||||
const assignment = await getAssignment(userId, splitTestName, options)
|
||||
if (!res.locals.splitTestVariants) {
|
||||
res.locals.splitTestVariants = {}
|
||||
}
|
||||
res.locals.splitTestVariants[splitTestName] = assignment.variant
|
||||
}
|
||||
|
||||
async function assignInLocalsContextForSession(
|
||||
res,
|
||||
session,
|
||||
splitTestName,
|
||||
options
|
||||
) {
|
||||
const assignment = await getAssignmentForSession(
|
||||
session,
|
||||
splitTestName,
|
||||
options
|
||||
)
|
||||
if (!res.locals.splitTestVariants) {
|
||||
res.locals.splitTestVariants = {}
|
||||
}
|
||||
res.locals.splitTestVariants[splitTestName] = assignment.variant
|
||||
return DEFAULT_ASSIGNMENT
|
||||
}
|
||||
|
||||
async function _getAssignmentMetadata(analyticsId, userId, splitTest) {
|
||||
|
||||
@@ -66,6 +66,10 @@ const VersionSchema = new Schema(
|
||||
required: true,
|
||||
},
|
||||
variants: [VariantSchema],
|
||||
createdAt: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
},
|
||||
{ _id: false }
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user