-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ODP Datafile Parsing and Audience Evaluation (#765)
- Loading branch information
1 parent
fff21e0
commit e4b5dc9
Showing
17 changed files
with
1,429 additions
and
543 deletions.
There are no files selected for viewing
This file contains 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
307 changes: 276 additions & 31 deletions
307
packages/optimizely-sdk/lib/core/audience_evaluator/index.tests.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
73 changes: 73 additions & 0 deletions
73
...optimizely-sdk/lib/core/audience_evaluator/odp_segment_condition_evaluator/index.tests.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/**************************************************************************** | ||
* Copyright 2022, Optimizely, Inc. and contributors * | ||
* * | ||
* Licensed under the Apache License, Version 2.0 (the "License"); * | ||
* you may not use this file except in compliance with the License. * | ||
* You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, software * | ||
* distributed under the License is distributed on an "AS IS" BASIS, * | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
* See the License for the specific language governing permissions and * | ||
* limitations under the License. * | ||
***************************************************************************/ | ||
import sinon from 'sinon'; | ||
import { assert } from 'chai'; | ||
import { sprintf } from '../../../utils/fns'; | ||
|
||
import { | ||
LOG_LEVEL, | ||
LOG_MESSAGES, | ||
} from '../../../utils/enums'; | ||
import * as logging from '../../../modules/logging'; | ||
import * as odpSegmentEvalutor from './'; | ||
|
||
var odpSegment1Condition = { | ||
"value": "odp-segment-1", | ||
"type": "third_party_dimension", | ||
"name": "odp.audiences", | ||
"match": "qualified" | ||
}; | ||
|
||
var getMockUserContext = (attributes, segments) => ({ | ||
getAttributes: () => ({ ... (attributes || {})}), | ||
isQualifiedFor: segment => segments.indexOf(segment) > -1 | ||
}); | ||
|
||
describe('lib/core/audience_evaluator/odp_segment_condition_evaluator', function() { | ||
var stubLogHandler; | ||
|
||
beforeEach(function() { | ||
stubLogHandler = { | ||
log: sinon.stub(), | ||
}; | ||
logging.setLogLevel('notset'); | ||
logging.setLogHandler(stubLogHandler); | ||
}); | ||
|
||
afterEach(function() { | ||
logging.resetLogger(); | ||
}); | ||
|
||
it('should return true when segment qualifies and known match type is provided', () => { | ||
assert.isTrue(odpSegmentEvalutor.evaluate(odpSegment1Condition, getMockUserContext({}, ['odp-segment-1']))); | ||
}); | ||
|
||
it('should return false when segment does not qualify and known match type is provided', () => { | ||
assert.isFalse(odpSegmentEvalutor.evaluate(odpSegment1Condition, getMockUserContext({}, ['odp-segment-2']))); | ||
}) | ||
|
||
it('should return null when segment qualifies but unknown match type is provided', () => { | ||
const invalidOdpMatchCondition = { | ||
... odpSegment1Condition, | ||
"match": 'unknown', | ||
}; | ||
assert.isNull(odpSegmentEvalutor.evaluate(invalidOdpMatchCondition, getMockUserContext({}, ['odp-segment-1']))); | ||
sinon.assert.calledOnce(stubLogHandler.log); | ||
assert.strictEqual(stubLogHandler.log.args[0][0], LOG_LEVEL.WARNING); | ||
var logMessage = stubLogHandler.log.args[0][1]; | ||
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.UNKNOWN_MATCH_TYPE, 'ODP_SEGMENT_CONDITION_EVALUATOR', JSON.stringify(invalidOdpMatchCondition))); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/optimizely-sdk/lib/core/audience_evaluator/odp_segment_condition_evaluator/index.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/**************************************************************************** | ||
* Copyright 2022 Optimizely, Inc. and contributors * | ||
* * | ||
* Licensed under the Apache License, Version 2.0 (the "License"); * | ||
* you may not use this file except in compliance with the License. * | ||
* You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, software * | ||
* distributed under the License is distributed on an "AS IS" BASIS, * | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
* See the License for the specific language governing permissions and * | ||
* limitations under the License. * | ||
***************************************************************************/ | ||
import { getLogger } from '../../../modules/logging'; | ||
import { Condition, OptimizelyUserContext } from '../../../shared_types'; | ||
|
||
import { LOG_MESSAGES } from '../../../utils/enums'; | ||
|
||
const MODULE_NAME = 'ODP_SEGMENT_CONDITION_EVALUATOR'; | ||
|
||
const logger = getLogger(); | ||
|
||
const QUALIFIED_MATCH_TYPE = 'qualified'; | ||
|
||
const MATCH_TYPES = [ | ||
QUALIFIED_MATCH_TYPE, | ||
]; | ||
|
||
type ConditionEvaluator = (condition: Condition, user: OptimizelyUserContext) => boolean | null; | ||
|
||
const EVALUATORS_BY_MATCH_TYPE: { [conditionType: string]: ConditionEvaluator | undefined } = {}; | ||
EVALUATORS_BY_MATCH_TYPE[QUALIFIED_MATCH_TYPE] = qualifiedEvaluator; | ||
|
||
/** | ||
* Given a custom attribute audience condition and user attributes, evaluate the | ||
* condition against the attributes. | ||
* @param {Condition} condition | ||
* @param {OptimizelyUserContext} user | ||
* @return {?boolean} true/false if the given user attributes match/don't match the given condition, | ||
* null if the given user attributes and condition can't be evaluated | ||
* TODO: Change to accept and object with named properties | ||
*/ | ||
export function evaluate(condition: Condition, user: OptimizelyUserContext): boolean | null { | ||
const conditionMatch = condition.match; | ||
if (typeof conditionMatch !== 'undefined' && MATCH_TYPES.indexOf(conditionMatch) === -1) { | ||
logger.warn(LOG_MESSAGES.UNKNOWN_MATCH_TYPE, MODULE_NAME, JSON.stringify(condition)); | ||
return null; | ||
} | ||
|
||
let evaluator; | ||
if (!conditionMatch) { | ||
evaluator = qualifiedEvaluator; | ||
} else { | ||
evaluator = EVALUATORS_BY_MATCH_TYPE[conditionMatch] || qualifiedEvaluator; | ||
} | ||
|
||
return evaluator(condition, user); | ||
} | ||
|
||
function qualifiedEvaluator(condition: Condition, user: OptimizelyUserContext): boolean { | ||
return user.isQualifiedFor(condition.value as string); | ||
} |
Oops, something went wrong.