Ejemplo n.º 1
0
 /**
  * @return bool|null
  */
 public function getSplitChanges()
 {
     //Fetching next since value from cache.
     $splitCache = new SplitCache();
     $since_cached_value = $splitCache->getChangeNumber();
     Di::getLogger()->info("SINCE CACHED VALUE: {$since_cached_value}");
     $servicePath = $this->servicePath . '?since=' . $since_cached_value;
     Di::getLogger()->info("SERVICE PATH: {$servicePath}");
     $response = $this->get($servicePath);
     if ($response->isSuccessful()) {
         $splitChanges = json_decode($response->getBody(), true);
         if (isset($splitChanges['since']) && isset($splitChanges['till']) && $splitChanges['since'] == $splitChanges['till']) {
             Di::getLogger()->info("Registering splits ready mark");
             $dateTimeUTC = new \DateTime("now", new \DateTimeZone("UTC"));
             $bur = new BlockUntilReadyCache();
             $bur->setReadySplits($dateTimeUTC->getTimestamp());
         }
         $splits = isset($splitChanges['splits']) ? $splitChanges['splits'] : false;
         if (!$splits) {
             Di::getLogger()->notice("Splits returned by the server are empty");
             return false;
         }
         return $splitChanges;
     }
     return false;
 }
Ejemplo n.º 2
0
 private function addSplitsInCache()
 {
     $splitChanges = file_get_contents(__DIR__ . "/files/splitChanges.json");
     $this->assertJson($splitChanges);
     $splitCache = new SplitCache();
     $splitChanges = json_decode($splitChanges, true);
     $splits = $splitChanges['splits'];
     foreach ($splits as $split) {
         $splitName = $split['name'];
         $this->assertTrue($splitCache->addSplit($splitName, json_encode($split)));
     }
 }
Ejemplo n.º 3
0
 public function execute()
 {
     //Fetching the Splits changes
     $splitChanges = $this->getSplitClient()->getSplitChanges();
     $splits = isset($splitChanges['splits']) ? $splitChanges['splits'] : array();
     $splitCache = new SplitCache();
     if (!empty($splits)) {
         foreach ($splits as $split) {
             if (!is_array($split) || !isset($split['name']) || !isset($split['conditions']) || !isset($split['status'])) {
                 continue;
                 //continue with next Split
             }
             $this->logger()->debug(print_r($split, true));
             $splitName = $split['name'];
             $splitStatus = $split['status'];
             $splitConditions = $split['conditions'];
             foreach ($splitConditions as $condition) {
                 foreach ($condition['matcherGroup']['matchers'] as $matcher) {
                     if ($matcher['matcherType'] == "IN_SEGMENT") {
                         //Register segment to retrieve Segment Data.
                         SegmentCache::registerSegment($matcher['userDefinedSegmentMatcherData']['segmentName']);
                     }
                 }
             }
             if ($splitStatus == 'ACTIVE') {
                 //Update Cache
                 $splitCache->addSplit($splitName, json_encode($split));
             } else {
                 //Delete item from cache
                 $splitCache->removeSplit($splitName);
             }
         }
     }
     if (isset($splitChanges['till'])) {
         $till = $splitChanges['till'];
         $this->logger()->debug("Splits Till value: {$till}");
         //Updating next since (till) value.
         if ($till != $splitCache->getChangeNumber()) {
             $splitCache->setChangeNumber($till);
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * @depends testDiLog
  * @depends testDiCache
  */
 public function testSplitCacheInterface()
 {
     $splitChanges = file_get_contents(__DIR__ . "/../../files/splitChanges.json");
     $this->assertJson($splitChanges);
     $splitCache = new SplitCache();
     $splitChanges = json_decode($splitChanges, true);
     $splits = $splitChanges['splits'];
     $split = $splits[0];
     $splitName = $split['name'];
     $this->assertTrue($splitCache->addSplit($splitName, json_encode($split)));
     $this->assertEquals(strlen(json_encode($split)), strlen($splitCache->getSplit($splitName)));
     $this->assertTrue($splitCache->removeSplit($splitName));
     $this->assertTrue($splitCache->setChangeNumber($splitChanges['till']));
     $this->assertEquals($splitChanges['till'], $splitCache->getChangeNumber());
 }
Ejemplo n.º 5
0
 private function evalTreatment($matchingKey, $bucketingKey, $featureName, array $attributes = null)
 {
     $split = null;
     $do_evaluation = false;
     $cachedFeature = $this->getCachedFeature($featureName);
     if ($cachedFeature !== null) {
         $split = $cachedFeature;
         $do_evaluation = true;
     } else {
         $splitCacheKey = SplitCache::getCacheKeyForSplit($featureName);
         $splitCachedItem = SplitApp::cache()->getItem($splitCacheKey);
         if ($splitCachedItem->isHit()) {
             SplitApp::logger()->info("{$featureName} is present on cache");
             $splitRepresentation = $splitCachedItem->get();
             $split = new Split(json_decode($splitRepresentation, true));
             $this->cacheFeature($featureName, $split);
             $do_evaluation = true;
         }
     }
     if ($do_evaluation) {
         //If the split was killed, log the impression and return default treatment.
         if ($split->killed()) {
             $defaultTreatment = $split->getDefaultTratment();
             $chn = $split->getChangeNumber();
             $this->logImpression($matchingKey, $featureName, $defaultTreatment, ImpressionLabel::KILLED, $chn);
             return $defaultTreatment;
         }
         $timeStart = Metrics::startMeasuringLatency();
         $treatment = Engine::getTreatment($matchingKey, $bucketingKey, $split, $attributes);
         $latency = Metrics::calculateLatency($timeStart);
         //If the given key doesn't match on any condition, default treatment is returned
         $impressionLabel = '';
         if ($treatment == null) {
             $treatment = $split->getDefaultTratment();
             $impressionLabel = ImpressionLabel::NO_CONDITION_MATCHED;
         }
         //Registering latency value
         MetricsCache::addLatencyOnBucket(Metrics::MNAME_SDK_GET_TREATMENT, Metrics::getBucketForLatencyMicros($latency));
         SplitApp::logger()->info("*Treatment for {$matchingKey} in {$split->getName()} is: {$treatment}");
         //Logging treatment impressions
         $this->logImpression($matchingKey, $featureName, $treatment, $impressionLabel, $split->getChangeNumber());
         //Returning treatment.
         return $treatment;
     }
     // Split not found impression
     $this->logImpression($matchingKey, $featureName, TreatmentEnum::CONTROL, ImpressionLabel::SPLIT_NOT_FOUND);
     SplitApp::logger()->warning("The SPLIT definition for '{$featureName}' has not been found'");
     return TreatmentEnum::CONTROL;
 }
Ejemplo n.º 6
0
 /**
  * @param $featureName
  * @return null|SplitView
  */
 public function split($featureName)
 {
     return $this->getSplitByCacheKey(SplitCache::getCacheKeyForSplit($featureName));
 }