You are here

public function MemcacheServerStatsSubscriber::onReportStats in Memcache API and Integration 8.2

Populates the reporting of a stored set of stats.

Parameters

\Drupal\memcache_admin\Event\MemcacheStatsEvent $event:

File

memcache_admin/src/EventSubscriber/MemcacheServerStatsSubscriber.php, line 73

Class

MemcacheServerStatsSubscriber
Adds memcache server specific details to the stats array.

Namespace

Drupal\memcache_admin\EventSubscriber

Code

public function onReportStats(MemcacheStatsEvent $event) {
  $stats = $event
    ->getFormattedStats('memcache');
  $bin = $event
    ->getCacheBin();

  // No cache bin data, return.
  if (empty($stats[$bin])) {

    // Failed to load statistics. Provide a useful error about where to get
    // more information and help.
    $this
      ->messenger()
      ->addError($this
      ->t('There may be a problem with your Memcache configuration. Please review @readme for more information.', [
      '@readme' => 'README.txt',
    ]));
    return;
  }

  // No servers found, return.
  if (!is_array($stats[$bin])) {
    return;
  }

  /**
   * @var string $server
   * @var MemcacheStatsObject $statistics
   */
  foreach ($stats[$bin] as $server => $statistics) {
    if (empty($statistics
      ->getUptime())) {
      $this
        ->messenger()
        ->addError($this
        ->t('Failed to connect to server at :address.', [
        ':address' => $server,
      ]));
    }
    else {
      $data['server_overview'][$server] = $this
        ->t('v@version running @uptime', [
        '@version' => $statistics
          ->getVersion(),
        '@uptime' => $statistics
          ->getUptime(),
      ]);
      $data['server_time'][$server] = $statistics
        ->getServerTime();
      $data['server_connections'][$server] = $statistics
        ->getConnections();
      $data['cache_sets'][$server] = $statistics
        ->getSets();
      $data['cache_gets'][$server] = $statistics
        ->getGets();
      $data['cache_counters'][$server] = $statistics
        ->getCounters();
      $data['cache_transfer'][$server] = $statistics
        ->getTransferred();
      $data['cache_average'][$server] = $statistics
        ->getConnectionAvg();
      $data['memory_available'][$server] = $statistics
        ->getMemory();
      $data['memory_evictions'][$server] = $statistics
        ->getEvictions();
    }
  }

  // Build a custom report array.
  $report = [
    'uptime' => [
      'uptime' => [
        'label' => $this
          ->t('Uptime'),
        'servers' => $data['server_overview'],
      ],
      'time' => [
        'label' => $this
          ->t('Time'),
        'servers' => $data['server_time'],
      ],
      'connections' => [
        'label' => $this
          ->t('Connections'),
        'servers' => $data['server_connections'],
      ],
    ],
    'stats' => [
      'sets' => [
        'label' => $this
          ->t('Sets'),
        'servers' => $data["cache_sets"],
      ],
      'gets' => [
        'label' => $this
          ->t('Gets'),
        'servers' => $data["cache_gets"],
      ],
      'counters' => [
        'label' => $this
          ->t('Counters'),
        'servers' => $data["cache_counters"],
      ],
      'transfer' => [
        'label' => $this
          ->t('Transferred'),
        'servers' => $data["cache_transfer"],
      ],
      'average' => [
        'label' => $this
          ->t('Per-connection average'),
        'servers' => $data["cache_average"],
      ],
    ],
    'memory' => [
      'memory' => [
        'label' => $this
          ->t('Available memory'),
        'servers' => $data['memory_available'],
      ],
      'evictions' => [
        'label' => $this
          ->t('Evictions'),
        'servers' => $data['memory_evictions'],
      ],
    ],
  ];

  // Don't display aggregate totals if there's only one server.
  if (count($stats[$bin]) > 1) {

    /** @var MemcacheStatsObject $totals */
    $totals = $event
      ->getTotals();
    $report['uptime']['uptime']['total'] = $this
      ->t('n/a');
    $report['uptime']['time']['total'] = $this
      ->t('n/a');
    $report['uptime']['connections']['total'] = $totals[$bin]
      ->getConnections();
    $report['stats']['sets']['total'] = $totals[$bin]
      ->getSets();
    $report['stats']['gets']['total'] = $totals[$bin]
      ->getGets();
    $report['stats']['counters']['total'] = $totals[$bin]
      ->getCounters();
    $report['stats']['transfer']['total'] = $totals[$bin]
      ->getTransferred();
    $report['stats']['average']['total'] = $totals[$bin]
      ->getConnectionAvg();
    $report['memory']['memory']['total'] = $totals[$bin]
      ->getMemory();
    $report['memory']['evictions']['total'] = $totals[$bin]
      ->getEvictions();
  }
  $event
    ->updateReport($report);
}