You are here

private function MemcacheStatisticsController::statsTablesRawOutput in Memcache API and Integration 8.2

Generates render array for output.

1 call to MemcacheStatisticsController::statsTablesRawOutput()
MemcacheStatisticsController::statsTableRaw in memcache_admin/src/Controller/MemcacheStatisticsController.php
Callback for the Memcache Stats page.

File

memcache_admin/src/Controller/MemcacheStatisticsController.php, line 220

Class

MemcacheStatisticsController
Memcache Statistics.

Namespace

Drupal\memcache_admin\Controller

Code

private function statsTablesRawOutput($cluster, $server, $stats, $type) {
  $user = \Drupal::currentUser();
  $current_type = isset($type) ? $type : 'default';
  $memcache = \Drupal::service('memcache.factory')
    ->get(NULL, TRUE);
  $memcache_bins = $memcache
    ->getBins();
  $bin = isset($memcache_bins[$cluster]) ? $memcache_bins[$cluster] : 'default';
  $slab = \Drupal::routeMatch()
    ->getParameter('slab');

  // Provide navigation for the various memcache stats types.
  $links = [];
  if (count($memcache
    ->statsTypes())) {
    foreach ($memcache
      ->statsTypes() as $type) {

      // @todo render array
      $link = Link::fromTextandUrl($type, Url::fromUri('base:/admin/reports/memcache/' . $bin . '/' . str_replace('/', '!', $server) . '/' . ($type == 'default' ? '' : $type)))
        ->toString();
      if ($current_type == $type) {
        $links[] = '<strong>' . $link . '</strong>';
      }
      else {
        $links[] = $link;
      }
    }
  }
  $build = [
    'links' => [
      '#markup' => !empty($links) ? implode(' | ', $links) : '',
    ],
  ];
  $build['table'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Property'),
      $this
        ->t('Value'),
    ],
  ];
  $row = 0;

  // Items are returned as an array within an array within an array.  We step
  // in one level to properly display the contained statistics.
  if ($current_type == 'items' && isset($stats['items'])) {
    $stats = $stats['items'];
  }
  foreach ($stats as $key => $value) {

    // Add navigation for getting a cachedump of individual slabs.
    if (($current_type == 'slabs' || $current_type == 'items') && is_int($key) && $user
      ->hasPermission('access slab cachedump')) {
      $build['table'][$row]['key'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Slab @slab', [
          '@slab' => $key,
        ]),
        '#url' => Url::fromUri('base:/admin/reports/memcache/' . $bin . '/' . str_replace('/', '!', $server) . '/slabs/cachedump/' . $key),
      ];
    }
    else {
      $build['table'][$row]['key'] = [
        '#plain_text' => $key,
      ];
    }
    if (is_array($value)) {
      $subrow = 0;
      $build['table'][$row]['value'] = [
        '#type' => 'table',
      ];
      foreach ($value as $k => $v) {

        // Format timestamp when viewing cachedump of individual slabs.
        if ($current_type == 'slabs' && $user
          ->hasPermission('access slab cachedump') && !empty($slab) && $k == 0) {
          $k = $this
            ->t('Size');
          $v = format_size($v);
        }
        elseif ($current_type == 'slabs' && $user
          ->hasPermission('access slab cachedump') && !empty($slab) && $k == 1) {
          $k = $this
            ->t('Expire');
          $full_stats = $memcache
            ->stats($cluster);
          $infinite = $full_stats[$cluster][$server]['time'] - $full_stats[$cluster][$server]['uptime'];
          if ($v == $infinite) {
            $v = $this
              ->t('infinite');
          }
          else {
            $v = $this
              ->t('in @time', [
              '@time' => \Drupal::service('date.formatter')
                ->formatInterval($v - \Drupal::time()
                ->getRequestTime()),
            ]);
          }
        }
        $build['table'][$row]['value'][$subrow] = [
          'key' => [
            '#plain_text' => $k,
          ],
          'value' => [
            '#plain_text' => $v,
          ],
        ];
        $subrow++;
      }
    }
    else {
      $build['table'][$row]['value'] = [
        '#plain_text' => $value,
      ];
    }
    $row++;
  }
  return $build;
}