Skip to content

feat: Implement IGNORE_USER_PROFILE_SERVICE flag in decide options #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions packages/optimizely-sdk/lib/core/decision_service/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ export interface DecisionService {

/**
* Gets a decision response containing variation where visitor will be bucketed and the decide reasons.
* @param {ProjectConfig} configObj The parsed project configuration object
* @param {string} experimentKey
* @param {string} userId
* @param {UserAttributes} attributes
* @return {DecisionResponse} DecisionResponse DecisionResponse containing variation
* the user is bucketed into and the decide reasons.
* @param {ProjectConfig} configObj The parsed project configuration object
* @param {string} experimentKey
* @param {string} userId
* @param {UserAttributes} attributes
* @param {[key: string]: boolean} options Optional map of decide options
* @return {DecisionResponse} DecisionResponse DecisionResponse containing variation
* the user is bucketed into and the decide reasons.
*/
getVariation(
configObj: ProjectConfig,
experimentKey: string,
userId: string,
attributes?: UserAttributes
attributes?: UserAttributes,
options?: { [key: string]: boolean }
): DecisionResponse<string | null>;

/**
Expand All @@ -60,19 +62,21 @@ export interface DecisionService {
* experiment properties (both objects), as well as a decisionSource property.
* decisionSource indicates whether the decision was due to a rollout or an
* experiment.
* @param {ProjectConfig} configObj The parsed project configuration object
* @param {FeatureFlag} feature A feature flag object from project configuration
* @param {string} userId A string identifying the user, for bucketing
* @param {unknown} attributes Optional user attributes
* @return {DecisionResponse} DecisionResponse DecisionResponse containing an object with experiment, variation, and decisionSource
* properties and the decide reasons. If the user was not bucketed into a variation, the
* variation property in decision object is null.
* @param {ProjectConfig} configObj The parsed project configuration object
* @param {FeatureFlag} feature A feature flag object from project configuration
* @param {string} userId A string identifying the user, for bucketing
* @param {unknown} attributes Optional user attributes
* @param {[key: string]: boolean} options Optional map of decide options
* @return {DecisionResponse} DecisionResponse DecisionResponse containing an object with experiment, variation, and decisionSource
* properties and the decide reasons. If the user was not bucketed into a variation, the
* variation property in decision object is null.
*/
getVariationForFeature(
configObj: ProjectConfig,
feature: FeatureFlag,
userId: string,
attributes: unknown
attributes: unknown,
options?: { [key: string]: boolean }
): DecisionResponse<DecisionObj>;

