-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.ts
More file actions
36 lines (31 loc) · 1.48 KB
/
helpers.ts
File metadata and controls
36 lines (31 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import seedrandom from 'seedrandom';
import type { Test, Variant } from '../../shared/types';
import { putMetric } from '../utils/cloudwatch';
import { logError } from '../utils/logging';
import type { BanditVariantData } from './banditData';
export function selectRandomVariant<V extends Variant, T extends Test<V>>(test: T): V | undefined {
const randomVariantIndex = Math.floor(Math.random() * test.variants.length);
const randomVariantData = test.variants.length && test.variants[randomVariantIndex];
if (!randomVariantData) {
logError(`Failed to select random variant for bandit test: ${test.name}`);
putMetric('bandit-selection-error');
return;
}
return randomVariantData;
}
export const getRandomNumber = (seed: string, mvtId: number | string = ''): number => {
const rng = seedrandom(mvtId + seed);
return Math.abs(rng.int32());
};
/**
* It's possible for the variants in the bandit data to temporarily not match the variants in a test configuration.
* This can happen if a variant is deleted in the RRCP, and the cached bandit data still references the deleted variant.
* This function filters out any invalid variants.
*/
export const filterValidVariants = <V extends Variant, T extends Test<V>>(
sortedVariantsData: BanditVariantData[],
test: T,
): BanditVariantData[] => {
const validVariantNames = new Set(test.variants.map((v) => v.name));
return sortedVariantsData.filter((v) => validVariantNames.has(v.variantName));
};