You are here

class DatabaseDataCollector in Devel 8

Same name and namespace in other branches
  1. 8.3 webprofiler/src/DataCollector/DatabaseDataCollector.php \Drupal\webprofiler\DataCollector\DatabaseDataCollector
  2. 8.2 webprofiler/src/DataCollector/DatabaseDataCollector.php \Drupal\webprofiler\DataCollector\DatabaseDataCollector
  3. 4.x webprofiler/src/DataCollector/DatabaseDataCollector.php \Drupal\webprofiler\DataCollector\DatabaseDataCollector

Class DatabaseDataCollector

Hierarchy

Expanded class hierarchy of DatabaseDataCollector

1 file declares its use of DatabaseDataCollector
DatabaseController.php in webprofiler/src/Controller/DatabaseController.php
1 string reference to 'DatabaseDataCollector'
webprofiler.services.yml in webprofiler/webprofiler.services.yml
webprofiler/webprofiler.services.yml
1 service uses DatabaseDataCollector
webprofiler.database in webprofiler/webprofiler.services.yml
Drupal\webprofiler\DataCollector\DatabaseDataCollector

File

webprofiler/src/DataCollector/DatabaseDataCollector.php, line 17

Namespace

Drupal\webprofiler\DataCollector
View source
class DatabaseDataCollector extends DataCollector implements DrupalDataCollectorInterface {
  use StringTranslationTrait, DrupalDataCollectorTrait;

  /**
   * @var \Drupal\Core\Database\Connection
   */
  private $database;

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   */
  public function __construct(Connection $database, ConfigFactoryInterface $configFactory) {
    $this->database = $database;
    $this->configFactory = $configFactory;
  }

  /**
   * {@inheritdoc}
   */
  public function collect(Request $request, Response $response, \Exception $exception = NULL) {
    $connections = [];
    foreach (Database::getAllConnectionInfo() as $key => $info) {
      try {
        $database = Database::getConnection('default', $key);
        if ($database
          ->getLogger()) {
          $connections[$key] = $database
            ->getLogger()
            ->get('webprofiler');
        }
      } catch (\Exception $e) {

        // There was some error during database connection, maybe a stale
        // configuration in settings.php or wrong values used for a migration.
      }
    }
    $this->data['connections'] = array_keys($connections);
    $data = [];
    foreach ($connections as $key => $queries) {
      foreach ($queries as $query) {

        // Remove caller args.
        unset($query['caller']['args']);

        // Remove query args element if empty.
        if (isset($query['args']) && empty($query['args'])) {
          unset($query['args']);
        }

        // Save time in milliseconds.
        $query['time'] = $query['time'] * 1000;
        $query['database'] = $key;
        $data[] = $query;
      }
    }
    $querySort = $this->configFactory
      ->get('webprofiler.config')
      ->get('query_sort');
    if ('duration' === $querySort) {
      usort($data, [
        "Drupal\\webprofiler\\DataCollector\\DatabaseDataCollector",
        "orderQueryByTime",
      ]);
    }
    $this->data['queries'] = $data;
    $options = $this->database
      ->getConnectionOptions();

    // Remove password for security.
    unset($options['password']);
    $this->data['database'] = $options;
  }

  /**
   * @return array
   */
  public function getDatabase() {
    return $this->data['database'];
  }

  /**
   * @return int
   */
  public function getQueryCount() {
    return count($this->data['queries']);
  }

  /**
   * @return array
   */
  public function getQueries() {
    return $this->data['queries'];
  }

  /**
   * Returns the total execution time.
   *
   * @return float
   */
  public function getTime() {
    $time = 0;
    foreach ($this->data['queries'] as $query) {
      $time += $query['time'];
    }
    return $time;
  }

  /**
   * Returns a color based on the number of executed queries.
   *
   * @return string
   */
  public function getColorCode() {
    if ($this
      ->getQueryCount() < 100) {
      return 'green';
    }
    if ($this
      ->getQueryCount() < 200) {
      return 'yellow';
    }
    return 'red';
  }

  /**
   * Returns the configured query highlight threshold.
   *
   * @return int
   */
  public function getQueryHighlightThreshold() {

    // When a profile is loaded from storage this object is deserialized and
    // no constructor is called so we cannot use dependency injection.
    return \Drupal::config('webprofiler.config')
      ->get('query_highlight');
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'database';
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this
      ->t('Database');
  }