/**
Expand Down
92 changes: 51 additions & 41 deletions packages/optimizely-sdk/lib/core/decision_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as enums from '../../utils/enums';
import projectConfig from '../project_config';
import AudienceEvaluator from '../audience_evaluator';
import * as stringValidator from '../../utils/string_value_validator';
import { OptimizelyDecideOptions } from '../../shared_types';

var MODULE_NAME = 'DECISION_SERVICE';
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
Expand Down Expand Up @@ -55,14 +56,15 @@ function DecisionService(options) {

/**
* Gets variation where visitor will be bucketed.
* @param {Object} configObj The parsed project configuration object
* @param {string} experimentKey
* @param {string} userId
* @param {Object} attributes
* @return {Object} DecisionResonse DecisionResonse containing the variation the user is bucketed into
* and the decide reasons.
* @param {Object} configObj The parsed project configuration object
* @param {string} experimentKey
* @param {string} userId
* @param {Object} attributes
* @param {[key: string]: boolean} options Optional map of decide options
* @return {Object} DecisionResonse DecisionResonse containing the variation the user is bucketed into
* and the decide reasons.
*/
DecisionService.prototype.getVariation = function(configObj, experimentKey, userId, attributes) {
DecisionService.prototype.getVariation = function(configObj, experimentKey, userId, attributes, options = {}) {
// by default, the bucketing ID should be the user ID
var bucketingId = this._getBucketingId(userId, attributes);
var decideReasons = [];
Expand Down Expand Up @@ -98,26 +100,30 @@ DecisionService.prototype.getVariation = function(configObj, experimentKey, user
};
}

// check for sticky bucketing
var experimentBucketMap = this.__resolveExperimentBucketMap(userId, attributes);
variation = this.__getStoredVariation(configObj, experiment, userId, experimentBucketMap);
if (variation) {
var returningStoredVariationMessage = sprintf(
LOG_MESSAGES.RETURNING_STORED_VARIATION,
MODULE_NAME,
variation.key,
experimentKey,
userId
);
this.logger.log(
LOG_LEVEL.INFO,
returningStoredVariationMessage
);
decideReasons.push(returningStoredVariationMessage);
return {
result: variation.key,
reasons: decideReasons,
};
var shouldIgnoreUPS = options[OptimizelyDecideOptions.IGNORE_USER_PROFILE_SERVICE];

// check for sticky bucketing if decide options do not include shouldIgnoreUPS
if (!shouldIgnoreUPS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be used to bypass "saveUserProfile" as well.
I see a test case confirming this save bypass. Wondering how the test passed :)

Copy link
Contributor Author

@yavorona yavorona Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe "saveUserProfile" should not be called in this case since we are returning stored variation. I wrapped __saveUserProfile call below in if (!shouldIgnoreUPS) {..} statement and added a unit test to incorporate this comment.

var experimentBucketMap = this.__resolveExperimentBucketMap(userId, attributes);
variation = this.__getStoredVariation(configObj, experiment, userId, experimentBucketMap);
if (variation) {
var returningStoredVariationMessage = sprintf(
LOG_MESSAGES.RETURNING_STORED_VARIATION,
MODULE_NAME,
variation.key,
experimentKey,
userId
);
this.logger.log(
LOG_LEVEL.INFO,
returningStoredVariationMessage
);
decideReasons.push(returningStoredVariationMessage);
return {
result: variation.key,
reasons: decideReasons,
};
}
}

// Perform regular targeting and bucketing
Expand Down Expand Up @@ -174,8 +180,10 @@ DecisionService.prototype.getVariation = function(configObj, experimentKey, user
);
this.logger.log(LOG_LEVEL.INFO, userInVariationLogMessage);
decideReasons.push(userInVariationLogMessage);
// persist bucketing
this.__saveUserProfile(experiment, variation, userId, experimentBucketMap);
// persist bucketing if decide options do not include shouldIgnoreUPS
if (!shouldIgnoreUPS) {
this.__saveUserProfile(experiment, variation, userId, experimentBucketMap);
}

return {
result: variation.key,
Expand Down Expand Up @@ -414,17 +422,18 @@ DecisionService.prototype.__saveUserProfile = function(experiment, variation, us
* experiment properties (both objects), as well as a decisionSource property.
* decisionSource indicates whether the decision was due to a rollout or an
* experiment.
* @param {Object} configObj The parsed project configuration object
* @param {Object} feature A feature flag object from project configuration
* @param {String} userId A string identifying the user, for bucketing
* @param {Object} attributes Optional user attributes
* @return {Object} DecisionResponse DecisionResponse containing an object with experiment, variation, and decisionSource
* properties and decide reasons. If the user was not bucketed into a variation, the variation
* property in decision object is null.
* @param {Object} configObj The parsed project configuration object
* @param {Object} feature A feature flag object from project configuration
* @param {String} userId A string identifying the user, for bucketing
* @param {Object} attributes Optional user attributes
* @param {[key: string]: boolean} options Map of decide options
* @return {Object} DecisionResponse DecisionResponse containing an object with experiment, variation, and decisionSource
* properties and decide reasons. If the user was not bucketed into a variation, the variation
* property in decision object is null.
*/
DecisionService.prototype.getVariationForFeature = function(configObj, feature, userId, attributes) {
DecisionService.prototype.getVariationForFeature = function(configObj, feature, userId, attributes, options = {}) {
var decideReasons = [];
var decisionVariation = this._getVariationForFeatureExperiment(configObj, feature, userId, attributes);
var decisionVariation = this._getVariationForFeatureExperiment(configObj, feature, userId, attributes, options);
decideReasons.push(...decisionVariation.reasons);
var experimentDecision = decisionVariation.result;

Expand Down Expand Up @@ -457,7 +466,8 @@ DecisionService.prototype.getVariationForFeature = function(configObj, feature,
};
};

DecisionService.prototype._getVariationForFeatureExperiment = function(configObj, feature, userId, attributes) {

DecisionService.prototype._getVariationForFeatureExperiment = function(configObj, feature, userId, attributes, options = {}) {
var decideReasons = [];
var experiment = null;
var variationKey = null;
Expand All @@ -468,7 +478,7 @@ DecisionService.prototype._getVariationForFeatureExperiment = function(configObj
if (group) {
experiment = this._getExperimentInGroup(configObj, group, userId);
if (experiment && feature.experimentIds.indexOf(experiment.id) !== -1) {
decisionVariation = this.getVariation(configObj, experiment.key, userId, attributes);
decisionVariation = this.getVariation(configObj, experiment.key, userId, attributes, options);
decideReasons.push(...decisionVariation.reasons);
variationKey = decisionVariation.result;
}
Expand All @@ -478,7 +488,7 @@ DecisionService.prototype._getVariationForFeatureExperiment = function(configObj
// with one experiment, so we look at the first experiment ID only
experiment = projectConfig.getExperimentFromId(configObj, feature.experimentIds[0], this.logger);
if (experiment) {
decisionVariation = this.getVariation(configObj, experiment.key, userId, attributes);
decisionVariation = this.getVariation(configObj, experiment.key, userId, attributes, options);
decideReasons.push(...decisionVariation.reasons);
variationKey = decisionVariation.result;
}
Expand Down
16 changes: 11 additions & 5 deletions packages/optimizely-sdk/lib/core/decision_service/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ describe('lib/core/decision_service', function() {
var userProfileLookupStub;
var userProfileSaveStub;
var fakeDecisionWhitelistedVariation = {
getResult: sinon.stub().returns(null),
getReasons: sinon.stub().returns([])
result: null,
reasons: [],
}
beforeEach(function() {
userProfileServiceInstance = {
Expand Down Expand Up @@ -1364,9 +1364,15 @@ describe('lib/core/decision_service', function() {
decisionSource: DECISION_SOURCES.FEATURE_TEST,
};
assert.deepEqual(decision, expectedDecision);
sinon.assert.calledWithExactly(getVariationStub, configObj, 'testing_my_feature', 'user1', {
test_attribute: 'test_value',
});
sinon.assert.calledWithExactly(
getVariationStub,
configObj,
'testing_my_feature', 'user1',
{
test_attribute: 'test_value',
},
{}
);
});
});

Expand Down
Loading