-
Notifications
You must be signed in to change notification settings - Fork 30
feat(ForcedDecisions): add forced-decisions APIs to OptimizelyUserContext #233
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 26 commits
8607ff2
e013fea
0b1f517
cd4176c
5c753aa
0fd717b
7e2400f
9cb1d03
ae48a61
6a9a5f0
60ae438
df1c82f
80e5026
979a0e7
d22ad11
a5843c2
f387f89
3557793
8797195
83ef326
b7b980d
280371c
9168430
2bb8937
0df9732
bc3bb03
7566d55
32122b7
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 |
---|---|---|
|
@@ -199,7 +199,23 @@ public function getVariationFromKeyByExperimentId($experimentId, $variationKey); | |
* @return FeatureVariable / null | ||
*/ | ||
public function getFeatureVariableFromKey($featureFlagKey, $variableKey); | ||
|
||
|
||
/** | ||
* Gets the variation associated with experiment or rollout in instance of given feature flag key | ||
* | ||
* @param string Feature flag key | ||
* @param string variation key | ||
* | ||
* @return Variation / null | ||
*/ | ||
public function getFlagVariationByKey($flagKey, $variationKey); | ||
|
||
/** | ||
* Returns map array of Flag key as key and Variations as value | ||
* | ||
* @return array | ||
*/ | ||
public function getFlagVariationsMap(); | ||
Comment on lines
+211
to
+218
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. Implication of these new interface functions to existing clients with custom ProjectConfig? 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. it will simply return flag variations map do i have to add something other then comment above? 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. No. I think it's good as is. |
||
/** | ||
* Determines if given experiment is a feature test. | ||
* | ||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
use Optimizely\Decide\OptimizelyDecisionMessage; | ||
use Optimizely\DecisionService\DecisionService; | ||
use Optimizely\DecisionService\FeatureDecision; | ||
use Optimizely\OptimizelyDecisionContext; | ||
use Optimizely\Entity\Experiment; | ||
use Optimizely\Entity\FeatureVariable; | ||
use Optimizely\Enums\DecisionNotificationTypes; | ||
|
@@ -201,11 +202,15 @@ private function validateUserInputs($attributes, $eventTags = null) | |
} | ||
|
||
/** | ||
* @param DatafileProjectConfig DatafileProjectConfig instance | ||
* @param string Experiment ID | ||
* @param string Variation key | ||
* @param string Flag key | ||
* @param string Rule key | ||
* @param string Rule type | ||
* @param boolean Feature enabled | ||
* @param string User ID | ||
* @param array Associative array of user attributes | ||
* @param DatafileProjectConfig DatafileProjectConfig instance | ||
*/ | ||
protected function sendImpressionEvent($config, $experimentId, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes) | ||
{ | ||
|
@@ -347,22 +352,32 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp | |
$decisionEventDispatched = false; | ||
|
||
// get decision | ||
$decision = $this->_decisionService->getVariationForFeature( | ||
$config, | ||
$featureFlag, | ||
$userId, | ||
$userAttributes, | ||
$decideOptions | ||
); | ||
|
||
$decideReasons = $decision->getReasons(); | ||
$decision = null; | ||
// check forced-decisions first | ||
$context = new OptimizelyDecisionContext($flagKey, $ruleKey); | ||
list($forcedDecisionResponse, $reasons) = $userContext->findValidatedForcedDecision($context); | ||
if ($forcedDecisionResponse) { | ||
$decision = new FeatureDecision(null, $forcedDecisionResponse, FeatureDecision::DECISION_SOURCE_FEATURE_TEST, $decideReasons); | ||
} else { | ||
// regular decision | ||
$decision = $this->_decisionService->getVariationForFeature( | ||
$config, | ||
$featureFlag, | ||
$userContext, | ||
$decideOptions | ||
); | ||
} | ||
$decideReasons = array_merge($decideReasons, $reasons); | ||
$decideReasons = array_merge($decideReasons, $decision->getReasons()); | ||
$variation = $decision->getVariation(); | ||
|
||
if ($variation) { | ||
$variationKey = $variation->getKey(); | ||
$featureEnabled = $variation->getFeatureEnabled(); | ||
$ruleKey = $decision->getExperiment()->getKey(); | ||
$experimentId = $decision->getExperiment()->getId(); | ||
if ($decision->getExperiment()) { | ||
$ruleKey = $decision->getExperiment()->getKey(); | ||
$experimentId = $decision->getExperiment()->getId(); | ||
} | ||
} else { | ||
$variationKey = null; | ||
$ruleKey = null; | ||
|
@@ -687,7 +702,8 @@ public function getVariation($experimentKey, $userId, $attributes = null) | |
return null; | ||
} | ||
|
||
list($variation, $reasons) = $this->_decisionService->getVariation($config, $experiment, $userId, $attributes); | ||
$userContext = $this->createUserContext($userId, $attributes ? $attributes : []); | ||
list($variation, $reasons) = $this->_decisionService->getVariation($config, $experiment, $userContext); | ||
$variationKey = ($variation === null) ? null : $variation->getKey(); | ||
|
||
if ($config->isFeatureExperiment($experiment->getId())) { | ||
|
@@ -815,7 +831,8 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null) | |
} | ||
|
||
$featureEnabled = false; | ||
$decision = $this->_decisionService->getVariationForFeature($config, $featureFlag, $userId, $attributes); | ||
$userContext = $this->createUserContext($userId, $attributes?: []); | ||
mnoman09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$decision = $this->_decisionService->getVariationForFeature($config, $featureFlag, $userContext); | ||
$variation = $decision->getVariation(); | ||
|
||
if ($config->getSendFlagDecisions() && ($decision->getSource() == FeatureDecision::DECISION_SOURCE_ROLLOUT || !$variation)) { | ||
|
@@ -948,8 +965,8 @@ public function getFeatureVariableValueForType( | |
// Error logged in DatafileProjectConfig - getFeatureFlagFromKey | ||
return null; | ||
} | ||
|
||
$decision = $this->_decisionService->getVariationForFeature($config, $featureFlag, $userId, $attributes); | ||
$userContext = $this->createUserContext($userId, $attributes? $attributes : []); | ||
$decision = $this->_decisionService->getVariationForFeature($config, $featureFlag, $userContext); | ||
$variation = $decision->getVariation(); | ||
$experiment = $decision->getExperiment(); | ||
$featureEnabled = $variation !== null ? $variation->getFeatureEnabled() : false; | ||
|
@@ -1124,7 +1141,7 @@ public function getAllFeatureVariables($featureFlagKey, $userId, $attributes = n | |
return null; | ||
} | ||
|
||
$decision = $this->_decisionService->getVariationForFeature($config, $featureFlag, $userId, $attributes); | ||
$decision = $this->_decisionService->getVariationForFeature($config, $featureFlag, $this->createUserContext($userId, $attributes)); | ||
$variation = $decision->getVariation(); | ||
$experiment = $decision->getExperiment(); | ||
$featureEnabled = $variation !== null ? $variation->getFeatureEnabled() : false; | ||
|
@@ -1247,7 +1264,14 @@ private function getFeatureVariableValueFromVariation($featureFlagKey, $variable | |
*/ | ||
public function isValid() | ||
{ | ||
return $this->getConfig() !== null; | ||
if (!$this->getConfig()) { | ||
$this->_logger->log( | ||
Logger::ERROR, | ||
"Optimizely SDK not configured properly yet." | ||
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. Is there a static message for this that can be referenced? |
||
); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -1281,4 +1305,17 @@ protected function validateInputs(array $values, $logLevel = Logger::ERROR) | |
|
||
return $isValid; | ||
} | ||
|
||
/** | ||
* Gets the variation associated with experiment or rollout in instance of given feature flag key | ||
* | ||
* @param string Feature flag key | ||
* @param string variation key | ||
* | ||
* @return Variation / null | ||
*/ | ||
public function getFlagVariationByKey($flagKey, $variationKey) | ||
{ | ||
return $this->getConfig()->getFlagVariationByKey($flagKey, $variationKey); | ||
} | ||
} |
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.
My understanding of closures in PHP suggests this is copying the value of $flagVariations and not referencing it. Not sure if this could provide an edge case where the $flagVariations array is modified outside of this set of functions.