Skip to content

Commit c78424d

Browse files
Merge pull request #9 from optimizely/devel
Merge into master
2 parents df2ceb8 + 4cb1d79 commit c78424d

18 files changed

+1388
-90
lines changed

CONTRIBUTING.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,27 @@ We welcome contributions and feedback! All contributors must sign our [Contribut
1717

1818
##License
1919

20-
By contributing your code, you agree to license your contribution under the terms of the [Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0).
20+
All contributions are under the CLA mentioned above. For this project, Optimizely uses the Apache 2.0 license, and so asks that by contributing your code, you agree to license your contribution under the terms of the [Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0). Your contributions should also include the following header:
21+
22+
```
23+
/**
24+
* Copyright YEAR, Optimizely, Inc. and contributors
25+
*
26+
* Licensed under the Apache License, Version 2.0 (the "License");
27+
* you may not use this file except in compliance with the License.
28+
* You may obtain a copy of the License at
29+
*
30+
* http://www.apache.org/licenses/LICENSE-2.0
31+
*
32+
* Unless required by applicable law or agreed to in writing, software
33+
* distributed under the License is distributed on an "AS IS" BASIS,
34+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35+
* See the License for the specific language governing permissions and
36+
* limitations under the License.
37+
*/
38+
```
39+
40+
The YEAR above should be the year of the contribution. If work on the file has been done over multiple years, list each year in the section above. Example: Optimizely writes the file and releases it in 2014. No changes are made in 2015. Change made in 2016. YEAR should be “2014, 2016”.
2141

2242
##Contact
2343
If you have questions, please contact [email protected].

src/Optimizely/Bucketer.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
* limitations under the License.
1616
*/
1717
namespace Optimizely;
18+
use Monolog\Logger;
1819
use Optimizely\Entity\Experiment;
1920
use Optimizely\Entity\Variation;
21+
use Optimizely\Logger\LoggerInterface;
2022

2123
/**
2224
* Class Bucketer
@@ -45,6 +47,20 @@ class Bucketer
4547
*/
4648
private static $MAX_HASH_VALUE = 0x100000000;
4749

50+
/**
51+
* @var LoggerInterface Logger for logging messages.
52+
*/
53+
private $_logger;
54+
55+
/**
56+
* Bucketer constructor.
57+
* @param LoggerInterface $logger
58+
*/
59+
public function __construct(LoggerInterface $logger)
60+
{
61+
$this->_logger = $logger;
62+
}
63+
4864
/**
4965
* Generate a hash value to be used in determining which variation the user will be put in.
5066
*
@@ -82,8 +98,8 @@ private function findBucket($userId, $parentId, $trafficAllocations)
8298
{
8399
// Generate bucketing ID based on combination of user ID and experiment ID or group ID.
84100
$bucketingId = $userId.$parentId;
85-
86101
$bucketingNumber = $this->generateBucketValue($bucketingId);
102+
$this->_logger->log(Logger::DEBUG, sprintf('Assigned bucket %s to user "%s".', $bucketingNumber, $userId));
87103

88104
forEach ($trafficAllocations as $trafficAllocation)
89105
{
@@ -113,9 +129,15 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $userId)
113129

114130
// Check if user is whitelisted for a variation.
115131
$forcedVariations = $experiment->getForcedVariations();
116-
if (!is_null($forcedVariations) and isset($forcedVariations[$userId])) {
132+
if (!is_null($forcedVariations) && isset($forcedVariations[$userId])) {
117133
$variationKey = $forcedVariations[$userId];
118134
$variation = $config->getVariationFromKey($experiment->getKey(), $variationKey);
135+
if ($variationKey) {
136+
$this->_logger->log(
137+
Logger::INFO,
138+
sprintf('User "%s" is forced in variation "%s".', $userId, $variationKey)
139+
);
140+
}
119141
return $variation;
120142
}
121143

@@ -129,21 +151,37 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $userId)
129151

130152
$userExperimentId = $this->findBucket($userId, $group->getId(), $group->getTrafficAllocation());
131153
if (is_null($userExperimentId)) {
154+
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no experiment.', $userId));
132155
return new Variation();
133156
}
134157

135158
if ($userExperimentId != $experiment->getId()) {
159+
$this->_logger->log(
160+
Logger::INFO,
161+
sprintf('User "%s" is not in experiment %s of group %s.',
162+
$userId, $experiment->getKey(), $experiment->getGroupId()
163+
));
136164
return new Variation();
137165
}
166+
167+
$this->_logger->log(Logger::INFO,
168+
sprintf('User "%s" is in experiment %s of group %s.',
169+
$userId, $experiment->getKey(), $experiment->getGroupId()
170+
));
138171
}
139172

140173
// Bucket user if not in whitelist and in group (if any).
141174
$variationId = $this->findBucket($userId, $experiment->getId(), $experiment->getTrafficAllocation());
142175
if (!is_null($variationId)) {
143176
$variation = $config->getVariationFromId($experiment->getKey(), $variationId);
177+
$this->_logger->log(Logger::INFO,
178+
sprintf('User "%s" is in variation %s of experiment %s.',
179+
$userId, $variation->getKey(), $experiment->getKey()
180+
));
144181
return $variation;
145182
}
146183

184+
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no variation.', $userId));
147185
return new Variation();
148186
}
149187
}

src/Optimizely/Entity/Audience.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Audience
3434
*/
3535
private $_conditions;
3636

37+
/**
38+
* @var array De-serialized audience conditions
39+
*/
40+
private $_conditionsList;
41+
3742

3843
public function __construct($id = null, $name = null, $conditions = null) {
3944
$this->_id = $id;
@@ -88,4 +93,20 @@ public function setConditions($conditions)
8893
{
8994
$this->_conditions = $conditions;
9095
}
96+
97+
/**
98+
* @return array De-serialized audience conditions.
99+
*/
100+
public function getConditionsList()
101+
{
102+
return $this->_conditionsList;
103+
}
104+
105+
/**
106+
* @param $conditionsList array De-serialized audience conditions.
107+
*/
108+
public function setConditionsList($conditionsList)
109+
{
110+
$this->_conditionsList = $conditionsList;
111+
}
91112
}

src/Optimizely/Entity/Experiment.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
class Experiment
2323
{
24+
/**
25+
* @const string String denoting running state of experiment.
26+
*/
27+
const STATUS_RUNNING = 'Running';
28+
29+
/**
30+
* @const string String denoting policy of mutually exclusive group.
31+
*/
32+
const MUTEX_GROUP_POLICY = 'random';
33+
2434
/**
2535
* @var string Experiment ID.
2636
*/
@@ -204,7 +214,7 @@ public function getForcedVariations()
204214
*/
205215
public function setForcedVariations($forcedVariations)
206216
{
207-
$this->_forcedVariations = get_object_vars($forcedVariations);
217+
$this->_forcedVariations = $forcedVariations;
208218
}
209219

210220
/**
@@ -262,6 +272,28 @@ public function setTrafficAllocation($trafficAllocation)
262272
*/
263273
public function isInMutexGroup()
264274
{
265-
return !is_null($this->_groupPolicy) && $this->_groupPolicy == 'random';
275+
return !is_null($this->_groupPolicy) && $this->_groupPolicy == self::MUTEX_GROUP_POLICY;
276+
}
277+
278+
/**
279+
* Determine if experiment is running or not.
280+
*
281+
* @return boolean True if experiment has status "Running". False otherwise.
282+
*/
283+
public function isExperimentRunning()
284+
{
285+
return !is_null($this->_status) && $this->_status == self::STATUS_RUNNING;
286+
}
287+
288+
/**
289+
* Determine if user is in forced variation of experiment.
290+
*
291+
* @param $userId string ID of the user.
292+
* @return boolean True if user is in forced variation of experiment. False otherwise.
293+
*/
294+
public function isUserInForcedVariation($userId)
295+
{
296+
$forcedVariations = $this->getForcedVariations();
297+
return !is_null($forcedVariations) && isset($forcedVariations[$userId]);
266298
}
267299
}

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class EventBuilder
3838
/**
3939
* @var string URL to send impression event to.
4040
*/
41-
private static $IMPRESSION_ENDPOINT = 'https://p13nlog.dz.optimizely.com/log/decision';
41+
private static $IMPRESSION_ENDPOINT = 'https://logx.optimizely.com/log/decision';
4242

4343
/**
4444
* @var string URL to send conversion event to.
4545
*/
46-
private static $CONVERSION_ENDPOINT = 'https://p13nlog.dz.optimizely.com/log/event';
46+
private static $CONVERSION_ENDPOINT = 'https://logx.optimizely.com/log/event';
4747

4848
/**
4949
* @var string HTTP method to be used when making call to log endpoint.

0 commit comments

Comments
 (0)