From 0c7946f30c5e57ec733fa19c3a4bbd988f778a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Mon, 4 Oct 2021 14:28:27 +0200 Subject: [PATCH] Merge pull request #5329 from overleaf/ab-split-test-version-createdat Add a createdAt field to split test versions GitOrigin-RevId: d7cf8f6af0a6f1102e9e85f283df32c73a51fda4 --- .../Features/SplitTests/SplitTestV2Handler.js | 68 ++++++++++--------- services/web/app/src/models/SplitTest.js | 4 ++ 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/services/web/app/src/Features/SplitTests/SplitTestV2Handler.js b/services/web/app/src/Features/SplitTests/SplitTestV2Handler.js index 7516d6499b..c04129ac89 100644 --- a/services/web/app/src/Features/SplitTests/SplitTestV2Handler.js +++ b/services/web/app/src/Features/SplitTests/SplitTestV2Handler.js @@ -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) { diff --git a/services/web/app/src/models/SplitTest.js b/services/web/app/src/models/SplitTest.js index dc998f6d4d..789b7fb559 100644 --- a/services/web/app/src/models/SplitTest.js +++ b/services/web/app/src/models/SplitTest.js @@ -66,6 +66,10 @@ const VersionSchema = new Schema( required: true, }, variants: [VariantSchema], + createdAt: { + type: Date, + default: Date.now, + }, }, { _id: false } )