You are here

protected function DatabaseDiskUsagePlugin::getDiskUsageByTable in Monitoring 8

Gets disk usage by table.

Returns number of rows, data memory usage in MB, index memory usage in MB for every table.

Parameters

string $database_name: The name of the database.

int $from: The starting index.

int $count: The number of records.

Throws

\RuntimeException Thrown when the database type is not mysql or the database name is not set.

1 call to DatabaseDiskUsagePlugin::getDiskUsageByTable()
DatabaseDiskUsagePlugin::resultVerbose in src/Plugin/monitoring/SensorPlugin/DatabaseDiskUsagePlugin.php
Provide additional info about sensor call.

File

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

Class

DatabaseDiskUsagePlugin
Monitors database disk usage.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

protected function getDiskUsageByTable($database_name, $from = 0, $count = 10) {
  if ($this->database
    ->databaseType() !== 'mysql') {
    throw new \RuntimeException($this
      ->t('The table information is only available for mysql databases.'));
  }
  if (!$database_name) {
    throw new \RuntimeException($this
      ->t('The database name needs to be set.'));
  }
  $query = "\n    SELECT table_schema, table_name,\n    SUM(table_rows) AS 'table_rows',\n    SUM(round(data_length / 1048576, 2)) AS 'table_length',\n    SUM(round(index_length / 1048576, 2)) AS 'index_length',\n    SUM(round(((data_length + index_length) / 1024 / 1024),2)) AS 'size'\n    FROM information_schema.TABLES\n    WHERE TABLE_TYPE = 'BASE TABLE'\n    AND table_schema = :database_name\n    GROUP BY table_schema, table_name\n    ORDER BY size DESC";
  return $this->database
    ->queryRange($query, $from, $count, [
    ':database_name' => $database_name,
  ])
    ->fetchAll();
}