You are here

class SanityCheck in MongoDB 8.2

Class SanityCheck provides some reasonableness checks for MongoDB contents.

Hierarchy

Expanded class hierarchy of SanityCheck

See also

\Drupal\mongodb_watchdog\Command\SanityCheckCommand

\Drupal\mongodb_watchdog\Commands\MongoDbWatchdogCommands::sanityCheck()

2 files declare their use of SanityCheck
MongoDbWatchdogCommands.php in modules/mongodb_watchdog/src/Commands/MongoDbWatchdogCommands.php
SanityCheckCommand.php in modules/mongodb_watchdog/src/Command/SanityCheckCommand.php
1 string reference to 'SanityCheck'
mongodb_watchdog.services.yml in modules/mongodb_watchdog/mongodb_watchdog.services.yml
modules/mongodb_watchdog/mongodb_watchdog.services.yml
1 service uses SanityCheck
mongodb.watchdog.sanity_check in modules/mongodb_watchdog/mongodb_watchdog.services.yml
Drupal\mongodb_watchdog\Install\SanityCheck

File

modules/mongodb_watchdog/src/Install/SanityCheck.php, line 17

Namespace

Drupal\mongodb_watchdog\Install
View source
class SanityCheck {

  /**
   * The per-collection-size statistics buckets.
   *
   * @var array
   */
  protected $buckets;

  /**
   * The module configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The mongodb.database_factory service.
   *
   * @var \Drupal\mongodb\DatabaseFactory
   */
  protected $dbFactory;

  /**
   * The bucket size values.
   *
   * @var array
   */
  protected $items;

  /**
   * SanityCheck constructor.
   *
   * @param \Drupal\mongodb\DatabaseFactory $dbFactory
   *   The mongodb.database_factory service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config.factory service.
   */
  public function __construct(DatabaseFactory $dbFactory, ConfigFactoryInterface $configFactory) {
    $this->dbFactory = $dbFactory;
    $this->config = $configFactory
      ->get(Logger::CONFIG_NAME);
    $this->items = $this->config
      ->get(Logger::CONFIG_ITEMS);
  }

  /**
   * Build a list of the number of entries per collection in the default DB.
   */
  public function buildCollectionstats() : array {

    /** @var \Drupal\mongodb\DatabaseFactory $databaseFactory */
    $database = $this->dbFactory
      ->get(Logger::DB_LOGGER);
    $this
      ->initBucketsList();
    $collections = $database
      ->listCollections();
    foreach ($collections as $collectionInfo) {
      $name = $collectionInfo
        ->getName();
      $collection = $database
        ->selectCollection($name);
      $count = $collection
        ->countDocuments();
      if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) {
        $this
          ->store($count);
      }
    }
    return $this->buckets;
  }

  /**
   * Prepare a table of bucket to hold the statistics.
   */
  protected function initBucketsList() : void {
    $barCount = 10;
    $barWidth = $this->items / $barCount;
    $buckets = [
      0 => 0,
      1 => 0,
      $this->items - 1 => 0,
      $this->items => 0,
    ];

    // Values 0, 1 and the value of $items are reserved.
    for ($i = 1; $i < $barCount; $i++) {
      $buckets[$i * $barWidth] = 0;
    }
    ksort($buckets);
    $this->buckets = $buckets;
  }

  /**
   * Store a collection document count in its statistics bucket.
   *
   * @param int $value
   *   The value to store.
   */
  protected function store(int $value) : void {
    if ($value <= 1 || $value >= $this->items - 1) {
      $this->buckets[$value]++;
      return;
    }
    $keys = array_slice(array_keys($this->buckets), 1, -1);
    foreach ($keys as $key) {
      if ($value < $key) {
        $this->buckets[$key]++;
        return;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SanityCheck::$buckets protected property The per-collection-size statistics buckets.
SanityCheck::$config protected property The module configuration.
SanityCheck::$dbFactory protected property The mongodb.database_factory service.
SanityCheck::$items protected property The bucket size values.
SanityCheck::buildCollectionstats public function Build a list of the number of entries per collection in the default DB.
SanityCheck::initBucketsList protected function Prepare a table of bucket to hold the statistics.
SanityCheck::store protected function Store a collection document count in its statistics bucket.
SanityCheck::__construct public function SanityCheck constructor.