Passed
Pull Request — 8.x-2.x (#58)
by Frédéric G.
03:34
created

SanityCheck::initBucketsList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
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.
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
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 values.
42
   *
43
   * @var array
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
  public function buildCollectionstats(): array {
69
    /** @var \Drupal\mongodb\DatabaseFactory $databaseFactory */
70
    $database = $this->dbFactory->get(Logger::DB_LOGGER);
71
    $this->initBucketsList();
72
73
    $collections = $database->listCollections();
74
    foreach ($collections as $collectionInfo) {
75
      $name = $collectionInfo->getName();
76
      $collection = $database->selectCollection($name);
77
      $count = $collection->count();
78
      if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) {
79
        $this->store($count);
80
      }
81
    }
82
83
    return $this->buckets;
84
  }
85
86
  /**
87
   * Prepare a table of bucket to hold the statistics.
88
   */
89
  protected function initBucketsList(): void {
90
    $barCount = 10;
91
    $barWidth = $this->items / $barCount;
92
    $buckets = [
93
      0 => 0,
94
      1 => 0,
95
      $this->items - 1 => 0,
96
      $this->items => 0,
97
    ];
98
99
    // Values 0, 1 and the value of $items are reserved.
100
    for ($i = 1; $i < $barCount; $i++) {
101
      $buckets[$i * $barWidth] = 0;
102
    }
103
    ksort($buckets);
104
    $this->buckets = $buckets;
105
  }
106
107
  /**
108
   * Store a collection document count in its statistics bucket.
109
   *
110
   * @param int $value
111
   *   The value to store.
112
   */
113
  protected function store(int $value): void {
114
    if ($value <= 1 || $value >= $this->items - 1) {
115
      $this->buckets[$value]++;
116
      return;
117
    }
118
    $keys = array_slice(array_keys($this->buckets), 1, -1);
119
120
    foreach ($keys as $key) {
121
      if ($value < $key) {
122
        $this->buckets[$key]++;
123
        return;
124
      }
125
    }
126
  }
127
128
}
129