예제 #1
0
 public function sendMetrics()
 {
     $metricsKeys = Di::getCache()->getKeys(MetricsCache::getCacheKeySearchLatencyPattern());
     $metricsCache = new MetricsCache();
     $cachedMetrics = array();
     foreach ($metricsKeys as $key) {
         $metricName = MetricsCache::getMetricNameFromKey($key);
         $metricBucket = MetricsCache::getBucketFromKey($key);
         if (!isset($cachedMetrics[$metricName]) || !is_array($cachedMetrics[$metricName])) {
             //initialization latency butckets
             $maxBucket = Latency::getBucketForLatencyMicros(Latency::MAX_LATENCY);
             $cachedMetrics[$metricName] = array_fill(0, $maxBucket + 1, 0);
         }
         $cachedMetrics[$metricName][$metricBucket] = $metricsCache->getLatencyAndReset($key);
     }
     $dataset = array();
     foreach ($cachedMetrics as $name => $latencies) {
         $dataset[] = array('name' => $name, 'latencies' => $latencies);
     }
     //Sending Metrics dataset.
     $response = $this->post($this->servicePath, $dataset);
     if ($response->isSuccessful()) {
         Di::getLogger()->info(count($dataset) . " Metrics sent successfuly");
     } else {
         Di::getLogger()->error("Metrics have not been sent successfully");
         Di::getLogger()->error("HTTP Code: " . $response->getStatusCode());
         Di::getLogger()->error("HTTP Body: " . $response->getBody());
     }
 }
예제 #2
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;
 }
예제 #3
0
 /**
  * @param $segmentName
  * @param $changeNumber
  * @return mixed
  */
 public function setChangeNumber($segmentName, $changeNumber)
 {
     $sinceKey = self::getCacheKeyForSinceParameter($segmentName);
     $since_cached_item = Di::getCache()->getItem($sinceKey);
     Di::getLogger()->info(">>> SINCE CACHE KEY: {$sinceKey}");
     $since_cached_item->set($changeNumber);
     return Di::getCache()->save($since_cached_item);
 }
예제 #4
0
 private function dropDataset($toDrop)
 {
     try {
         $impressionCache = new ImpressionCache();
         foreach ($toDrop as $key => $impressions) {
             Di::getLogger()->debug("Dropping impressions for key: " . $key);
             $impressionCache->removeImpression($key, $impressions);
         }
         Di::getLogger()->info("Sent Impressions removed from cache successfuly");
     } catch (\Exception $e) {
         Di::getLogger()->error($e->getMessage());
     }
 }
예제 #5
0
 public function getSegmentChanges($segmentName)
 {
     //Fetching next since (till) value from cache.
     $segmentCache = new SegmentCache();
     $since_cached_value = $segmentCache->getChangeNumber($segmentName);
     $servicePath = $this->servicePath . $segmentName . '?since=' . $since_cached_value;
     Di::getLogger()->info("SERVICE PATH: {$servicePath}");
     try {
         //GETting data from server
         $response = $this->get($servicePath);
         if ($response->isSuccessful()) {
             $segment = json_decode($response->getBody(), true);
             //Returning false due the server has not changes
             if (isset($segment['since']) && isset($segment['till']) && $segment['since'] >= $segment['till']) {
                 Di::getLogger()->notice("Segments returned by the server are empty");
                 return false;
             }
             return $segment;
         }
     } catch (\Exception $e) {
         Di::getLogger()->error($e->getMessage());
     }
     return false;
 }
예제 #6
0
파일: Pool.php 프로젝트: splitio/php-client
 /**
  * Persists a cache item immediately.
  *
  * @param \SplitIO\Component\Cache\Item $item
  *   The cache item to save.
  *
  * @return bool
  *   True if the item was successfully persisted. False if there was an error.
  */
 public function save(Item $item)
 {
     $key = $item->getKey();
     $value = $item->get();
     //PSR-6 CacheItemInterface doesn't define a method to get the item expiration value.
     $expiration = method_exists($item, 'getExpiration') ? $item->getExpiration() : 0;
     if ($this->adapter->save($key, $value, $expiration)) {
         Di::getLogger()->debug("Saving cache item: {$key} - {$value} - {$expiration}");
         return true;
     }
     return false;
 }
예제 #7
0
 /**
  * @return null|\Psr\Log\LoggerInterface
  */
 public static function logger()
 {
     return Di::getLogger();
 }
예제 #8
0
파일: Item.php 프로젝트: splitio/php-client
 /**
  * Sets the expiration time for this cache item.
  *
  * @param int|\DateInterval $time
  *   The period of time from the present after which the item MUST be considered
  *   expired. An integer parameter is understood to be the time in seconds until
  *   expiration. If null is passed explicitly, a default value MAY be used.
  *   If none is set, the value should be stored permanently or for as long as the
  *   implementation allows.
  *
  * @return static
  *   The called object.
  */
 public function expiresAfter($time)
 {
     if ($time instanceof DateInterval) {
         $expire = new DateTime();
         $expire->add($time);
         // convert datetime to unix timestamp
         $this->expire = (int) $expire->format('U');
     } elseif (is_int($time)) {
         $this->expire = time() + $time;
     } elseif (is_null($time)) {
         $this->expire = 0;
     }
     Di::getLogger()->info("//--> [CacheItem:{$this->key}] Set expiration time at:\n            {$this->expire} - " . date('Y-m-d H:i:s', $this->expire));
     return $this;
 }