You are here

class MemcacheStatsObject in Memcache API and Integration 8.2

Class MemcacheStats.

@package Drupal\memcache_admin\Stats

Hierarchy

Expanded class hierarchy of MemcacheStatsObject

2 files declare their use of MemcacheStatsObject
McrouterStatsSubscriber.php in memcache_admin/src/EventSubscriber/McrouterStatsSubscriber.php
MemcacheServerStatsSubscriber.php in memcache_admin/src/EventSubscriber/MemcacheServerStatsSubscriber.php

File

memcache_admin/src/Stats/MemcacheStatsObject.php, line 12

Namespace

Drupal\memcache_admin\Stats
View source
class MemcacheStatsObject implements MemcacheStatsInterface {
  use StringTranslationTrait;

  /**
   * Stat Not Available
   */
  const NA = 'n/a';

  /**
   * @var array $stats
   */
  protected $stats;
  public function __construct(array $raw_stats) {
    $this->stats = $raw_stats;
  }

  /**
   * @inheritDoc
   */
  public function getUptime() : string {
    return isset($this->stats['uptime']) ? \Drupal::service('date.formatter')
      ->formatInterval($this->stats['uptime']) : self::NA;
  }

  /**
   * @inheritDoc
   */
  public function getExtension() : string {
    return isset($this->stats['extension']) ?? self::NA;
  }

  /**
   * @inheritDoc
   */
  public function getServerTime() : string {
    return isset($this->stats['time']) ? \Drupal::service('date.formatter')
      ->format($this->stats['time']) : self::NA;
  }

  /**
   * Statistics report: format total and open connections.
   */
  public function getConnections() {
    if (!isset($this->stats['curr_connections']) || !isset($this->stats['total_connections'])) {
      return self::NA;
    }
    return $this
      ->t('@current open of @total total', [
      '@current' => number_format($this->stats['curr_connections']),
      '@total' => number_format($this->stats['total_connections']),
    ]);
  }

  /**
   * @inheritDoc
   */
  public function getCurrentConnections() : string {
    return isset($this->stats['curr_connections']) ?? '0';
  }

  /**
   * @inheritDoc
   */
  public function getTotalConnections() : string {
    return isset($this->stats['total_connections']) ?? '0';
  }

  /**
   * Statistics report: calculate # of set cmds and total cmds.
   */
  public function getSets() : string {
    if (!isset($this->stats['cmd_set'])) {
      return self::NA;
    }
    if ($this->stats['cmd_set'] + $this->stats['cmd_get'] == 0) {
      $sets = 0;
    }
    else {
      $sets = $this->stats['cmd_set'] / ($this->stats['cmd_set'] + $this->stats['cmd_get']) * 100;
    }
    if (!isset($this->stats['uptime'])) {
      $average = 0;
    }
    else {
      $average = $sets / $this->stats['uptime'];
    }
    return $this
      ->t('@average/s; @set sets (@sets%) of @total commands', [
      '@average' => number_format($average, 2),
      '@sets' => number_format($sets, 2),
      '@set' => number_format($this->stats['cmd_set']),
      '@total' => number_format($this->stats['cmd_set'] + $this->stats['cmd_get']),
    ]);
  }

  /**
   * Statistics report: calculate # of get cmds, broken down by hits and misses.
   */
  public function getGets() : string {
    if (!isset($this->stats['cmd_set']) || !isset($this->stats['cmd_get'])) {
      return self::NA;
    }
    else {
      $get = $this->stats['cmd_get'];
      $set = $this->stats['cmd_set'];
      $hits = isset($this->stats['get_hits']) ?? 0;
      $misses = isset($this->stats['get_misses']) ?? 0;
    }
    if ($set + $get == 0) {
      $gets = 0;
    }
    else {
      $gets = $get / ($set + $get) * 100;
    }
    if (empty($stats['uptime'])) {
      $average = 0;
    }
    else {
      $average = $get / $stats['uptime'];
    }
    return $this
      ->t('@average/s; @total gets (@gets%); @hit hits (@percent_hit%) @miss misses (@percent_miss%)', [
      '@average' => number_format($average, 2),
      '@gets' => number_format($gets, 2),
      '@hit' => number_format($hits),
      '@percent_hit' => $get > 0 ? number_format($hits / $get * 100, 2) : '0.00',
      '@miss' => number_format($misses),
      '@percent_miss' => $get > 0 ? number_format($misses / $get * 100, 2) : '0.00',
      '@total' => number_format($get),
    ]);
  }

  /**
   * @inheritDoc
   */
  public function getCounters() : string {
    $incr_hits = isset($this->stats['incr_hits']) ?? 0;
    $incr_misses = isset($this->stats['incr_misses']) ?? 0;
    $decr_hits = isset($this->stats['decr_hits']) ?? 0;
    $decr_misses = isset($this->stats['decr_misses']) ?? 0;
    return $this
      ->t('@incr increments, @decr decrements', [
      '@incr' => number_format($incr_hits + $incr_misses),
      '@decr' => number_format($decr_hits + $decr_misses),
    ]);
  }

