-
Notifications
You must be signed in to change notification settings - Fork 19
feat(integrations): Added support for integrations in datafile and evaluation. #350
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
msohailhussain
merged 12 commits into
master
from
yasir/datafile-parsing-audience-evaluation
Nov 7, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
11c17ed
LRU cache implemented
yasirfolio3 85ae342
Adding cache interface.
yasirfolio3 0193ad0
Merge branch 'master' into yasir/lru-cache
msohailhussain 2e2b804
Added support for integrations in datafile and evaluation.
yasirfolio3 e283e8e
fixes.
yasirfolio3 b97aab1
Merge branch 'master' into yasir/datafile-parsing-audience-evaluation
yasirfolio3 1c3a3ec
fixing race condition.
yasirfolio3 4b5cb0c
nit fixes.
yasirfolio3 960fc07
fixes.
yasirfolio3 dc881db
fixes.
yasirfolio3 e9ad97b
1. Audience segments added.
yasirfolio3 67f6d45
Merge branch 'master' into yasir/datafile-parsing-audience-evaluation
msohailhussain 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
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 |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| /**************************************************************************** | ||
| * Copyright 2020-2021, Optimizely, Inc. and contributors * | ||
| * Copyright 2020-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 * | ||
| * https://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, * | ||
|
|
@@ -31,21 +31,24 @@ type OptimizelyUserContext struct { | |
| UserID string `json:"userId"` | ||
| Attributes map[string]interface{} `json:"attributes"` | ||
|
|
||
| qualifiedSegments []string | ||
| optimizely *OptimizelyClient | ||
| forcedDecisionService *pkgDecision.ForcedDecisionService | ||
| mutex *sync.RWMutex | ||
| } | ||
|
|
||
| // returns an instance of the optimizely user context. | ||
| func newOptimizelyUserContext(optimizely *OptimizelyClient, userID string, attributes map[string]interface{}, forcedDecisionService *pkgDecision.ForcedDecisionService) OptimizelyUserContext { | ||
| func newOptimizelyUserContext(optimizely *OptimizelyClient, userID string, attributes map[string]interface{}, qualifiedSegments []string, forcedDecisionService *pkgDecision.ForcedDecisionService) OptimizelyUserContext { | ||
| // store a copy of the provided attributes so it isn't affected by changes made afterwards. | ||
| if attributes == nil { | ||
| attributes = map[string]interface{}{} | ||
| } | ||
| attributesCopy := copyUserAttributes(attributes) | ||
| qualifiedSegmentsCopy := copyQualifiedSegments(qualifiedSegments) | ||
| return OptimizelyUserContext{ | ||
| UserID: userID, | ||
| Attributes: attributesCopy, | ||
| qualifiedSegments: qualifiedSegmentsCopy, | ||
| optimizely: optimizely, | ||
| forcedDecisionService: forcedDecisionService, | ||
| mutex: new(sync.RWMutex), | ||
|
|
@@ -69,6 +72,13 @@ func (o OptimizelyUserContext) GetUserAttributes() map[string]interface{} { | |
| return copyUserAttributes(o.Attributes) | ||
| } | ||
|
|
||
| // GetQualifiedSegments returns qualified segments for Optimizely user context | ||
| func (o *OptimizelyUserContext) GetQualifiedSegments() []string { | ||
| o.mutex.RLock() | ||
| defer o.mutex.RUnlock() | ||
| return copyQualifiedSegments(o.qualifiedSegments) | ||
| } | ||
|
|
||
| func (o OptimizelyUserContext) getForcedDecisionService() *pkgDecision.ForcedDecisionService { | ||
| if o.forcedDecisionService != nil { | ||
| return o.forcedDecisionService.CreateCopy() | ||
|
|
@@ -86,25 +96,40 @@ func (o *OptimizelyUserContext) SetAttribute(key string, value interface{}) { | |
| o.Attributes[key] = value | ||
| } | ||
|
|
||
| // SetQualifiedSegments clears and adds qualified segments for Optimizely user context | ||
| func (o *OptimizelyUserContext) SetQualifiedSegments(qualifiedSegments []string) { | ||
| o.mutex.Lock() | ||
| defer o.mutex.Unlock() | ||
| o.qualifiedSegments = copyQualifiedSegments(qualifiedSegments) | ||
| } | ||
|
|
||
| // IsQualifiedFor returns true if the user is qualified for the given segment name | ||
| func (o *OptimizelyUserContext) IsQualifiedFor(segment string) bool { | ||
| userContext := entities.UserContext{ | ||
| QualifiedSegments: o.GetQualifiedSegments(), | ||
| } | ||
| return userContext.IsQualifiedFor(segment) | ||
| } | ||
|
|
||
| // Decide returns a decision result for a given flag key and a user context, which contains | ||
| // all data required to deliver the flag or experiment. | ||
| func (o *OptimizelyUserContext) Decide(key string, options []decide.OptimizelyDecideOptions) OptimizelyDecision { | ||
| // use a copy of the user context so that any changes to the original context are not reflected inside the decision | ||
| userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.getForcedDecisionService()) | ||
| userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.GetQualifiedSegments(), o.getForcedDecisionService()) | ||
| return o.optimizely.decide(userContextCopy, key, convertDecideOptions(options)) | ||
| } | ||
|
|
||
| // DecideAll returns a key-map of decision results for all active flag keys with options. | ||
| func (o *OptimizelyUserContext) DecideAll(options []decide.OptimizelyDecideOptions) map[string]OptimizelyDecision { | ||
| // use a copy of the user context so that any changes to the original context are not reflected inside the decision | ||
| userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.getForcedDecisionService()) | ||
| userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.GetQualifiedSegments(), o.getForcedDecisionService()) | ||
| return o.optimizely.decideAll(userContextCopy, convertDecideOptions(options)) | ||
| } | ||
|
|
||
| // DecideForKeys returns a key-map of decision results for multiple flag keys and options. | ||
| func (o *OptimizelyUserContext) DecideForKeys(keys []string, options []decide.OptimizelyDecideOptions) map[string]OptimizelyDecision { | ||
| // use a copy of the user context so that any changes to the original context are not reflected inside the decision | ||
| userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.getForcedDecisionService()) | ||
| userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.GetQualifiedSegments(), o.getForcedDecisionService()) | ||
| return o.optimizely.decideForKeys(userContextCopy, keys, convertDecideOptions(options)) | ||
| } | ||
|
|
||
|
|
@@ -160,3 +185,12 @@ func copyUserAttributes(attributes map[string]interface{}) (attributesCopy map[s | |
| } | ||
| return attributesCopy | ||
| } | ||
|
|
||
| func copyQualifiedSegments(qualifiedSegments []string) (qualifiedSegmentsCopy []string) { | ||
|
Contributor
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. need to check offline. |
||
| if qualifiedSegments == nil { | ||
| return nil | ||
| } | ||
| qualifiedSegmentsCopy = make([]string, len(qualifiedSegments)) | ||
| copy(qualifiedSegmentsCopy, qualifiedSegments) | ||
| return qualifiedSegmentsCopy | ||
| } | ||
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
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.
what's the need?
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.
This was forced by linter since we are using
optimizelykeyword to access the package in the sample code.