Completed
Branch 2892309-incorrect_watchdog_pag... (29896f)
by Frédéric G.
06:06
created

SanityCheckCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
A execute() 0 7 1
A initBucketsList() 0 22 2
B store() 0 14 5
A buildCollectionstats() 0 16 3
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.
0 ignored issues
show
introduced by
2 spaces found before inline comment; expected "// Values 0, 1 and the value of $items are reserved." but found "// Values 0, 1 and the value of $items are reserved."
Loading history...
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) {
0 ignored issues
show
introduced by
Unknown type hint "int" found for $value
Loading history...
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