Skip to content

Commit ef6fada

Browse files
authored
Merge b8bbb6d into fd4733a
2 parents fd4733a + b8bbb6d commit ef6fada

File tree

14 files changed

+236
-67
lines changed

14 files changed

+236
-67
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
command:
2+
sanitycheck:
3+
description: 'Greet someone'
4+
arguments:
5+
name: 'Who do you want to greet?'
6+
options:
7+
yell: 'If set, the task will yell in uppercase letters'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: fgm
5+
* Date: 09/08/2016
6+
* Time: 23:21
7+
*/

modules/mongodb_watchdog/mongodb_watchdog.drush.yml

Whitespace-only changes.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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);
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.
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 $df */
116+
$df = $this->getService('mongodb.database_factory');
117+
$db = $df->get('default');
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+
}

modules/mongodb_watchdog/src/Controller/ControllerBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(LoggerInterface $logger, Logger $watchdog, Immutable
5555
* @param array|null $top
5656
* A render array for the top element present on some controllers results.
5757
*
58-
* @return array<string,string|array>
58+
* @return string[string|array]
5959
* A render array for the whole controller.
6060
*/
6161
protected function buildDefaults(array $main, array $top = NULL) {
@@ -78,7 +78,7 @@ protected function buildDefaults(array $main, array $top = NULL) {
7878
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $markup
7979
* The message proper.
8080
*
81-
* @return array<string,string|\Drupal\Core\StringTranslation\TranslatableMarkup>
81+
* @return string[string|\Drupal\Core\StringTranslation\TranslatableMarkup]
8282
* A render array for a message.
8383
*/
8484
protected function buildEmpty(TranslatableMarkup $markup) {

modules/mongodb_watchdog/src/Controller/DetailController.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public function __construct(
5353
* @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
5454
* The event template.
5555
*
56-
* @return array<string,string|array>
56+
* @return string[string|array]
5757
* A render array.
5858
*/
5959
public function build(Request $request, EventTemplate $eventTemplate) {
6060
$top = $this->getTop($eventTemplate);
6161

6262
$rows = $this->getRowData($request, $eventTemplate);
6363
$main = empty($rows)
64-
? $this->buildEmpty(t('No occurrence of this event found in logger.'))
64+
? $this->buildEmpty($this->t('No occurrence of this event found in logger.'))
6565
: $this->buildMainTable($rows, $eventTemplate);
6666

6767
$ret = $this->buildDefaults($main, $top);
@@ -76,13 +76,13 @@ public function build(Request $request, EventTemplate $eventTemplate) {
7676
* @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
7777
* The template for which to built the detail lines.
7878
*
79-
* @return array<string,string|array>
79+
* @return string[string|array]
8080
* A render array for the main table.
8181
*/
8282
protected function buildMainTable(array $events, EventTemplate $eventTemplate) {
8383
$ret = [
8484
'#attributes' => new Attribute(['class' => 'mongodb_watchdog__detail']),
85-
'#caption' => t('Event occurrences'),
85+
'#caption' => $this->t('Event occurrences'),
8686
'#header' => $this->buildMainTableHeader(),
8787
'#rows' => $this->buildMainTableRows($events, $eventTemplate),
8888
'#type' => 'table',
@@ -99,13 +99,13 @@ protected function buildMainTable(array $events, EventTemplate $eventTemplate) {
9999
*/
100100
protected function buildMainTableHeader() {
101101
$header = [
102-
t('Date'),
103-
t('User'),
104-
t('Message'),
105-
t('Location'),
106-
t('Referrer'),
107-
t('Hostname'),
108-
t('Operations'),
102+
$this->t('Date'),
103+
$this->t('User'),
104+
$this->t('Message'),
105+
$this->t('Location'),
106+
$this->t('Referrer'),
107+
$this->t('Hostname'),
108+
$this->t('Operations'),
109109
];
110110

111111
return $header;
@@ -119,7 +119,7 @@ protected function buildMainTableHeader() {
119119
* @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
120120
* The template for these events.
121121
*
122-
* @return array<string,array|string>
122+
* @return string[array|string]
123123
* A render array for a table.
124124
*/
125125
protected function buildMainTableRows(array $events, EventTemplate $eventTemplate) {
@@ -143,7 +143,7 @@ protected function buildMainTableRows(array $events, EventTemplate $eventTemplat
143143
* The page title.
144144
*/
145145
public function buildTitle(EventTemplate $eventTemplate) {
146-
return t('MongoDB events: "@template"', ['@template' => $eventTemplate->message]);
146+
return $this->t('MongoDB events: "@template"', ['@template' => $eventTemplate->message]);
147147
}
148148

149149
/**
@@ -213,7 +213,7 @@ protected function getTop(EventTemplate $eventTemplate = NULL) {
213213
}
214214

215215
$ret = [
216-
'#caption' => t('Event template'),
216+
'#caption' => $this->t('Event template'),
217217
'#rows' => $rows,
218218
'#type' => 'table',
219219
];

modules/mongodb_watchdog/src/Controller/OverviewController.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function build(Request $request) {
120120

121121
$rows = $this->getRowData($request);
122122
$main = empty($rows)
123-
? $this->buildEmpty(t('No event found in logger.'))
123+
? $this->buildEmpty($this->t('No event found in logger.'))
124124
: $this->buildMainTable($rows);
125125

126126
$ret = $this->buildDefaults($main, $top);
@@ -133,7 +133,7 @@ public function build(Request $request) {
133133
* @param \Drupal\mongodb_watchdog\EventTemplate[] $rows
134134
* The template data.
135135
*
136-
* @return array<string,string|array>
136+
* @return string[string|array]
137137
* A render array for the main table.
138138
*/
139139
protected function buildMainTable(array $rows) {
@@ -154,12 +154,12 @@ protected function buildMainTable(array $rows) {
154154
*/
155155
protected function buildMainTableHeader() {
156156
$header = [
157-
t('#'),
158-
t('Latest'),
159-
t('Severity'),
160-
t('Type'),
161-
t('Message'),
162-
t('Source'),
157+
$this->t('#'),
158+
$this->t('Latest'),
159+
$this->t('Severity'),
160+
$this->t('Type'),
161+
$this->t('Message'),
162+
$this->t('Source'),
163163
];
164164

165165
return $header;
@@ -171,7 +171,7 @@ protected function buildMainTableHeader() {
171171
* @param \Drupal\mongodb_watchdog\EventTemplate[] $templates
172172
* The event template data.
173173
*
174-
* @return array<string,array|string>
174+
* @return string[array|string]
175175
* A render array for a table.
176176
*/
177177
protected function buildMainTableRows(array $templates) {
@@ -235,11 +235,11 @@ public static function create(ContainerInterface $container) {
235235
protected function getEventLink(EventTemplate $template) {
236236
switch ($template->type) {
237237
case 'page not found':
238-
$cell = Link::createFromRoute(t('( Top 404 )'), 'mongodb_watchdog.reports.top404');
238+
$cell = Link::createFromRoute($this->t('( Top 404 )'), 'mongodb_watchdog.reports.top404');
239239
break;
240240

241241
case 'access denied':
242-
$cell = Link::createFromRoute(t('( Top 403 )'), 'mongodb_watchdog.reports.top403');
242+
$cell = Link::createFromRoute($this->t('( Top 403 )'), 'mongodb_watchdog.reports.top403');
243243
break;
244244

245245
// Limited-length message.

0 commit comments

Comments
 (0)