|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Console\Command\ContainerAwareCommand; |
|
6
|
|
|
use Drupal\mongodb_watchdog\Logger; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Drupal\Console\Style\DrupalStyle; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class SanityCheckCommand. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Drupal\mongodb_watchdog |
|
15
|
|
|
*/ |
|
16
|
|
|
class SanityCheckCommand extends ContainerAwareCommand { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The per-collection-size statistics buckets. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $buckets; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The bucket size values. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $items; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function configure() { |
|
36
|
|
|
$this |
|
37
|
|
|
->setName('mongodb:watchdog:sanitycheck') |
|
38
|
|
|
->setDescription($this->trans('Check the sizes of the watchdog collections')) |
|
39
|
|
|
->setHelp($this->trans(<<<HELP |
|
40
|
|
|
This command produces a list of the sizes of the watchdog capped collections, |
|
41
|
|
|
grouped by "bucket". The bucket sizes are 0 (empty collection), 1 (single document), one bucket for each fraction of the size of the capping limit |
|
42
|
|
|
(which should be the typical case), one for capping limit - 1, and one for the |
|
43
|
|
|
capping limit itself, showing events occurring too often for the configured |
|
44
|
|
|
limit. |
|
45
|
|
|
|
|
46
|
|
|
For example: with a typical capping limit of 10000, the list will be made of |
|
47
|
|
|
the following buckers: 0, 1, 2-1000, 1001-2000, 2001-3000, ... 9000-9998, |
|
48
|
|
|
9999, and 10000. |
|
49
|
|
|
HELP |
|
50
|
|
|
)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
57
|
|
|
|
|
58
|
|
|
$this->io = new DrupalStyle($input, $output); |
|
59
|
|
|
|
|
60
|
|
|
$this->buildCollectionstats(); |
|
61
|
|
|
$this->io->info(print_r($this->buckets, TRUE)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Prepare a table of bucket to hold the statistics. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function initBucketsList() { |
|
68
|
|
|
|
|
69
|
|
|
$config = $this->getConfigFactory()->get(Logger::CONFIG_NAME); |
|
70
|
|
|
$this->items = $items = $config->get('items'); |
|
71
|
|
|
unset($config); |
|
72
|
|
|
|
|
73
|
|
|
$barCount = 10; |
|
74
|
|
|
$barWidth = $items / $barCount; |
|
75
|
|
|
$buckets = [ |
|
76
|
|
|
0 => 0, |
|
77
|
|
|
1 => 0, |
|
78
|
|
|
$items - 1 => 0, |
|
79
|
|
|
$items => 0, |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
// Values 0, 1 and the value of $items are reserved. |
|
|
|
|
|
|
83
|
|
|
for ($i = 1; $i < $barCount; $i++) { |
|
84
|
|
|
$buckets[$i * $barWidth] = 0; |
|
85
|
|
|
} |
|
86
|
|
|
ksort($buckets); |
|
87
|
|
|
$this->buckets = $buckets; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Store a collection document count in its statistics bucket. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $value |
|
94
|
|
|
* The value to store. |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function store(int $value) { |
|
|
|
|
|
|
97
|
|
|
if ($value <= 1 || $value >= $this->items - 1) { |
|
98
|
|
|
$this->buckets[$value]++; |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
$keys = array_slice(array_keys($this->buckets), 1, -1); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($keys as $key) { |
|
104
|
|
|
if ($value < $key) { |
|
105
|
|
|
$this->buckets[$key]++; |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Build a list of the number of entries per collection in the default DB. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function buildCollectionstats() { |
|
115
|
|
|
/** @var \Drupal\mongodb\DatabaseFactory $databaseFactory */ |
|
116
|
|
|
$databaseFactory = $this->getService('mongodb.database_factory'); |
|
117
|
|
|
$database = $databaseFactory->get('default'); |
|
118
|
|
|
$this->initBucketsList(); |
|
119
|
|
|
|
|
120
|
|
|
$collections = $database->listCollections(); |
|
121
|
|
|
foreach ($collections as $collectionInfo) { |
|
122
|
|
|
$name = $collectionInfo->getName(); |
|
123
|
|
|
$collection = $database->selectCollection($name); |
|
124
|
|
|
$count = $collection->count(); |
|
125
|
|
|
if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) { |
|
126
|
|
|
$this->store($count); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|