mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-30 04:21:32 +02:00
New global split test middleware for locals GitOrigin-RevId: b530572f709572663fc3d051f544064bd8804f76
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
const SplitTestV2Handler = require('./SplitTestV2Handler')
|
|
const SplitTestCache = require('./SplitTestCache')
|
|
const LocalsHelper = require('./LocalsHelper')
|
|
const logger = require('@overleaf/logger')
|
|
|
|
function loadAssignmentsInLocals(splitTestNames) {
|
|
return async function (req, res, next) {
|
|
try {
|
|
if (!req.session.cachedSplitTestAssignments) {
|
|
req.session.cachedSplitTestAssignments = {}
|
|
}
|
|
for (const splitTestName of splitTestNames) {
|
|
const splitTest = await SplitTestCache.get(splitTestName)
|
|
if (splitTest) {
|
|
await _loadAssignmentInLocals(splitTest, req.session, res.locals)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.error(
|
|
{ err: error, splitTestNames },
|
|
'Failed to load split test assignments in express locals in middleware'
|
|
)
|
|
}
|
|
next()
|
|
}
|
|
}
|
|
|
|
async function _loadAssignmentInLocals(splitTest, session, locals) {
|
|
const currentVersion = splitTest.getCurrentVersion()
|
|
const cacheKey = `${splitTest.name}-${currentVersion.versionNumber}`
|
|
if (currentVersion.active) {
|
|
const cachedVariant = session.cachedSplitTestAssignments[cacheKey]
|
|
if (cachedVariant) {
|
|
LocalsHelper.setSplitTestVariant(locals, splitTest.name, cachedVariant)
|
|
} else {
|
|
const assignment = await SplitTestV2Handler.promises.getAssignmentForSession(
|
|
session,
|
|
splitTest.name
|
|
)
|
|
session.cachedSplitTestAssignments[cacheKey] = assignment.variant
|
|
LocalsHelper.setSplitTestVariant(
|
|
locals,
|
|
splitTest.name,
|
|
assignment.variant
|
|
)
|
|
}
|
|
} else {
|
|
delete session.cachedSplitTestAssignments[cacheKey]
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
loadAssignmentsInLocals,
|
|
}
|