Skip to content

Commit

Permalink
Merge pull request #69 from flagship-io/update_usage_hit_allocation
Browse files Browse the repository at this point in the history
update ANALYTIC_HIT_ALLOCATION
  • Loading branch information
Heihokon committed Feb 29, 2024
2 parents 522bba7 + a41d6ef commit 1172f2e
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/Api/BatchingCachingStrategyAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Flagship\Config\FlagshipConfig;
use Flagship\Enum\FlagshipConstant;
use Flagship\Enum\HitCacheFields;
use Flagship\Enum\LogLevel;
use Flagship\Enum\TroubleshootingLabel;
use Flagship\Hit\Activate;
use Flagship\Hit\ActivateBatch;
Expand Down Expand Up @@ -341,7 +342,7 @@ protected function sendActivateHit()
$this->cacheHit($this->activatePoolQueue);
$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::SEND_ACTIVATE_HIT_ROUTE_ERROR)
->setLogLevel("ERROR")
->setLogLevel(LogLevel::ERROR)
->setVisitorId($this->flagshipInstanceId)
->setFlagshipInstanceId($this->flagshipInstanceId)
->setTraffic(100)
Expand Down Expand Up @@ -468,7 +469,7 @@ public function sendBatch()
$this->cacheHit($this->hitsPoolQueue);
$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::SEND_BATCH_HIT_ROUTE_RESPONSE_ERROR)
->setLogLevel("ERROR")
->setLogLevel(LogLevel::ERROR)
->setVisitorId($this->flagshipInstanceId)
->setFlagshipInstanceId($this->flagshipInstanceId)
->setTraffic(100)
Expand Down
35 changes: 35 additions & 0 deletions src/Api/NoBatchingContinuousCachingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Flagship\Api;

use Flagship\Enum\FlagshipConstant;
use Flagship\Enum\LogLevel;
use Flagship\Enum\TroubleshootingLabel;
use Flagship\Hit\Activate;
use Flagship\Hit\ActivateBatch;
use Flagship\Hit\UsageHit;
Expand Down Expand Up @@ -77,6 +79,22 @@ protected function sendHit(HitAbstract $hit)
[FlagshipConstant::SEND_HIT,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, $header, $this->getNow() - $now)]
);
$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::SEND_HIT_ROUTE_ERROR)
->setLogLevel(LogLevel::ERROR)
->setVisitorId($this->flagshipInstanceId)
->setFlagshipInstanceId($this->flagshipInstanceId)
->setTraffic(100)
->setConfig($this->config)
->setHttpRequestBody($requestBody)
->setHttpRequestHeaders($header)
->setHttpRequestMethod("POST")
->setHttpRequestUrl($url)
->setHttpResponseBody($exception->getMessage())
->setHttpResponseTime($this->getNow() - $now)
;
$this->addTroubleshootingHit($troubleshooting);
$this->sendTroubleshootingQueue();
}
}

Expand Down Expand Up @@ -119,6 +137,23 @@ public function activateFlag(Activate $hit)
[FlagshipConstant::SEND_ACTIVATE,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, $headers, $this->getNow() - $now)]
);

$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::SEND_ACTIVATE_HIT_ROUTE_ERROR)
->setLogLevel(LogLevel::ERROR)
->setVisitorId($this->flagshipInstanceId)
->setFlagshipInstanceId($this->flagshipInstanceId)
->setTraffic(100)
->setConfig($this->config)
->setHttpRequestBody($requestBody)
->setHttpRequestHeaders($headers)
->setHttpRequestMethod("POST")
->setHttpRequestUrl($url)
->setHttpResponseBody($exception->getMessage())
->setHttpResponseTime($this->getNow() - $now)
;
$this->addTroubleshootingHit($troubleshooting);
$this->sendTroubleshootingQueue();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Decision/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Flagship\Enum\FlagshipConstant;
use Flagship\Enum\FlagshipField;
use Flagship\Enum\LogLevel;
use Flagship\Enum\TroubleshootingLabel;
use Flagship\Hit\Troubleshooting;
use Flagship\Model\TroubleshootingData;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function getCampaigns(VisitorAbstract $visitor)

