Completed
Pull Request — 8.x-2.x (#28)
by Frédéric G.
06:12
created

SanityCheckCommand::store()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
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 = $io = new DrupalStyle($input, $output);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
59
60
    $this->buildCollectionstats();
61
    $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
    // 0, 1 and $items are reserved.
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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 $df */
116
    $df = $this->getService('mongodb.database_factory');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $df. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
117
    $db = $df->get('default');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
118
    $this->initBucketsList();
119
120
    $collections = $db->listCollections();
121
    foreach ($collections as $collectionInfo) {
122
      $name = $collectionInfo->getName();
123
      $collection = $db->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