Skip to content
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

feat(Decide): Add Decide API #220

Merged
merged 23 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ php:
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- '7.3.24' # revert it back to 7.3 once 7.3.25 latest build gets fixed.
install: "composer install"
script:
- mkdir -p build/logs
Expand Down
46 changes: 26 additions & 20 deletions src/Optimizely/Bucketer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ protected function generateBucketValue($bucketingKey)
* @param $userId string ID for user.
* @param $parentId mixed ID representing Experiment or Group.
* @param $trafficAllocations array Traffic allocations for variation or experiment.
* @param $decideReasons array Evaluation Logs.
*
* @return string ID representing experiment or variation.
*/
private function findBucket($bucketingId, $userId, $parentId, $trafficAllocations)
private function findBucket($bucketingId, $userId, $parentId, $trafficAllocations, &$decideReasons = [])
{
// Generate the bucketing key based on combination of user ID and experiment ID or group ID.
$bucketingKey = $bucketingId.$parentId;
$bucketingNumber = $this->generateBucketValue($bucketingKey);
$this->_logger->log(Logger::DEBUG, sprintf('Assigned bucket %s to user "%s" with bucketing ID "%s".', $bucketingNumber, $userId, $bucketingId));
$message = sprintf('Assigned bucket %s to user "%s" with bucketing ID "%s".', $bucketingNumber, $userId, $bucketingId);
$this->_logger->log(Logger::DEBUG, $message);
$decideReasons[] = $message;

foreach ($trafficAllocations as $trafficAllocation) {
$currentEnd = $trafficAllocation->getEndOfRange();
Expand All @@ -136,10 +139,11 @@ private function findBucket($bucketingId, $userId, $parentId, $trafficAllocation
* @param $experiment Experiment Experiment or Rollout rule in which user is to be bucketed.
* @param $bucketingId string A customer-assigned value used to create the key for the murmur hash.
* @param $userId string User identifier.
* @param $decideReasons array Evaluation Logs.
*
* @return Variation Variation which will be shown to the user.
*/
public function bucket(ProjectConfigInterface $config, Experiment $experiment, $bucketingId, $userId)
public function bucket(ProjectConfigInterface $config, Experiment $experiment, $bucketingId, $userId, &$decideReasons = [])
{
if (is_null($experiment->getKey())) {
return null;
Expand All @@ -156,32 +160,34 @@ public function bucket(ProjectConfigInterface $config, Experiment $experiment, $

$userExperimentId = $this->findBucket($bucketingId, $userId, $group->getId(), $group->getTrafficAllocation());
if (empty($userExperimentId)) {
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no experiment.', $userId));
$message = sprintf('User "%s" is in no experiment.', $userId);
$this->_logger->log(Logger::INFO, $message);
$decideReasons[] = $message;
return null;
}

if ($userExperimentId != $experiment->getId()) {
$this->_logger->log(
Logger::INFO,
sprintf(
'User "%s" is not in experiment %s of group %s.',
$userId,
$experiment->getKey(),
$experiment->getGroupId()
)
$message = sprintf(
'User "%s" is not in experiment %s of group %s.',
$userId,
$experiment->getKey(),
$experiment->getGroupId()
);

$this->_logger->log(Logger::INFO, $message);
$decideReasons[] = $message;
return null;
}

$this->_logger->log(
Logger::INFO,
sprintf(
'User "%s" is in experiment %s of group %s.',
$userId,
$experiment->getKey(),
$experiment->getGroupId()
)
$message = sprintf(
'User "%s" is in experiment %s of group %s.',
$userId,
$experiment->getKey(),
$experiment->getGroupId()
);

$this->_logger->log(Logger::INFO, $message);
$decideReasons[] = $message;
}

// Bucket user if not in whitelist and in group (if any).
Expand Down
27 changes: 27 additions & 0 deletions src/Optimizely/Decide/OptimizelyDecideOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright 2020, Optimizely
*
* 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.
*/

namespace Optimizely\Decide;

class OptimizelyDecideOption
{
const DISABLE_DECISION_EVENT = 'DISABLE_DECISION_EVENT';
const ENABLED_FLAGS_ONLY = 'ENABLED_FLAGS_ONLY';
const IGNORE_USER_PROFILE_SERVICE = 'IGNORE_USER_PROFILE_SERVICE';
const INCLUDE_REASONS = 'INCLUDE_REASONS';
const EXCLUDE_VARIABLES = 'EXCLUDE_VARIABLES';
}
88 changes: 88 additions & 0 deletions src/Optimizely/Decide/OptimizelyDecision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright 2020, Optimizely
*
* 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.
*/

namespace Optimizely\Decide;

class OptimizelyDecision implements \JsonSerializable
{
private $variationKey;
private $enabled;
private $variables;
private $ruleKey;
private $flagKey;
private $userContext;
private $reasons;


public function __construct(
$variationKey = null,
$enabled = null,
$variables = null,
$ruleKey = null,
$flagKey = null,
$userContext = null,
$reasons = null
) {
$this->variationKey = $variationKey;
$this->enabled = $enabled === null ? false : $enabled;
$this->variables = $variables === null ? [] : $variables;
$this->ruleKey = $ruleKey;
$this->flagKey = $flagKey;
$this->userContext = $userContext;
$this->reasons = $reasons === null ? [] : $reasons;
}

public function getVariationKey()
{
return $this->variationKey;
}

public function getEnabled()
{
return $this->enabled;
}

public function getVariables()
{
return $this->variables;
}

public function getRuleKey()
{
return $this->ruleKey;
}

public function getFlagKey()
{
return $this->flagKey;
}

public function getUserContext()
{
return $this->userContext;
}

public function getReasons()
{
return $this->reasons;
}

public function jsonSerialize()
{
return get_object_vars($this);
}
}
25 changes: 25 additions & 0 deletions src/Optimizely/Decide/OptimizelyDecisionMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright 2020, Optimizely
*
* 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.
*/

namespace Optimizely\Decide;

class OptimizelyDecisionMessage
{
const SDK_NOT_READY = 'Optimizely SDK not configured properly yet.';
const FLAG_KEY_INVALID = 'No flag was found for key "%s".';
const VARIABLE_VALUE_INVALID = 'Variable value for key "%s" is invalid or wrong type.';
}
Loading