$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::GET_CAMPAIGNS_ROUTE_RESPONSE_ERROR)
->setLogLevel("ERROR")
->setLogLevel(LogLevel::ERROR)
->setVisitorId($visitor->getVisitorId())
->setAnonymousId($visitor->getAnonymousId())
->setVisitorContext($visitor->getContext())
Expand Down
10 changes: 4 additions & 6 deletions src/Decision/BucketingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct(HttpClientInterface $httpClient, BucketingConfig $co
*/
protected function sendContext(VisitorAbstract $visitor)
{
if (count($visitor->getContext()) <= self::NB_MIN_CONTEXT_KEYS) {
if (count($visitor->getContext()) <= self::NB_MIN_CONTEXT_KEYS || !$visitor->hasConsented()) {
return;
}

Expand Down Expand Up @@ -171,7 +171,7 @@ protected function getBucketingFile()
$troubleshooting->setLabel(TroubleshootingLabel::SDK_BUCKETING_FILE_ERROR)
->setFlagshipInstanceId($this->getFlagshipInstanceId())
->setTraffic(0)
->setLogLevel("ERROR")
->setLogLevel(LogLevel::ERROR)
->setConfig($this->getConfig())
->setHttpRequestMethod("GET")
->setHttpRequestUrl($url)
Expand Down Expand Up @@ -276,8 +276,7 @@ private function getVisitorCampaigns(
) {
$visitorCampaigns = [];
foreach ($variationGroups as $variationGroup) {
$check = $this->isMatchTargeting($variationGroup, $visitor);
if ($check) {
if ($this->isMatchTargeting($variationGroup, $visitor)) {
$variations = $this->getVariation(
$variationGroup,
$visitor
Expand Down Expand Up @@ -409,8 +408,7 @@ private function isMatchTargeting($variationGroup, VisitorAbstract $visitor)

$innerTargetings = $targetingGroup[FlagshipField::FIELD_TARGETINGS];

$check = $this->checkAndTargeting($innerTargetings, $visitor);
if ($check) {
if ($this->checkAndTargeting($innerTargetings, $visitor)) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Enum/FlagshipConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class FlagshipConstant
/**
* SDK version
*/
const SDK_VERSION = "3.2.0";
const SDK_VERSION = "3.2.1";

const GET_FLAG = 'GET_FLAG';

Expand Down Expand Up @@ -205,5 +205,5 @@ class FlagshipConstant
const LOG_FORMAT_RESPONSE_STATUS = 'RESPONSE_STATUS';
const LOG_FORMAT_DURATION = 'DURATION';

const ANALYTIC_HIT_ALLOCATION = 20;
const ANALYTIC_HIT_ALLOCATION = 1;
}
1 change: 1 addition & 0 deletions src/Enum/TroubleshootingLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ class TroubleshootingLabel
const SDK_BUCKETING_FILE_ERROR= "SDK_BUCKETING_FILE_ERROR";
const SDK_BUCKETING_FILE = "SDK_BUCKETING_FILE";
const SDK_CONFIG = "SDK_CONFIG";
const SEND_HIT_ROUTE_ERROR = 'SEND_HIT_ROUTE_ERROR';
}
31 changes: 29 additions & 2 deletions src/Hit/Diagnostic.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Diagnostic extends HitAbstract
*/
private $sdkConfigMode;

/**
* @var numeric
*/
private $sdkConfigLogLeve;

/**
* @var bool
*/
Expand Down Expand Up @@ -288,7 +293,6 @@ class Diagnostic extends HitAbstract
private $flagshipInstanceId;



public function __construct($type)
{
parent::__construct($type);
Expand Down Expand Up @@ -1293,6 +1297,26 @@ public function setHitContent($hitContent)
return $this;
}

/**
* @return float|int|string
*/
public function getSdkConfigLogLeve()
{
return $this->sdkConfigLogLeve;
}

/**
* @param float|int|string $sdkConfigLogLeve
* @return Diagnostic
*/
public function setSdkConfigLogLeve($sdkConfigLogLeve)
{
$this->sdkConfigLogLeve = $sdkConfigLogLeve;
return $this;
}



public function toApiKeys()
{
$customVariable = [
Expand Down Expand Up @@ -1330,6 +1354,9 @@ public function toApiKeys()
if ($this->getSdkStatus() !== null) {
$customVariable["sdk.status"] = FlagshipStatus::getStatusName($this->getSdkStatus());
}
if ($this->getSdkConfigLogLeve() !== null) {
$customVariable["sdk.config.logLevel"] = LogLevel::getLogName($this->getSdkConfigLogLeve());
}
if ($this->getSdkConfigMode() !== null) {
$customVariable["sdk.config.mode"] = DecisionMode::getDecisionModeName($this->getSdkConfigMode());
}
Expand Down Expand Up @@ -1416,7 +1443,7 @@ public function toApiKeys()
if (is_array($this->getVisitorContext())) {
$context = $this->getVisitorContext();
foreach ($context as $index => $item) {
$customVariable["visitor.context.$index"] = is_string($item) ? $item : json_encode($item);
$customVariable["visitor.context.[$index]"] = is_string($item) ? $item : json_encode($item);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Visitor/NoConsentStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function fetchVisitorCampaigns(VisitorAbstract $visitor)
*/
private function log($functionName)
{
$this->logError(
$this->logInfo(
$this->getVisitor()->getConfig(),
sprintf(
FlagshipConstant::METHOD_DEACTIVATED_CONSENT_ERROR,
Expand Down
4 changes: 2 additions & 2 deletions src/Visitor/PanicStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PanicStrategy extends DefaultStrategy
public function setConsent($hasConsented)
{
$this->visitor->hasConsented = $hasConsented;
$this->logError(
$this->logInfo(
$this->getVisitor()->getConfig(),
sprintf(
FlagshipConstant::METHOD_DEACTIVATED_SEND_CONSENT_ERROR,
Expand Down Expand Up @@ -146,7 +146,7 @@ protected function fetchVisitorCampaigns(VisitorAbstract $visitor)
*/
private function log($functionName)
{
$this->logError(
$this->logInfo(
$this->getVisitor()->getConfig(),
sprintf(
FlagshipConstant::METHOD_DEACTIVATED_ERROR,
Expand Down
8 changes: 6 additions & 2 deletions src/Visitor/VisitorStrategyAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,12 @@ public function sendSdkConfigAnalyticHit()
}
$uniqueId = $this->getVisitor()->getVisitorId() . $this->getCurrentDateTime()->format("Y-m-d");
$hash = $this->getMurmurHash()->murmurHash3Int32($uniqueId);
$traffic = $hash % 100;
if ($traffic >= FlagshipConstant::ANALYTIC_HIT_ALLOCATION) {
$traffic = $hash % 1000;

if ($traffic > FlagshipConstant::ANALYTIC_HIT_ALLOCATION) {
return;
}

$visitor = $this->getVisitor();
$config = $this->getConfig();
$bucketingUrl = null;
Expand All @@ -382,6 +384,7 @@ public function sendSdkConfigAnalyticHit()
->setLogLevel(LogLevel::INFO)
->setVisitorId($this->getFlagshipInstanceId())
->setSdkConfigMode($config->getDecisionMode())
->setSdkConfigLogLeve($config->getLogLevel())
->setSdkConfigTimeout($config->getTimeout())
->setSdkConfigTrackingManagerConfigStrategy($config->getCacheStrategy())
->setSdkConfigBucketingUrl($bucketingUrl)
Expand Down Expand Up @@ -444,6 +447,7 @@ public function sendFetchFlagsTroubleshooting($troubleshootingData, $flagsDTO, $
->setVisitorIsAuthenticated(!!$visitor->getAnonymousId())
->setHttpResponseTime(($this->getNow() - $now))
->setSdkConfigMode($config->getDecisionMode())
->setSdkConfigLogLeve($config->getLogLevel())
->setSdkConfigTimeout($config->getTimeout())
->setSdkConfigBucketingUrl($bucketingUrl)
->setSdkConfigFetchThirdPartyData($fetchThirdPartyData)
Expand Down
10 changes: 8 additions & 2 deletions tests/Api/NoBatchingContinuousCachingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testAddHitFailed()
true,
true,
true,
["cacheHit","flushHits","logErrorSprintf"]
["cacheHit","flushHits","logErrorSprintf", "addTroubleshootingHit", "sendTroubleshootingQueue"]
);

$strategy->expects($this->never())->method("flushHits");
Expand All @@ -152,6 +152,7 @@ public function testAddHitFailed()

$strategy->expects($this->once())->method("cacheHit")->with([$page]);


$requestBody = $page->toApiKeys();

$url = FlagshipConstant::HIT_EVENT_URL;
Expand Down Expand Up @@ -186,6 +187,9 @@ public function testAddHitFailed()
]
);

$strategy->expects($this->once())->method("addTroubleshootingHit");
$strategy->expects($this->once())->method("sendTroubleshootingQueue");

$this->assertCount(0, $strategy->getHitsPoolQueue());
$this->assertCount(0, $strategy->getActivatePoolQueue());
$strategy->addHit($page);
Expand Down Expand Up @@ -381,7 +385,7 @@ public function testActivateFlagFailed()
true,
true,
true,
["cacheHit","flushHits","logErrorSprintf"]
["cacheHit","flushHits","logErrorSprintf", "addTroubleshootingHit", "sendTroubleshootingQueue"]
);

$strategy->expects($this->never())->method("flushHits");
Expand Down Expand Up @@ -425,6 +429,8 @@ public function testActivateFlagFailed()
[FlagshipConstant::SEND_ACTIVATE, $logMessage ]
]
);
$strategy->expects($this->once())->method("addTroubleshootingHit");
$strategy->expects($this->once())->method("sendTroubleshootingQueue");

$this->assertCount(0, $strategy->getHitsPoolQueue());
$this->assertCount(0, $strategy->getActivatePoolQueue());
Expand Down
9 changes: 7 additions & 2 deletions tests/Decision/BucketingManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,24 @@ public function testSendContext()
$configManager->setConfig($config)->setTrackingManager($trackerManager);
$visitor = new VisitorDelegate($containerMock, $configManager, $visitorId, false, $visitorContext, true);

$httpClientMock->expects($this->exactly(2))
$httpClientMock->expects($this->exactly(3))
->method('get')
->willReturn(
new HttpResponse(204, json_decode('{"campaigns":[{}]}', true))
);

$trackerManager->expects($this->exactly(2))->method("addHit");
$trackerManager->expects($this->exactly(3))->method("addHit");

$bucketingManager->getCampaignModifications($visitor);

//Test empty context
$visitor = new VisitorDelegate($containerMock, $configManager, $visitorId, false, [], true);
$bucketingManager->getCampaignModifications($visitor);

//Test visitor has not consented
$visitor = new VisitorDelegate($containerMock, $configManager, $visitorId, false, $visitorContext, false);
$bucketingManager->getCampaignModifications($visitor);

}

public function testGetVariation()
Expand Down
6 changes: 4 additions & 2 deletions tests/Hit/TroubleshootingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function testConstruct()
->setStackOriginVersion($stackOriginVersion)
->setSdkStatus($sdkStatus)
->setSdkConfigMode($sdkConfigMode)
->setSdkConfigLogLeve($config->getLogLevel())
->setSdkConfigCustomLogManager(true)
->setSdkConfigCustomCacheManager(true)
->setSdkConfigStatusListener(false)
Expand Down Expand Up @@ -162,6 +163,7 @@ public function testConstruct()
'stack.origin.name' => $stackOriginName,
'stack.origin.version' => $stackOriginVersion,
'sdk.status' => FlagshipStatus::getStatusName($sdkStatus),
'sdk.config.logLevel' => LogLevel::getLogName($config->getLogLevel()),
'sdk.config.mode' => DecisionMode::getDecisionModeName($sdkConfigMode),
'sdk.config.customLogManager' => 'true',
'sdk.config.customCacheManager' => 'true',
Expand All @@ -183,8 +185,8 @@ public function testConstruct()
'http.response.code' => (string)$httpResponseCode,
"http.response.body" => json_encode($httpResponseBody),
'http.response.time' => (string)$httpResponseTime,
'visitor.context.key1' => 'value1',
'visitor.context.key2' => 'value2',
'visitor.context.[key1]' => 'value1',
'visitor.context.[key2]' => 'value2',
'visitor.consent' => 'true',
'visitor.assignments.key1' => 'value1',
'visitor.assignments.key2' => 'value2',
Expand Down
Loading

0 comments on commit 1172f2e

Please sign in to comment.