|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\Debug; |
|
6
|
|
|
use Drupal\Console\Command\ContainerAwareCommand; |
|
7
|
|
|
use Drupal\mongodb_watchdog\Logger; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Drupal\Console\Style\DrupalStyle; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class SanityCheckCommand. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Drupal\mongodb_watchdog |
|
16
|
|
|
*/ |
|
17
|
|
|
class SanityCheckCommand extends ContainerAwareCommand { |
|
18
|
|
|
|
|
19
|
|
|
protected $buckets; |
|
20
|
|
|
|
|
21
|
|
|
protected $items; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function configure() { |
|
27
|
|
|
$this |
|
28
|
|
|
->setName('mongodb:watchdog:sanitycheck') |
|
29
|
|
|
->setDescription($this->trans('Check the sizes of the watchdog collections')); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
36
|
|
|
|
|
37
|
|
|
$this->io = $io = new DrupalStyle($input, $output); |
|
38
|
|
|
|
|
39
|
|
|
$this->buildCollectionstats(); |
|
40
|
|
|
$io->info(print_r($this->buckets, TRUE)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function initBucketsList() { |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$config = $this->getConfigFactory()->get(Logger::CONFIG_NAME); |
|
46
|
|
|
$this->items = $items = $config->get('items'); |
|
47
|
|
|
unset($config); |
|
48
|
|
|
|
|
49
|
|
|
$barCount = 10; |
|
50
|
|
|
$barWidth = $items / $barCount; |
|
51
|
|
|
$buckets = [ |
|
52
|
|
|
0 => 0, |
|
53
|
|
|
1 => 0, |
|
54
|
|
|
$items - 1 => 0, |
|
55
|
|
|
$items => 0, |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
// 0, 1 and $items are reserved. |
|
|
|
|
|
|
59
|
|
|
for ($i = 1 ; $i < $barCount ; $i++) { |
|
|
|
|
|
|
60
|
|
|
$buckets[$i * $barWidth] = 0; |
|
61
|
|
|
} |
|
62
|
|
|
ksort($buckets); |
|
63
|
|
|
$this->buckets = $buckets; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function store(int $value): void { |
|
|
|
|
|
|
67
|
|
|
if ($value <= 1 || $value >= $this->items - 1) { |
|
68
|
|
|
$this->buckets[$value]++; |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
$keys = array_slice(array_keys($this->buckets), 1, -1); |
|
72
|
|
|
foreach ($keys as $key) { |
|
73
|
|
|
if ($value < $key) { |
|
74
|
|
|
$this->buckets[$key]++; |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
function buildCollectionstats() { |
|
|
|
|
|
|
81
|
|
|
/** @var \Drupal\mongodb\DatabaseFactory $df */ |
|
82
|
|
|
$df = $this->getService('mongodb.database_factory'); |
|
83
|
|
|
$db = $df->get('default'); |
|
84
|
|
|
$this->initBucketsList(); |
|
85
|
|
|
|
|
86
|
|
|
$collections = $db->listCollections(); |
|
87
|
|
|
foreach ($collections as $collectionInfo) { |
|
88
|
|
|
$name = $collectionInfo->getName();// . " " . $collection->count() ."\n" |
|
|
|
|
|
|
89
|
|
|
$collection = $db->selectCollection($name); |
|
90
|
|
|
$count = $collection->count(); |
|
91
|
|
|
if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) { |
|
92
|
|
|
$this->store($count); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|