Skip to content

Commit 761180a

Browse files
committed
fix fsc
1 parent 59a87eb commit 761180a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

lib/core/decision_service/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getVariationKeyFromId,
3333
isActive,
3434
ProjectConfig,
35+
getTrafficAllocation,
3536
} from '../../project_config/project_config';
3637
import { AudienceEvaluator, createAudienceEvaluator } from '../audience_evaluator';
3738
import * as stringValidator from '../../utils/string_value_validator';
@@ -593,7 +594,7 @@ export class DecisionService {
593594
bucketingId: string,
594595
userId: string
595596
): BucketerParams {
596-
let trafficAllocationConfig: TrafficAllocation[] = experiment.trafficAllocation;
597+
let trafficAllocationConfig: TrafficAllocation[] = getTrafficAllocation(configObj, experiment.id);
597598
if (experiment.cmab) {
598599
trafficAllocationConfig = [{
599600
entityId: CMAB_DUMMY_ENTITY_ID,

lib/project_config/project_config.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,32 @@ describe('getVariationKeyFromId', () => {
457457
});
458458
});
459459

460+
describe('getTrafficAllocation', () => {
461+
let testData: Record<string, any>;
462+
let configObj: ProjectConfig;
463+
464+
beforeEach(function() {
465+
testData = cloneDeep(testDatafile.getTestProjectConfig());
466+
configObj = projectConfig.createProjectConfig(cloneDeep(testData) as JSON);
467+
});
468+
469+
it('should retrieve traffic allocation given valid experiment key in getTrafficAllocation', function() {
470+
expect(projectConfig.getTrafficAllocation(configObj, testData.experiments[0].id)).toEqual(
471+
testData.experiments[0].trafficAllocation
472+
);
473+
});
474+
475+
it('should throw error for invalid experient key in getTrafficAllocation', function() {
476+
expect(() => {
477+
projectConfig.getTrafficAllocation(configObj, 'invalidExperimentId');
478+
}).toThrowError(
479+
expect.objectContaining({
480+
baseMessage: INVALID_EXPERIMENT_ID,
481+
params: ['invalidExperimentId'],
482+
})
483+
);
484+
});
485+
});
460486

461487
describe('getVariationIdFromExperimentAndVariationKey', () => {
462488
let testData: Record<string, any>;

lib/project_config/project_config.tests.js

+15
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,21 @@ describe('lib/core/project_config', function() {
402402
);
403403
});
404404

405+
it('should retrieve traffic allocation given valid experiment key in getTrafficAllocation', function() {
406+
assert.deepEqual(
407+
projectConfig.getTrafficAllocation(configObj, testData.experiments[0].id),
408+
testData.experiments[0].trafficAllocation
409+
);
410+
});
411+
412+
it('should throw error for invalid experient key in getTrafficAllocation', function() {
413+
const ex = assert.throws(function() {
414+
projectConfig.getTrafficAllocation(configObj, 'invalidExperimentId');
415+
});
416+
assert.equal(ex.baseMessage, INVALID_EXPERIMENT_ID);
417+
assert.deepEqual(ex.params, ['invalidExperimentId']);
418+
});
419+
405420
describe('#getVariationIdFromExperimentAndVariationKey', function() {
406421
it('should return the variation id for the given experiment key and variation key', function() {
407422
assert.strictEqual(

lib/project_config/project_config.ts

+17
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,22 @@ export const getExperimentFromKey = function(projectConfig: ProjectConfig, exper
559559
throw new OptimizelyError(EXPERIMENT_KEY_NOT_IN_DATAFILE, experimentKey);
560560
};
561561

562+
563+
/**
564+
* Given an experiment id, returns the traffic allocation within that experiment
565+
* @param {ProjectConfig} projectConfig Object representing project configuration
566+
* @param {string} experimentId Id representing the experiment
567+
* @return {TrafficAllocation[]} Traffic allocation for the experiment
568+
* @throws If experiment key is not in datafile
569+
*/
570+
export const getTrafficAllocation = function(projectConfig: ProjectConfig, experimentId: string): TrafficAllocation[] {
571+
const experiment = projectConfig.experimentIdMap[experimentId];
572+
if (!experiment) {
573+
throw new OptimizelyError(INVALID_EXPERIMENT_ID, experimentId);
574+
}
575+
return experiment.trafficAllocation;
576+
};
577+
562578
/**
563579
* Get experiment from provided experiment id. Log an error if no experiment
564580
* exists in the project config with the given ID.
@@ -879,4 +895,5 @@ export default {
879895
isFeatureExperiment,
880896
toDatafile,
881897
tryCreatingProjectConfig,
898+
getTrafficAllocation,
882899
};

0 commit comments

Comments
 (0)