-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Added a new getAllFeatureVariables Api #470
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cd8bd90
implemented the api. decision notification listeners pending.
zashraf1985 be1eb74
added notifications and its tests
zashraf1985 a6165c6
fixed a spelling mistake
zashraf1985 b1a924e
Merge branch 'master' into zeeshan/get-all-feature-variables
zashraf1985 018bcca
refactored to remove redundant code and simplified logic
zashraf1985 41e9a8d
Added types of both methods
fayyazarshad 3e29a4f
added null defaults
fayyazarshad 906e4c8
incorporated review feedback
zashraf1985 05a00d9
Merge branch 'master' into zeeshan/get-all-feature-variables
zashraf1985 dafdf8f
added comments to the helper method
zashraf1985 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 |
---|---|---|
|
@@ -744,33 +744,78 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK | |
return null; | ||
} | ||
|
||
if (!variableType) { | ||
variableType = variable.type; | ||
} else if (variable.type !== variableType) { | ||
if (variableType && variable.type !== variableType) { | ||
this.logger.log( | ||
LOG_LEVEL.WARNING, | ||
sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type) | ||
); | ||
return null; | ||
} | ||
|
||
var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes); | ||
var featureEnabled = decision.variation !== null ? decision.variation.featureEnabled : false; | ||
var variableValue = this._getFeatureVariableValueFromVariation(featureKey, featureEnabled, decision.variation, variable, userId); | ||
|
||
var featureEnabled = false; | ||
var variableValue = variable.defaultValue; | ||
var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes); | ||
var sourceInfo = {}; | ||
if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) { | ||
sourceInfo = { | ||
experimentKey: decision.experiment.key, | ||
variationKey: decision.variation.key, | ||
}; | ||
} | ||
|
||
this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.DECISION, { | ||
type: DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE, | ||
userId: userId, | ||
attributes: attributes || {}, | ||
decisionInfo: { | ||
featureKey: featureKey, | ||
featureEnabled: featureEnabled, | ||
source: decision.decisionSource, | ||
variableKey: variableKey, | ||
variableValue: variableValue, | ||
variableType: variable.type, | ||
sourceInfo: sourceInfo, | ||
}, | ||
}); | ||
return variableValue; | ||
}; | ||
|
||
/** | ||
* Helper method to get the non type-casted value for a variable attached to a | ||
* feature flag. Returns appropriate variable value depending on whether there | ||
* was a matching variation, feature was enabled or not or varible was part of the | ||
* available variation or not. Also logs the appropriate message explaining how it | ||
* evaluated the value of the variable. | ||
* | ||
* @param {string} featureKey Key of the feature whose variable's value is | ||
* being accessed | ||
* @param {boolean} featureEnabled Boolean indicating if feature is enabled or not | ||
* @param {object} variation variation returned by decision service | ||
* @param {object} variable varible whose value is being evaluated | ||
* @param {string} userId ID for the user | ||
* @return {string|null} String value of the variable or null if the config Obj | ||
* is null | ||
*/ | ||
Optimizely.prototype._getFeatureVariableValueFromVariation = function(featureKey, featureEnabled, variation, variable, userId) { | ||
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. Can you add a doc comment for 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. Done |
||
var configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
return null; | ||
} | ||
|
||
if (decision.variation !== null) { | ||
featureEnabled = decision.variation.featureEnabled; | ||
var value = projectConfig.getVariableValueForVariation(configObj, variable, decision.variation, this.logger); | ||
var variableValue = variable.defaultValue; | ||
if (variation !== null) { | ||
var value = projectConfig.getVariableValueForVariation(configObj, variable, variation, this.logger); | ||
if (value !== null) { | ||
if (featureEnabled === true) { | ||
if (featureEnabled) { | ||
variableValue = value; | ||
this.logger.log( | ||
LOG_LEVEL.INFO, | ||
sprintf( | ||
LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, | ||
MODULE_NAME, | ||
variableKey, | ||
featureFlag.key, | ||
variable.key, | ||
featureKey, | ||
variableValue, | ||
userId | ||
) | ||
|
@@ -781,9 +826,9 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK | |
sprintf( | ||
LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE, | ||
MODULE_NAME, | ||
featureFlag.key, | ||
featureKey, | ||
userId, | ||
variableKey | ||
variable.key | ||
) | ||
); | ||
} | ||
|
@@ -793,8 +838,8 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK | |
sprintf( | ||
LOG_MESSAGES.VARIABLE_NOT_USED_RETURN_DEFAULT_VARIABLE_VALUE, | ||
MODULE_NAME, | ||
variableKey, | ||
decision.variation.key | ||
variable.key, | ||
variation.key | ||
) | ||
); | ||
} | ||
|
@@ -805,37 +850,14 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK | |
LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, | ||
MODULE_NAME, | ||
userId, | ||
variableKey, | ||
featureFlag.key | ||
variable.key, | ||
featureKey | ||
) | ||
); | ||
} | ||
|
||
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(NOTIFICATION_TYPES.DECISION, { | ||
type: DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE, | ||
userId: userId, | ||
attributes: attributes || {}, | ||
decisionInfo: { | ||
featureKey: featureKey, | ||
featureEnabled: featureEnabled, | ||
source: decision.decisionSource, | ||
variableKey: variableKey, | ||
variableValue: typeCastedValue, | ||
variableType: variableType, | ||
sourceInfo: sourceInfo, | ||
}, | ||
}); | ||
return typeCastedValue; | ||
}; | ||
|
||
return projectConfig.getTypeCastValue(variableValue, variable.type, this.logger); | ||
} | ||
|
||
/** | ||
* Returns value for the given boolean variable attached to the given feature | ||
|
@@ -957,6 +979,73 @@ Optimizely.prototype.getFeatureVariableJson = function(featureKey, variableKey, | |
} | ||
}; | ||
|
||
/** | ||
* Returns values for all the variables attached to the given feature | ||
* flag. | ||
* @param {string} featureKey Key of the feature whose variables are being | ||
* accessed | ||
* @param {string} userId ID for the user | ||
* @param {Object} attributes Optional user attributes | ||
* @return {object|null} Object containing all the variables, or null if the | ||
* feature key is invalid | ||
*/ | ||
Optimizely.prototype.getAllFeatureVariables = function(featureKey, userId, attributes) { | ||
try { | ||
if (!this.__isValidInstance()) { | ||
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getAllFeatureVariables')); | ||
return null; | ||
} | ||
|
||
if (!this.__validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) { | ||
return null; | ||
} | ||
|
||
var configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
return null; | ||
} | ||
|
||
var featureFlag = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger); | ||
if (!featureFlag) { | ||
return null; | ||
} | ||
|
||
var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes); | ||
var featureEnabled = decision.variation !== null ? decision.variation.featureEnabled : false; | ||
var allVariables = {}; | ||
|
||
featureFlag.variables.forEach(function (variable) { | ||
allVariables[variable.key] = this._getFeatureVariableValueFromVariation(featureKey, featureEnabled, decision.variation, variable, userId); | ||
}.bind(this)); | ||
|
||
var sourceInfo = {}; | ||
if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) { | ||
sourceInfo = { | ||
experimentKey: decision.experiment.key, | ||
variationKey: decision.variation.key, | ||
}; | ||
} | ||
this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.DECISION, { | ||
type: DECISION_NOTIFICATION_TYPES.ALL_FEATURE_VARIABLES, | ||
userId: userId, | ||
attributes: attributes || {}, | ||
decisionInfo: { | ||
featureKey: featureKey, | ||
featureEnabled: featureEnabled, | ||
source: decision.decisionSource, | ||
variableValues: allVariables, | ||
sourceInfo: sourceInfo, | ||
}, | ||
}); | ||
|
||
return allVariables; | ||
} catch (e) { | ||
this.logger.log(LOG_LEVEL.ERROR, e.message); | ||
this.errorHandler.handleError(e); | ||
return null; | ||
} | ||
}; | ||
|
||
/** | ||
* Returns OptimizelyConfig object containing experiments and features data | ||
* @return {Object} | ||
|
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.
👍 Good catch updating this! Let's use
unknown
for the return value of these, instead ofany
.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.
Done