Skip to content

feat(decision-listener): Incorporated new decision notification listener changes. #258

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 5 commits into from
Apr 18, 2019
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
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/decision_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ DecisionService.prototype._getVariationForFeatureExperiment = function(configObj
return {
experiment: experiment,
variation: variation,
decisionSource: DECISION_SOURCES.EXPERIMENT,
decisionSource: DECISION_SOURCES.FEATURE_TEST,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ describe('lib/core/decision_service', function() {
'featureEnabled': true,
'key': 'variation'
},
decisionSource: DECISION_SOURCES.EXPERIMENT,
decisionSource: DECISION_SOURCES.FEATURE_TEST,
};
assert.deepEqual(decision, expectedDecision);
sinon.assert.calledWithExactly(mockLogger.log, LOG_LEVEL.DEBUG, 'DECISION_SERVICE: User user1 is in variation variation of experiment testing_my_feature on the feature test_feature_for_experiment.');
Expand Down Expand Up @@ -944,7 +944,7 @@ describe('lib/core/decision_service', function() {
'variables': [],
'key': 'var',
},
decisionSource: DECISION_SOURCES.EXPERIMENT,
decisionSource: DECISION_SOURCES.FEATURE_TEST,
};
assert.deepEqual(decision, expectedDecision);
sinon.assert.calledWithExactly(mockLogger.log, LOG_LEVEL.DEBUG, 'DECISION_SERVICE: User user1 is in variation var of experiment exp_with_group on the feature feature_with_group.');
Expand Down
22 changes: 22 additions & 0 deletions packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,21 @@ module.exports = {

projectConfig.forcedVariationMap = {};

// Object containing experiment Ids that exist in any feature
// for checking that experiment is a feature experiment or not.
projectConfig.experimentFeatureMap = {};

projectConfig.featureKeyMap = fns.keyBy(projectConfig.featureFlags || [], 'key');
fns.forOwn(projectConfig.featureKeyMap, function(feature) {
feature.variableKeyMap = fns.keyBy(feature.variables, 'key');
fns.forEach(feature.experimentIds || [], function(experimentId) {
// Add this experiment in experiment-feature map.
if (projectConfig.experimentFeatureMap[experimentId]) {
projectConfig.experimentFeatureMap[experimentId].push(feature.id);
} else {
projectConfig.experimentFeatureMap[experimentId] = [feature.id];
}

var experimentInFeature = projectConfig.experimentIdMap[experimentId];
if (experimentInFeature.groupId) {
feature.groupId = experimentInFeature.groupId;
Expand Down Expand Up @@ -594,4 +605,15 @@ module.exports = {
eventWithKeyExists: function(projectConfig, eventKey) {
return projectConfig.eventKeyMap.hasOwnProperty(eventKey);
},

/**
*
* @param {Object} projectConfig
* @param {string} experimentId
* @returns {boolean} Returns true if experiment belongs to
* any feature, false otherwise.
*/
isFeatureExperiment: function(projectConfig, experimentId) {
return projectConfig.experimentFeatureMap.hasOwnProperty(experimentId);
}
};
59 changes: 32 additions & 27 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var LOG_MESSAGES = enums.LOG_MESSAGES;
var MODULE_NAME = 'OPTIMIZELY';
var DECISION_SOURCES = enums.DECISION_SOURCES;
var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES;
var DECISION_INFO_TYPES = enums.DECISION_INFO_TYPES;
var DECISION_NOTIFICATION_TYPES = enums.DECISION_NOTIFICATION_TYPES;
var NOTIFICATION_TYPES = enums.NOTIFICATION_TYPES;

var DEFAULT_EVENT_MAX_QUEUE_SIZE = 1;
Expand Down Expand Up @@ -343,10 +343,13 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
}

var variationKey = this.decisionService.getVariation(this.configObj, experimentKey, userId, attributes);
var decisionNotificationType = projectConfig.isFeatureExperiment(this.configObj, experiment.id) ? DECISION_NOTIFICATION_TYPES.FEATURE_TEST :
DECISION_NOTIFICATION_TYPES.AB_TEST;

this.notificationCenter.sendNotifications(
NOTIFICATION_TYPES.DECISION,
{
type: DECISION_INFO_TYPES.EXPERIMENT,
type: decisionNotificationType,
userId: userId,
attributes: attributes || {},
decisionInfo: {
Expand Down Expand Up @@ -501,16 +504,17 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
}

var featureEnabled = false;
var experimentKey = null;
var variationKey = null;
var decision = this.decisionService.getVariationForFeature(this.configObj, feature, userId, attributes);
var variation = decision.variation;

var sourceInfo = {};

if (!!variation) {
featureEnabled = variation.featureEnabled;
if (decision.decisionSource === DECISION_SOURCES.EXPERIMENT) {
experimentKey = decision.experiment.key;
variationKey = decision.variation.key;
if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) {
sourceInfo = {
experimentKey: decision.experiment.key,
variationKey: decision.variation.key,
}
// got a variation from the exp, so we track the impression
this._sendImpressionEvent(decision.experiment.key, decision.variation.key, userId, attributes);
}
Expand All @@ -523,19 +527,20 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
featureEnabled = false;
}

var featureInfo = {
featureKey: featureKey,
featureEnabled: featureEnabled,
source: decision.decisionSource,
sourceInfo: sourceInfo
};

this.notificationCenter.sendNotifications(
enums.NOTIFICATION_TYPES.DECISION,
NOTIFICATION_TYPES.DECISION,
{
type: DECISION_INFO_TYPES.FEATURE,
type: DECISION_NOTIFICATION_TYPES.FEATURE,
userId: userId,
attributes: attributes || {},
decisionInfo: {
featureKey: featureKey,
featureEnabled: featureEnabled,
source: decision.decisionSource,
sourceExperimentKey: experimentKey,
sourceVariationKey: variationKey,
}
decisionInfo: featureInfo,
}
);

Expand Down Expand Up @@ -655,29 +660,29 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
variableKey, featureFlag.key));
}

var experimentKey = null;
var variationKey = null;
if (decision.decisionSource === DECISION_SOURCES.EXPERIMENT) {
experimentKey = decision.experiment.key;
variationKey = decision.variation.key;
var sourceInfo = {};
if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) {
sourceInfo = {
experimentKey: decision.experiment.key,
variationKey: decision.variation.key,
}
}

var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variableType, this.logger);
this.notificationCenter.sendNotifications(
enums.NOTIFICATION_TYPES.DECISION,
NOTIFICATION_TYPES.DECISION,
{
type: DECISION_INFO_TYPES.FEATURE_VARIABLE,
type: DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE,
userId: userId,
attributes: attributes || {},
decisionInfo: {
featureKey: featureKey,
featureEnabled: featureEnabled,
source: decision.decisionSource,
variableKey: variableKey,
variableValue: typeCastedValue,
variableType: variableType,
source: decision.decisionSource,
sourceExperimentKey: experimentKey,
sourceVariationKey: variationKey
sourceInfo: sourceInfo,
}
}
);
Expand Down
Loading