-
Notifications
You must be signed in to change notification settings - Fork 83
feat(api): OnDecision Notification Listener #240
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
272b5e1
Working on on-decision listener.
mfahadahmed 337e9ac
Fix unit tests.
mfahadahmed 96c82c8
Fix unit tests.
mfahadahmed 92e37c8
code refactoring.
mfahadahmed 0e13435
Added unit tests for decision listener.
mfahadahmed 38ba13b
Added unit tests for feature-variable APIs.
mfahadahmed f3edf5b
Implemented new changes in the requirements document.
mfahadahmed dce7a0a
Implemented new changes in the requirements document.
mfahadahmed 341a09b
Address code review comments.
mfahadahmed e61fb00
Merge remote-tracking branch 'origin/master' into fahad/on-decision-l…
mfahadahmed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jordangarcia shouldn't it be DECISION_TYPES? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
/** | ||
* The Optimizely class | ||
|
@@ -304,7 +305,21 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes) | |
return null; | ||
} | ||
|
||
return this.decisionService.getVariation(experimentKey, userId, attributes); | ||
var variationKey = this.decisionService.getVariation(experimentKey, userId, attributes); | ||
this.notificationCenter.sendNotifications( | ||
enums.NOTIFICATION_TYPES.ON_DECISION, | ||
{ | ||
type: DECISION_INFO_TYPES.EXPERIMENT, | ||
userId: userId, | ||
attributes: attributes, | ||
decisionInfo: { | ||
experimentKey: experimentKey, | ||
variationKey: variationKey, | ||
} | ||
} | ||
); | ||
|
||
return variationKey; | ||
} catch (ex) { | ||
this.logger.log(LOG_LEVEL.ERROR, ex.message); | ||
this.errorHandler.handleError(ex); | ||
|
@@ -463,20 +478,43 @@ Optimizely.prototype.isFeatureEnabled = function (featureKey, userId, attributes | |
return false; | ||
} | ||
|
||
var featureEnabled = false; | ||
var decision = this.decisionService.getVariationForFeature(feature, userId, attributes); | ||
var variation = decision.variation; | ||
|
||
if (!!variation) { | ||
featureEnabled = variation.featureEnabled; | ||
if (decision.decisionSource === DECISION_SOURCES.EXPERIMENT) { | ||
// got a variation from the exp, so we track the impression | ||
this._sendImpressionEvent(decision.experiment.key, decision.variation.key, userId, attributes); | ||
} | ||
if (variation.featureEnabled === true) { | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); | ||
return true; | ||
} | ||
} | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); | ||
return false; | ||
|
||
if (!featureEnabled) { | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); | ||
featureEnabled = false; | ||
} else { | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); | ||
} | ||
|
||
var decisionSource = decision.decisionSource || DECISION_SOURCES.ROLLOUT; | ||
var experimentKey = decisionSource === DECISION_SOURCES.EXPERIMENT ? decision.experiment.key : null; | ||
this.notificationCenter.sendNotifications( | ||
enums.NOTIFICATION_TYPES.ON_DECISION, | ||
{ | ||
type: DECISION_INFO_TYPES.FEATURE, | ||
userId: userId, | ||
attributes: attributes, | ||
decisionInfo: { | ||
featureKey: featureKey, | ||
featureEnabled: featureEnabled, | ||
source: decisionSource, | ||
sourceExperimentKey: experimentKey | ||
} | ||
} | ||
); | ||
|
||
return featureEnabled; | ||
} catch (e) { | ||
this.logger.log(LOG_LEVEL.ERROR, e.message); | ||
this.errorHandler.handleError(e); | ||
|
@@ -563,17 +601,44 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK | |
return null; | ||
} | ||
|
||
var featureEnabled = false; | ||
var variableValue = variable.defaultValue; | ||
var decision = this.decisionService.getVariationForFeature(featureFlag, userId, attributes); | ||
var variableValue; | ||
|
||
if (decision.variation !== null) { | ||
variableValue = projectConfig.getVariableValueForVariation(this.configObj, variable, decision.variation, this.logger); | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId)); | ||
featureEnabled = decision.variation.featureEnabled; | ||
if (featureEnabled) { | ||
variableValue = projectConfig.getVariableValueForVariation(this.configObj, variable, decision.variation, this.logger); | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId)); | ||
} else { | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME, featureFlag.key, userId, variableKey)); | ||
} | ||
} else { | ||
variableValue = variable.defaultValue; | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId, variableKey, featureFlag.key)); | ||
} | ||
|
||
return projectConfig.getTypeCastValue(variableValue, variableType, this.logger); | ||
var decisionSource = decision.decisionSource || DECISION_SOURCES.ROLLOUT; | ||
var experimentKey = decisionSource === DECISION_SOURCES.EXPERIMENT ? decision.experiment.key : null; | ||
var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variableType, this.logger); | ||
|
||
this.notificationCenter.sendNotifications( | ||
enums.NOTIFICATION_TYPES.ON_DECISION, | ||
{ | ||
type: DECISION_INFO_TYPES.FEATURE_VARIABLE, | ||
userId: userId, | ||
attributes: attributes, | ||
decisionInfo: { | ||
featureKey: featureKey, | ||
featureEnabled: featureEnabled, | ||
variableKey: variableKey, | ||
variableValue: typeCastedValue, | ||
variableType: variableType, | ||
source: decisionSource, | ||
sourceExperimentKey: experimentKey | ||
} | ||
} | ||
); | ||
return typeCastedValue; | ||
}; | ||
|
||
/** | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are making any change in the existing datafile, please add comments in the PR, why we changed it. Otherwise difficult for reviewers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for all where you changed
true
,Buy me Later