You are here

public function DatabaseDiskUsagePlugin::resultVerbose in Monitoring 8

Provide additional info about sensor call.

This method is only executed on request. It is guaranteed that runSensor() is executed before this method.

Parameters

\Drupal\monitoring\Result\SensorResultInterface $result: Sensor result.

Return value

array Sensor call verbose info as render array.

Overrides ExtendedInfoSensorPluginInterface::resultVerbose

File

src/Plugin/monitoring/SensorPlugin/DatabaseDiskUsagePlugin.php, line 86

Class

DatabaseDiskUsagePlugin
Monitors database disk usage.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

public function resultVerbose(SensorResultInterface $result) {
  $output = [];
  $disk_usage = $this
    ->getDiskUsage();
  $database_name = $this->database
    ->getConnectionOptions()['database'];
  $usage_by_table = $this
    ->getDiskUsageByTable($database_name);
  if ($this->sensorConfig
    ->getThresholdValue('warning') && $this->sensorConfig
    ->getThresholdValue('critical') && $disk_usage) {
    $output['database_usage'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Database usage'),
    ];
    $output['database_usage']['table'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Usage'),
        $this
          ->t('Warning level (@amountMB)', [
          '@amount' => $this->sensorConfig
            ->getThresholdValue('warning'),
        ]),
        $this
          ->t('Critical level (@amountMB)', [
          '@amount' => $this->sensorConfig
            ->getThresholdValue('critical'),
        ]),
      ],
    ];
    $output['database_usage']['table'][0]['usage'] = [
      '#type' => 'item',
      '#plain_text' => number_format($disk_usage, 2) . 'MB',
    ];
    $output['database_usage']['table'][0]['warning'] = [
      '#type' => 'item',
      '#plain_text' => number_format($disk_usage * 100 / $this->sensorConfig
        ->getThresholdValue('warning'), 2) . '%',
    ];
    $output['database_usage']['table'][0]['critical'] = [
      '#type' => 'item',
      '#plain_text' => number_format($disk_usage * 100 / $this->sensorConfig
        ->getThresholdValue('critical'), 2) . '%',
    ];
  }
  if ($usage_by_table) {
    $output['tables_usage'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Biggest database tables by size.'),
    ];
    $output['tables_usage']['table'] = [
      '#type' => 'table',
      '#header' => [
        'table_name' => [
          'data' => $this
            ->t('Table name'),
        ],
        'table_rows' => [
          'data' => $this
            ->t('Table rows'),
          'class' => [
            RESPONSIVE_PRIORITY_MEDIUM,
          ],
        ],
        'table_length' => [
          'data' => $this
            ->t('Table length (MB)'),
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        'index_length' => [
          'data' => $this
            ->t('Index length (MB)'),
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        'size' => [
          'data' => $this
            ->t('Size (MB)'),
        ],
      ],
    ];
    foreach ($usage_by_table as $key => $table_info) {
      $output['tables_usage']['table'][$key]['table_name'] = [
        '#type' => 'item',
        '#plain_text' => $table_info->table_name,
      ];
      $output['tables_usage']['table'][$key]['table_rows'] = [
        '#type' => 'item',
        '#plain_text' => $table_info->table_rows,
      ];
      $output['tables_usage']['table'][$key]['table_length'] = [
        '#type' => 'item',
        '#plain_text' => $table_info->table_length,
      ];
      $output['tables_usage']['table'][$key]['index_length'] = [
        '#type' => 'item',
        '#plain_text' => $table_info->index_length,
      ];
      $output['tables_usage']['table'][$key]['size'] = [
        '#type' => 'item',
        '#plain_text' => $table_info->size,
      ];
    }
  }
  return $output;
}