  /**
   * {@inheritdoc}
   */
  public function getPanelSummary() {
    return $this
      ->t('Executed queries: @count', [
      '@count' => $this
        ->getQueryCount(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getIcon() {
    return 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAcCAYAAABh2p9gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQRJREFUeNpi/P//PwM1ARMDlcGogZQDlpMnT7pxc3NbA9nhQKxOpL5rQLwJiPeBsI6Ozl+YBOOOHTv+AOllQNwtLS39F2owKYZ/gRq8G4i3ggxEToggWzvc3d2Pk+1lNL4fFAs6ODi8JzdS7mMRVyDVoAMHDsANdAPiOCC+jCQvQKqBQB/BDbwBxK5AHA3E/kB8nKJkA8TMQBwLxaBIKQbi70AvTADSBiSadwFXpCikpKQU8PDwkGTaly9fHFigkaKIJid4584dkiMFFI6jkTJII0WVmpHCAixZQEXWYhDeuXMnyLsVlEQKI45qFBQZ8eRECi4DBaAlDqle/8A48ip6gAADANdQY88Uc0oGAAAAAElFTkSuQmCC';
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries() {
    return [
      'webprofiler/database',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getData() {
    $data = $this->data;
    $conn = Database::getConnection();
    foreach ($data['queries'] as &$query) {
      $explain = TRUE;
      $type = 'select';
      if (strpos($query['query'], 'INSERT') !== FALSE) {
        $explain = FALSE;
        $type = 'insert';
      }
      if (strpos($query['query'], 'UPDATE') !== FALSE) {
        $explain = FALSE;
        $type = 'update';
      }
      if (strpos($query['query'], 'CREATE') !== FALSE) {
        $explain = FALSE;
        $type = 'create';
      }
      if (strpos($query['query'], 'DELETE') !== FALSE) {
        $explain = FALSE;
        $type = 'delete';
      }
      $query['explain'] = $explain;
      $query['type'] = $type;
      $quoted = [];
      if (isset($query['args'])) {
        foreach ((array) $query['args'] as $key => $val) {
          $quoted[$key] = is_null($val) ? 'NULL' : $conn
            ->quote($val);
        }
      }
      $query['query_args'] = strtr($query['query'], $quoted);
    }
    $data['query_highlight_threshold'] = $this
      ->getQueryHighlightThreshold();
    return $data;
  }

  /**
   * @param $a
   * @param $b
   *
   * @return int
   */
  private function orderQueryByTime($a, $b) {
    $at = $a['time'];
    $bt = $b['time'];
    if ($at == $bt) {
      return 0;
    }
    return $at < $bt ? 1 : -1;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseDataCollector::$configFactory private property
DatabaseDataCollector::$database private property
DatabaseDataCollector::collect public function Collects data for the given Request and Response.
DatabaseDataCollector::getColorCode public function Returns a color based on the number of executed queries.
DatabaseDataCollector::getData public function Overrides DrupalDataCollectorInterface::getData
DatabaseDataCollector::getDatabase public function
DatabaseDataCollector::getIcon public function Returns the collector icon in base64 format. Overrides DrupalDataCollectorInterface::getIcon
DatabaseDataCollector::getLibraries public function Returns the libraries needed in detail panel. Overrides DrupalDataCollectorInterface::getLibraries
DatabaseDataCollector::getName public function Returns the name of the collector. Overrides DrupalDataCollectorInterface::getName
DatabaseDataCollector::getPanelSummary public function Returns the string used in vertical tab summary. Overrides DrupalDataCollectorInterface::getPanelSummary
DatabaseDataCollector::getQueries public function
DatabaseDataCollector::getQueryCount public function
DatabaseDataCollector::getQueryHighlightThreshold public function Returns the configured query highlight threshold.
DatabaseDataCollector::getTime public function Returns the total execution time.
DatabaseDataCollector::getTitle public function Returns the datacollector title. Overrides DrupalDataCollectorInterface::getTitle
DatabaseDataCollector::orderQueryByTime private function
DatabaseDataCollector::__construct public function
DrupalDataCollectorInterface::getDrupalSettings public function 1
DrupalDataCollectorInterface::hasPanel public function Returns true if this datacollector has a detail panel. 2
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.