  /**
   * @inheritDoc
   */
  public function getTransferred() : string {
    $read = isset($this->stats['bytes_read']) ?? 0;
    $write = isset($this->stats['bytes_written']) ?? 0;
    if ($write == 0) {
      $written = 0;
    }
    else {
      $written = $read / $write * 100;
    }
    return $this
      ->t('@to:@from (@written% to cache)', [
      '@to' => format_size((int) $read),
      '@from' => format_size((int) $write),
      '@written' => number_format($written, 2),
    ]);
  }

  /**
   * @inheritDoc
   */
  public function getConnectionAvg() : string {
    if (!isset($this->stats['total_connections']) || !isset($this->stats['cmd_get']) || !isset($this->stats['cmd_set']) || !isset($this->stats['bytes_written']) || !isset($this->stats['bytes_read'])) {
      return self::NA;
    }
    if ($this->stats['total_connections'] == 0) {
      $get = 0;
      $set = 0;
      $read = 0;
      $write = 0;
    }
    else {
      $get = $this->stats['cmd_get'] / $this->stats['total_connections'];
      $set = $this->stats['cmd_set'] / $this->stats['total_connections'];
      $read = $this->stats['bytes_written'] / $this->stats['total_connections'];
      $write = $this->stats['bytes_read'] / $this->stats['total_connections'];
    }
    return $this
      ->t('@read in @get gets; @write in @set sets', [
      '@get' => number_format($get, 2),
      '@set' => number_format($set, 2),
      '@read' => format_size((int) number_format($read, 2)),
      '@write' => format_size((int) number_format($write, 2)),
    ]);
  }

  /**
   * @inheritDoc
   */
  public function getMemory() : string {
    if (!isset($this->stats['limit_maxbytes']) || !isset($this->stats['bytes'])) {
      return self::NA;
    }
    if ($this->stats['limit_maxbytes'] == 0) {
      $percent = 0;
    }
    else {
      $percent = 100 - $this->stats['bytes'] / $this->stats['limit_maxbytes'] * 100;
    }
    return $this
      ->t('@available (@percent%) of @total', [
      '@available' => format_size($this->stats['limit_maxbytes'] - $this->stats['bytes']),
      '@percent' => number_format($percent, 2),
      '@total' => format_size($this->stats['limit_maxbytes']),
    ]);
  }

  /**
   * @inheritDoc
   */
  public function getEvictions() : string {
    return isset($this->stats['evictions']) ? number_format($this->stats['evictions']) : self::NA;
  }

  /**
   * @inheritDoc
   */
  public function setRaw(array $raw_data) {
    $this->stats = $raw_data;
  }

  /**
   * @inheritDoc
   */
  public function getRaw() : array {
    return $this->stats;
  }

  /**
   * @inheritDoc
   */
  public function getVersion() : string {
    return isset($this->stats['version']) ? (string) $this->stats['version'] : self::NA;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MemcacheStatsObject::$stats protected property 1
MemcacheStatsObject::getConnectionAvg public function @inheritDoc Overrides MemcacheStatsInterface::getConnectionAvg 1
MemcacheStatsObject::getConnections public function Statistics report: format total and open connections. 1
MemcacheStatsObject::getCounters public function @inheritDoc Overrides MemcacheStatsInterface::getCounters 1
MemcacheStatsObject::getCurrentConnections public function @inheritDoc 1
MemcacheStatsObject::getEvictions public function @inheritDoc Overrides MemcacheStatsInterface::getEvictions 1
MemcacheStatsObject::getExtension public function @inheritDoc Overrides MemcacheStatsInterface::getExtension 1
MemcacheStatsObject::getGets public function Statistics report: calculate # of get cmds, broken down by hits and misses. Overrides MemcacheStatsInterface::getGets 1
MemcacheStatsObject::getMemory public function @inheritDoc Overrides MemcacheStatsInterface::getMemory 1
MemcacheStatsObject::getRaw public function @inheritDoc Overrides MemcacheStatsInterface::getRaw 1
MemcacheStatsObject::getServerTime public function @inheritDoc 1
MemcacheStatsObject::getSets public function Statistics report: calculate # of set cmds and total cmds. Overrides MemcacheStatsInterface::getSets
MemcacheStatsObject::getTotalConnections public function @inheritDoc Overrides MemcacheStatsInterface::getTotalConnections 1
MemcacheStatsObject::getTransferred public function @inheritDoc Overrides MemcacheStatsInterface::getTransferred 1
MemcacheStatsObject::getUptime public function @inheritDoc Overrides MemcacheStatsInterface::getUptime
MemcacheStatsObject::getVersion public function @inheritDoc Overrides MemcacheStatsInterface::getVersion 1
MemcacheStatsObject::NA constant Stat Not Available
MemcacheStatsObject::setRaw public function @inheritDoc Overrides MemcacheStatsInterface::setRaw 1
MemcacheStatsObject::__construct public function 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.