SanityCheck.php in MongoDB 8.2
File
modules/mongodb_watchdog/src/Install/SanityCheck.php
View source
<?php
declare (strict_types=1);
namespace Drupal\mongodb_watchdog\Install;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\mongodb\DatabaseFactory;
use Drupal\mongodb_watchdog\Logger;
class SanityCheck {
protected $buckets;
protected $config;
protected $dbFactory;
protected $items;
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);
}
public function buildCollectionstats() : array {
$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;
}
protected function initBucketsList() : void {
$barCount = 10;
$barWidth = $this->items / $barCount;
$buckets = [
0 => 0,
1 => 0,
$this->items - 1 => 0,
$this->items => 0,
];
for ($i = 1; $i < $barCount; $i++) {
$buckets[$i * $barWidth] = 0;
}
ksort($buckets);
$this->buckets = $buckets;
}
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;
}
}
}
}
Classes
Name |
Description |
SanityCheck |
Class SanityCheck provides some reasonableness checks for MongoDB contents. |