Esempio n. 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());
     }
 }
Esempio n. 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;
 }
Esempio n. 3
0
 /**
  * @depends testDiLog
  */
 public function testDiCache()
 {
     try {
         $cachePoolAdapter = ['name' => 'redis', 'options' => ['host' => REDIS_HOST, 'port' => REDIS_PORT]];
         $cachePool = new Pool(['adapter' => $cachePoolAdapter]);
         Di::getInstance()->setCache($cachePool);
     } catch (\Exception $e) {
         $this->assertTrue(false, "Error setting cache on Di");
     }
     $this->assertTrue(true);
 }
Esempio n. 4
0
 public function __construct($apiKey)
 {
     $serverSDKUrl = SplitIOUtils\getSplitServerUrl();
     $serverEventsUrl = SplitIOUtils\getSplitEventsUrl();
     $config = new Config();
     $config->setUrl($serverSDKUrl);
     $config->setEventsUrl($serverEventsUrl);
     $config->setAuthorization($apiKey);
     $this->config = $config;
     //Adding Client configuration as Di value for all Client Resources.
     Di::set(Di::KEY_SPLIT_CLIENT_CONFIG, $config);
 }
Esempio n. 5
0
 /**
  * @return array
  */
 public function splits()
 {
     $_splits = array();
     $splitKeys = Di::getCache()->getKeys(SplitCache::getCacheKeySearchPattern());
     foreach ($splitKeys as $key) {
         $splitView = $this->getSplitByCacheKey($key);
         if ($splitView != null) {
             $_splits[] = $splitView;
         }
     }
     return $_splits;
 }
 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());
     }
 }
Esempio n. 7
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;
 }
Esempio n. 8
0
 /**
  * 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;
 }
Esempio n. 9
0
 public function __construct()
 {
     $this->config = Di::get(Di::KEY_SPLIT_CLIENT_CONFIG);
     $this->httpTransport = array('transport' => 'Requests_Transport_fsockopen');
     $this->resourceType = $this->getResourceType();
 }
Esempio n. 10
0
 /**
  * @return null|\SplitIO\Component\Cache\Pool
  */
 public static function cache()
 {
     return Di::getCache();
 }
Esempio n. 11
0
 /**
  * @param $key
  * @return mixed|string
  */
 public function getLatencyAndReset($key)
 {
     return Di::getCache()->getSet($key, 0);
 }
Esempio n. 12
0
 /**
  * @param $segmentName
  * @return mixed
  */
 public function getChangeNumber($segmentName)
 {
     $since = Di::getCache()->getItem(self::getCacheKeyForSinceParameter($segmentName))->get();
     return empty($since) ? -1 : $since;
 }
Esempio n. 13
0
 /**
  * @param $key
  * @return mixed
  */
 private function get($key)
 {
     $item = Di::getCache()->getItem($key);
     $checkpoint = $item->get();
     return empty($checkpoint) ? -1 : (int) $checkpoint;
 }
Esempio n. 14
0
 /**
  * @param string $splitName
  * @return string JSON representation
  */
 public function getSplit($splitName)
 {
     $cache = Di::getCache();
     $cacheItem = $cache->getItem(self::getCacheKeyForSplit($splitName));
     return $cacheItem->get();
 }
Esempio n. 15
0
 /**
  * @param $key
  * @param $value
  * @return mixed
  */
 public function removeImpression($key, $value)
 {
     return Di::getCache()->removeItemOnList($key, $value);
 }
Esempio n. 16
0
 /**
  * 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;
 }