1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Drupal\mongodb_watchdog\Install; |
6
|
|
|
|
7
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
|
8
|
|
|
use Drupal\mongodb\DatabaseFactory; |
9
|
|
|
use Drupal\mongodb_watchdog\Logger; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SanityCheck provides some reasonableness checks for MongoDB contents. |
13
|
|
|
* |
14
|
|
|
* @see \Drupal\mongodb_watchdog\Command\SanityCheckCommand |
15
|
|
|
* @see \Drupal\mongodb_watchdog\Commands\MongoDbWatchdogCommands::sanityCheck() |
16
|
|
|
*/ |
17
|
|
|
class SanityCheck { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The per-collection-size statistics buckets. |
21
|
|
|
* |
22
|
|
|
* @var array{0: int, 1: int, 2: int, 3: int} |
23
|
|
|
*/ |
24
|
|
|
protected $buckets; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The module configuration. |
28
|
|
|
* |
29
|
|
|
* @var \Drupal\Core\Config\ImmutableConfig |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
protected $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The mongodb.database_factory service. |
35
|
|
|
* |
36
|
|
|
* @var \Drupal\mongodb\DatabaseFactory |
37
|
|
|
*/ |
38
|
|
|
protected $dbFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The bucket size. |
42
|
|
|
* |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $items; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* SanityCheck constructor. |
49
|
|
|
* |
50
|
|
|
* @param \Drupal\mongodb\DatabaseFactory $dbFactory |
51
|
|
|
* The mongodb.database_factory service. |
52
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory |
53
|
|
|
* The config.factory service. |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
DatabaseFactory $dbFactory, |
57
|
|
|
ConfigFactoryInterface $configFactory |
58
|
|
|
) { |
59
|
|
|
$this->dbFactory = $dbFactory; |
60
|
|
|
|
61
|
|
|
$this->config = $configFactory->get(Logger::CONFIG_NAME); |
62
|
|
|
$this->items = $this->config->get(Logger::CONFIG_ITEMS); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Build a list of the number of entries per collection in the default DB. |
67
|
|
|
* |
68
|
|
|
* @return array{0: int, 1: int, 2: int, 3: int} |
69
|
|
|
* The fill level statistics of the buckets: empty, single, max-1, max. |
70
|
|
|
*/ |
71
|
|
|
public function buildCollectionstats(): array { |
72
|
|
|
/** @var \MongoDB\Database $database */ |
73
|
|
|
$database = $this->dbFactory->get(Logger::DB_LOGGER); |
74
|
|
|
$this->initBucketsList(); |
75
|
|
|
|
76
|
|
|
$collections = $database->listCollections(); |
77
|
|
|
foreach ($collections as $collectionInfo) { |
78
|
|
|
$name = $collectionInfo->getName(); |
79
|
|
|
$collection = $database->selectCollection($name); |
80
|
|
|
$count = $collection->countDocuments(); |
81
|
|
|
if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) { |
82
|
|
|
$this->store($count); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->buckets; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Prepare a table of bucket to hold the statistics. |
91
|
|
|
*/ |
92
|
|
|
protected function initBucketsList(): void { |
93
|
|
|
$barCount = 10; |
94
|
|
|
$barWidth = $this->items / $barCount; |
95
|
|
|
$buckets = [ |
96
|
|
|
0 => 0, |
97
|
|
|
1 => 0, |
98
|
|
|
$this->items - 1 => 0, |
99
|
|
|
$this->items => 0, |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
// Values 0, 1 and the value of $items are reserved. |
103
|
|
|
for ($i = 1; $i < $barCount; $i++) { |
104
|
|
|
$buckets[$i * $barWidth] = 0; |
105
|
|
|
} |
106
|
|
|
ksort($buckets); |
107
|
|
|
$this->buckets = $buckets; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Store a collection document count in its statistics bucket. |
112
|
|
|
* |
113
|
|
|
* @param int $value |
114
|
|
|
* The value to store. |
115
|
|
|
*/ |
116
|
|
|
protected function store(int $value): void { |
117
|
|
|
if ($value <= 1 || $value >= $this->items - 1) { |
118
|
|
|
$this->buckets[$value]++; |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
$keys = array_slice(array_keys($this->buckets), 1, -1); |
122
|
|
|
|
123
|
|
|
foreach ($keys as $key) { |
124
|
|
|
if ($value < $key) { |
125
|
|
|
$this->buckets[$key]++; |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths