-
Notifications
You must be signed in to change notification settings - Fork 83
feat (audiences): Audience combinations #175
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
Changes from all commits
066357b
1c4e2dd
4be7e53
a861ea6
e84a5e1
f1dcb2d
e21e4a6
8a5d595
34cc8ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,36 +13,45 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
var conditionEvaluator = require('../condition_evaluator'); | ||
var conditionTreeEvaluator = require('../condition_tree_evaluator'); | ||
var customAttributeConditionEvaluator = require('../custom_attribute_condition_evaluator'); | ||
|
||
module.exports = { | ||
/** | ||
* Determine if the given user attributes satisfy the given audience conditions | ||
* @param {Object[]} audiences Audiences to match the user attributes against | ||
* @param {Object[]} audiences.conditions Audience conditions to match the user attributes against | ||
* @param {Object} [userAttributes] Hash representing user attributes which will be used in | ||
* determining if the audience conditions are met. If not | ||
* provided, defaults to an empty object. | ||
* @return {Boolean} True if the user attributes match the given audience conditions | ||
* @param {Array|String|null|undefined} audienceConditions Audience conditions to match the user attributes against - can be an array | ||
* of audience IDs, a nested array of conditions, or a single leaf condition. | ||
* Examples: ["5", "6"], ["and", ["or", "1", "2"], "3"], "1" | ||
* @param {Object} audiencesById Object providing access to full audience objects for audience IDs | ||
* contained in audienceConditions. Keys should be audience IDs, values | ||
* should be full audience objects with conditions properties | ||
* @param {Object} [userAttributes] User attributes which will be used in determining if audience conditions | ||
* are met. If not provided, defaults to an empty object | ||
* @return {Boolean} true if the user attributes match the given audience conditions, false | ||
* otherwise | ||
*/ | ||
evaluate: function(audiences, userAttributes) { | ||
evaluate: function(audienceConditions, audiencesById, userAttributes) { | ||
// if there are no audiences, return true because that means ALL users are included in the experiment | ||
if (!audiences || audiences.length === 0) { | ||
if (!audienceConditions || audienceConditions.length === 0) { | ||
return true; | ||
} | ||
|
||
if (!userAttributes) { | ||
userAttributes = {}; | ||
} | ||
|
||
for (var i = 0; i < audiences.length; i++) { | ||
var audience = audiences[i]; | ||
var conditions = audience.conditions; | ||
if (conditionEvaluator.evaluate(conditions, userAttributes)) { | ||
return true; | ||
var evaluateConditionWithUserAttributes = function(condition) { | ||
return customAttributeConditionEvaluator.evaluate(condition, userAttributes); | ||
}; | ||
nchilada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var evaluateAudience = function(audienceId) { | ||
var audience = audiencesById[audienceId]; | ||
if (audience) { | ||
return conditionTreeEvaluator.evaluate(audience.conditions, evaluateConditionWithUserAttributes); | ||
} | ||
} | ||
return null; | ||
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. @nchilada @mikeng13 Any thoughts on returning 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 believe returning In the design doc I'd written:
but I don't mind if someone want to propose that we consistently check for invalid audience references during initialization. @mikeng13 @thomaszurkan-optimizely what do you think? |
||
}; | ||
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. Nit: for symmetry with 🔭:architecture_👨🚀:🤔 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. Interesting. This does feel cleaner. I could see the other code fitting into 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. With this approach, 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. Up to you. I was just thinking out loud, especially if there was interest in using the |
||
|
||
return false; | ||
return conditionTreeEvaluator.evaluate(audienceConditions, evaluateAudience) || false; | ||
}, | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.