Last active
October 26, 2022 13:58
-
-
Save ivangrozni/1fb809319ed25eb817e3fda3ae15df0d to your computer and use it in GitHub Desktop.
dev-2024--drupal-monitoring-requirements-recap.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From a62d7c3d7283ad152473afec92ac1f6d65dc2d78 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Fri, 2 Sep 2022 13:04:31 +0200 | |
Subject: [PATCH 01/13] Add requirements recap sensor plugin. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 156 ++++++++++++++++++ | |
1 file changed, 156 insertions(+) | |
create mode 100644 src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
new file mode 100644 | |
index 0000000..b612b73 | |
--- /dev/null | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -0,0 +1,156 @@ | |
+<?php | |
+/** | |
+ * @file | |
+ * Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\RequirementsRecapSensorPlugin. | |
+ */ | |
+ | |
+namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin; | |
+ | |
+use Drupal\Core\Form\FormStateInterface; | |
+use Drupal\system\SystemManager; | |
+use Drupal\monitoring\Entity\SensorConfig; | |
+use Drupal\monitoring\Result\SensorResultInterface; | |
+use Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface; | |
+use Drupal\monitoring\SensorPlugin\SensorPluginBase; | |
+ | |
+/** | |
+ * Requirements recap sensor plugin that gathers requirements checks by severity. | |
+ * | |
+ * @SensorPlugin( | |
+ * id = "requirements_recap", | |
+ * label = @Translation("Requirements Recap"), | |
+ * description = @Translation("Aggregator of hook requirements messages."), | |
+ * addable = TRUE | |
+ * ) | |
+ */ | |
+class RequirementsRecapSensorPlugin extends SensorPluginBase implements ExtendedInfoSensorPluginInterface { | |
+ | |
+ /** | |
+ * Sensor manager service. | |
+ * | |
+ * @var \Drupal\system\SystemManager; | |
+ */ | |
+ protected $systemManager; | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function __construct(SensorConfig $sensor_config, $plugin_id, $plugin_definition) { | |
+ $this->systemManager = $this->getService('system.manager'); | |
+ parent::__construct($sensor_config, $plugin_id, $plugin_definition); | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function runSensor(SensorResultInterface $sensor_result) { | |
+ $severity = (int) $this->sensorConfig->getSetting('severity'); | |
+ $requirements = array_filter($this->systemManager->listRequirements(), function ($requirement) use ($severity) { | |
+ return isset($requirement['severity']) && $requirement['severity'] === $severity; | |
+ }); | |
+ $sensor_result->requirements = $requirements; | |
+ $sensor_result->setValue(count($requirements)); | |
+ if (count($requirements)) { | |
+ foreach ($requirements as $requirement) { | |
+ $sensor_result->addStatusMessage(' @title@value', [ | |
+ '@title' => $requirement['title'] ? "{$requirement['title']}" : '', | |
+ '@value' => isset($requirement['value']) ? ": {$requirement['value']}" : '', | |
+ ]); | |
+ } | |
+ } | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function resultVerbose(SensorResultInterface $result) { | |
+ $counter = 0; | |
+ if ($requirements = $result->requirements) { | |
+ $output['table'] = [ | |
+ '#type' => 'table', | |
+ '#header' => [ | |
+ 'label' => [ | |
+ 'data' => $this->t('Label'), | |
+ ], | |
+ 'value' => [ | |
+ 'data' => $this->t('Value'), | |
+ ], | |
+ 'description' => [ | |
+ 'data' => $this->t('Description'), | |
+ ], | |
+ ], | |
+ ]; | |
+ $renderer = $this->getService('renderer'); | |
+ foreach ($requirements as $key => $requirement) { | |
+ $title = (string) $requirement['title']; | |
+ $value = isset($requirement['value']) ? (string) $requirement['value'] : ''; | |
+ $description = $requirement['description'] ?? ''; | |
+ $description = is_array($description) ? $description : ['#markup' => (string) $description]; | |
+ $output['table'][$counter]['label'] = [ | |
+ '#type' => 'item', | |
+ '#plain_text' => $title, | |
+ ]; | |
+ $output['table'][$counter]['value'] = [ | |
+ '#type' => 'item', | |
+ '#plain_text' => $value, | |
+ ]; | |
+ $output['table'][$counter]['description'] = [ | |
+ '#type' => 'item', | |
+ '#markup' => (string) $renderer->renderPlain($description), | |
+ ]; | |
+ $counter++; | |
+ } | |
+ return $output; | |
+ } | |
+ return []; | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function getDefaultConfiguration() { | |
+ $default_config = parent::getDefaultConfiguration(); | |
+ $default_config['settings']['severity'] = SystemManager::REQUIREMENT_ERROR; | |
+ $default_config['category'] = $this->t('Requirements'); | |
+ $default_config['value_label'] = $this->t('Errors in status report'); | |
+ $default_config['description'] = $this->t('Number of errors in status report checks.'); | |
+ $default_config['value_type'] = 'number'; | |
+ $default_config['cache_time'] = 3600; | |
+ $default_config['thresholds'] = [ | |
+ 'type' => 'exceeds', | |
+ 'critical' => 1, | |
+ ]; | |
+ return $default_config; | |
+ } | |
+ | |
+ /** | |
+ * Adds UI for variables config object and key. | |
+ */ | |
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
+ $form = parent::buildConfigurationForm($form, $form_state); | |
+ // Add weight to display config key before expected value. | |
+ $form['severity'] = array( | |
+ '#type' => 'select', | |
+ '#default_value' => $this->sensorConfig->getSetting('severity') ?? SystemManager::REQUIREMENT_ERROR, | |
+ '#options' => $this->getSeverityOptions(), | |
+ '#title' => t('Requirements Severity'), | |
+ '#required' => TRUE, | |
+ '#weight' => -1, | |
+ ); | |
+ return $form; | |
+ } | |
+ | |
+ /** | |
+ * Get options for | |
+ * | |
+ * @return array | |
+ */ | |
+ protected function getSeverityOptions() { | |
+ return [ | |
+ SystemManager::REQUIREMENT_ERROR => $this->t('Error'), | |
+ SystemManager::REQUIREMENT_WARNING => $this->t('Warning'), | |
+ SystemManager::REQUIREMENT_OK => $this->t('Ok'), | |
+ ]; | |
+ } | |
+ | |
+} | |
-- | |
GitLab | |
From a11be5f1d93d71ff5e77fca946859d0b2a1640a9 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Fri, 2 Sep 2022 13:09:51 +0200 | |
Subject: [PATCH 02/13] Fix code style. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 18 +++++++----------- | |
1 file changed, 7 insertions(+), 11 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index b612b73..f4c53cb 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -1,8 +1,4 @@ | |
<?php | |
-/** | |
- * @file | |
- * Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\RequirementsRecapSensorPlugin. | |
- */ | |
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin; | |
@@ -14,7 +10,7 @@ use Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface; | |
use Drupal\monitoring\SensorPlugin\SensorPluginBase; | |
/** | |
- * Requirements recap sensor plugin that gathers requirements checks by severity. | |
+ * Requirements recap sensor that gathers requirements checks by severity. | |
* | |
* @SensorPlugin( | |
* id = "requirements_recap", | |
@@ -28,7 +24,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
/** | |
* Sensor manager service. | |
* | |
- * @var \Drupal\system\SystemManager; | |
+ * @var \Drupal\system\SystemManager | |
*/ | |
protected $systemManager; | |
@@ -81,7 +77,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
], | |
]; | |
$renderer = $this->getService('renderer'); | |
- foreach ($requirements as $key => $requirement) { | |
+ foreach ($requirements as $requirement) { | |
$title = (string) $requirement['title']; | |
$value = isset($requirement['value']) ? (string) $requirement['value'] : ''; | |
$description = $requirement['description'] ?? ''; | |
@@ -129,19 +125,19 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
$form = parent::buildConfigurationForm($form, $form_state); | |
// Add weight to display config key before expected value. | |
- $form['severity'] = array( | |
+ $form['severity'] = [ | |
'#type' => 'select', | |
'#default_value' => $this->sensorConfig->getSetting('severity') ?? SystemManager::REQUIREMENT_ERROR, | |
'#options' => $this->getSeverityOptions(), | |
- '#title' => t('Requirements Severity'), | |
+ '#title' => $this->t('Requirements Severity'), | |
'#required' => TRUE, | |
'#weight' => -1, | |
- ); | |
+ ]; | |
return $form; | |
} | |
/** | |
- * Get options for | |
+ * Get options for requirements severity. | |
* | |
* @return array | |
*/ | |
-- | |
GitLab | |
From 86b9a4b7f77306ef57cd28514c479aafc581c5ac Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Tue, 13 Sep 2022 12:49:42 +0200 | |
Subject: [PATCH 03/13] Improve readability. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 70 +++++++++---------- | |
1 file changed, 35 insertions(+), 35 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index f4c53cb..36e3c45 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -60,45 +60,45 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
* {@inheritdoc} | |
*/ | |
public function resultVerbose(SensorResultInterface $result) { | |
+ if (!$requirements = $result->requirements) { | |
+ return []; | |
+ } | |
$counter = 0; | |
- if ($requirements = $result->requirements) { | |
- $output['table'] = [ | |
- '#type' => 'table', | |
- '#header' => [ | |
- 'label' => [ | |
- 'data' => $this->t('Label'), | |
- ], | |
- 'value' => [ | |
- 'data' => $this->t('Value'), | |
- ], | |
- 'description' => [ | |
- 'data' => $this->t('Description'), | |
- ], | |
+ $output['table'] = [ | |
+ '#type' => 'table', | |
+ '#header' => [ | |
+ 'label' => [ | |
+ 'data' => $this->t('Label'), | |
+ ], | |
+ 'value' => [ | |
+ 'data' => $this->t('Value'), | |
], | |
+ 'description' => [ | |
+ 'data' => $this->t('Description'), | |
+ ], | |
+ ], | |
+ ]; | |
+ $renderer = $this->getService('renderer'); | |
+ foreach ($requirements as $requirement) { | |
+ $title = (string) $requirement['title']; | |
+ $value = isset($requirement['value']) ? (string) $requirement['value'] : ''; | |
+ $description = $requirement['description'] ?? ''; | |
+ $description = is_array($description) ? $description : ['#markup' => (string) $description]; | |
+ $output['table'][$counter]['label'] = [ | |
+ '#type' => 'item', | |
+ '#plain_text' => $title, | |
]; | |
- $renderer = $this->getService('renderer'); | |
- foreach ($requirements as $requirement) { | |
- $title = (string) $requirement['title']; | |
- $value = isset($requirement['value']) ? (string) $requirement['value'] : ''; | |
- $description = $requirement['description'] ?? ''; | |
- $description = is_array($description) ? $description : ['#markup' => (string) $description]; | |
- $output['table'][$counter]['label'] = [ | |
- '#type' => 'item', | |
- '#plain_text' => $title, | |
- ]; | |
- $output['table'][$counter]['value'] = [ | |
- '#type' => 'item', | |
- '#plain_text' => $value, | |
- ]; | |
- $output['table'][$counter]['description'] = [ | |
- '#type' => 'item', | |
- '#markup' => (string) $renderer->renderPlain($description), | |
- ]; | |
- $counter++; | |
- } | |
- return $output; | |
+ $output['table'][$counter]['value'] = [ | |
+ '#type' => 'item', | |
+ '#plain_text' => $value, | |
+ ]; | |
+ $output['table'][$counter]['description'] = [ | |
+ '#type' => 'item', | |
+ '#markup' => (string) $renderer->renderPlain($description), | |
+ ]; | |
+ $counter++; | |
} | |
- return []; | |
+ return $output; | |
} | |
/** | |
-- | |
GitLab | |
From fa2b207c1a9c8c829c7bc9d3a9c53f07079c6031 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Fri, 16 Sep 2022 13:03:51 +0200 | |
Subject: [PATCH 04/13] Add html decoding to messages. | |
--- | |
.../SensorPlugin/RequirementsRecapSensorPlugin.php | 7 ++++--- | |
1 file changed, 4 insertions(+), 3 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 36e3c45..6190c86 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -48,9 +48,10 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
$sensor_result->setValue(count($requirements)); | |
if (count($requirements)) { | |
foreach ($requirements as $requirement) { | |
+ $value = isset($requirement['value']) ? html_entity_decode($requirement['value']) : ''; | |
$sensor_result->addStatusMessage(' @title@value', [ | |
'@title' => $requirement['title'] ? "{$requirement['title']}" : '', | |
- '@value' => isset($requirement['value']) ? ": {$requirement['value']}" : '', | |
+ '@value' => isset($requirement['value']) ? ": {$value}" : '', | |
]); | |
} | |
} | |
@@ -90,11 +91,11 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
]; | |
$output['table'][$counter]['value'] = [ | |
'#type' => 'item', | |
- '#plain_text' => $value, | |
+ '#plain_text' => html_entity_decode($value), | |
]; | |
$output['table'][$counter]['description'] = [ | |
'#type' => 'item', | |
- '#markup' => (string) $renderer->renderPlain($description), | |
+ '#markup' => html_entity_decode((string) $renderer->renderPlain($description)), | |
]; | |
$counter++; | |
} | |
-- | |
GitLab | |
From 0b08d88282fb53e73c30ab74602ecaf3c7b80ef2 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Mon, 19 Sep 2022 16:35:21 +0200 | |
Subject: [PATCH 05/13] Use drupal utility component to decode enities. | |
--- | |
.../SensorPlugin/RequirementsRecapSensorPlugin.php | 7 ++++--- | |
1 file changed, 4 insertions(+), 3 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 6190c86..1829114 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -2,6 +2,7 @@ | |
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin; | |
+use Drupal\Component\Utility\Html; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\system\SystemManager; | |
use Drupal\monitoring\Entity\SensorConfig; | |
@@ -48,7 +49,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
$sensor_result->setValue(count($requirements)); | |
if (count($requirements)) { | |
foreach ($requirements as $requirement) { | |
- $value = isset($requirement['value']) ? html_entity_decode($requirement['value']) : ''; | |
+ $value = isset($requirement['value']) ? Html::decodeEntities($requirement['value']) : ''; | |
$sensor_result->addStatusMessage(' @title@value', [ | |
'@title' => $requirement['title'] ? "{$requirement['title']}" : '', | |
'@value' => isset($requirement['value']) ? ": {$value}" : '', | |
@@ -91,11 +92,11 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
]; | |
$output['table'][$counter]['value'] = [ | |
'#type' => 'item', | |
- '#plain_text' => html_entity_decode($value), | |
+ '#plain_text' => Html::decodeEntities($value), | |
]; | |
$output['table'][$counter]['description'] = [ | |
'#type' => 'item', | |
- '#markup' => html_entity_decode((string) $renderer->renderPlain($description)), | |
+ '#markup' => Html::decodeEntities((string) $renderer->renderPlain($description)), | |
]; | |
$counter++; | |
} | |
-- | |
GitLab | |
From 5c62ee6214a7c081fb7b7e0762dddfafd168bb37 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Mon, 19 Sep 2022 17:03:48 +0200 | |
Subject: [PATCH 06/13] Improve language, severity is lowercase. | |
--- | |
.../monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | 2 +- | |
1 file changed, 1 insertion(+), 1 deletion(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 1829114..1f86971 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -131,7 +131,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'#type' => 'select', | |
'#default_value' => $this->sensorConfig->getSetting('severity') ?? SystemManager::REQUIREMENT_ERROR, | |
'#options' => $this->getSeverityOptions(), | |
- '#title' => $this->t('Requirements Severity'), | |
+ '#title' => $this->t('Requirements severity'), | |
'#required' => TRUE, | |
'#weight' => -1, | |
]; | |
-- | |
GitLab | |
From 10efeacbd5bb5615d74d219913cbaebe8ad72627 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Wed, 5 Oct 2022 11:43:50 +0200 | |
Subject: [PATCH 07/13] Allow enabling and disabling of requirements in plugin | |
configuration. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 40 +++++++++++++++++-- | |
1 file changed, 37 insertions(+), 3 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 1f86971..34bfbb0 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -42,7 +42,13 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
*/ | |
public function runSensor(SensorResultInterface $sensor_result) { | |
$severity = (int) $this->sensorConfig->getSetting('severity'); | |
- $requirements = array_filter($this->systemManager->listRequirements(), function ($requirement) use ($severity) { | |
+ $enabled_requirements = $this->sensorConfig->getSetting('requirements'); | |
+ // Configuration keys can't have dots in them and are replaced with `__`. | |
+ array_walk($enabled_requirements, function (&$val) { | |
+ $val = str_replace('.', '__', $val); | |
+ }); | |
+ $requirements = array_intersect_key($this->systemManager->listRequirements(), array_flip($enabled_requirements)); | |
+ $requirements = array_filter($requirements, function ($requirement) use ($severity) { | |
return isset($requirement['severity']) && $requirement['severity'] === $severity; | |
}); | |
$sensor_result->requirements = $requirements; | |
@@ -52,7 +58,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
$value = isset($requirement['value']) ? Html::decodeEntities($requirement['value']) : ''; | |
$sensor_result->addStatusMessage(' @title@value', [ | |
'@title' => $requirement['title'] ? "{$requirement['title']}" : '', | |
- '@value' => isset($requirement['value']) ? ": {$value}" : '', | |
+ '@value' => $value ? ": {$value}" : '', | |
]); | |
} | |
} | |
@@ -118,6 +124,8 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'type' => 'exceeds', | |
'critical' => 1, | |
]; | |
+ $all_requirements = $this->getAllRequirements(); | |
+ $default_config['settings']['requirements'] = array_combine(array_keys($all_requirements), array_keys($all_requirements)); | |
return $default_config; | |
} | |
@@ -131,13 +139,39 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'#type' => 'select', | |
'#default_value' => $this->sensorConfig->getSetting('severity') ?? SystemManager::REQUIREMENT_ERROR, | |
'#options' => $this->getSeverityOptions(), | |
- '#title' => $this->t('Requirements severity'), | |
+ '#title' => $this->t('Requirements Severity'), | |
'#required' => TRUE, | |
'#weight' => -1, | |
]; | |
+ $all_requirements = $this->getAllRequirements(); | |
+ $form['requirements'] = [ | |
+ '#type' => 'checkboxes', | |
+ '#default_value' => $this->sensorConfig->getSetting('requirements') ?? array_combine(array_keys($all_requirements), array_keys($all_requirements)), | |
+ '#options' => $all_requirements, | |
+ '#title' => $this->t('Enabled requirements for sensor'), | |
+ '#weight' => 0, | |
+ ]; | |
return $form; | |
} | |
+ /** | |
+ * List of all requirements used of form options. | |
+ * | |
+ * @return array | |
+ * All requirements. | |
+ */ | |
+ protected function getAllRequirements() { | |
+ $requirements = $this->systemManager->listRequirements(); | |
+ $all_requirements = []; | |
+ foreach ($requirements as $id => $requirement) { | |
+ if (strpos($id, '.') !== FALSE) { | |
+ $id = str_replace('.', '__', $id); | |
+ } | |
+ $all_requirements[$id] = is_string($requirement['title']) ? $requirement['title'] : $requirement['title']->__toString(); | |
+ } | |
+ return $all_requirements; | |
+ } | |
+ | |
/** | |
* Get options for requirements severity. | |
* | |
-- | |
GitLab | |
From 3d31eeeccda9ae16fa76b49afae58a6b400044be Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Wed, 5 Oct 2022 11:56:47 +0200 | |
Subject: [PATCH 08/13] Small fix in cache time. | |
--- | |
.../monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | 2 +- | |
1 file changed, 1 insertion(+), 1 deletion(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 34bfbb0..fc38628 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -119,7 +119,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
$default_config['value_label'] = $this->t('Errors in status report'); | |
$default_config['description'] = $this->t('Number of errors in status report checks.'); | |
$default_config['value_type'] = 'number'; | |
- $default_config['cache_time'] = 3600; | |
+ $default_config['caching_time'] = 3600; | |
$default_config['thresholds'] = [ | |
'type' => 'exceeds', | |
'critical' => 1, | |
-- | |
GitLab | |
From 4f1ccd8c9b29120f3e4cfb411844f82d6f8202c0 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Fri, 7 Oct 2022 13:18:11 +0200 | |
Subject: [PATCH 09/13] Track disabled requirements instead of enabled ones. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 17 +++++++++-------- | |
1 file changed, 9 insertions(+), 8 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index fc38628..9f8473c 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -42,12 +42,12 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
*/ | |
public function runSensor(SensorResultInterface $sensor_result) { | |
$severity = (int) $this->sensorConfig->getSetting('severity'); | |
- $enabled_requirements = $this->sensorConfig->getSetting('requirements'); | |
+ $disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements'); | |
// Configuration keys can't have dots in them and are replaced with `__`. | |
- array_walk($enabled_requirements, function (&$val) { | |
+ array_walk($disabled_requirements, function (&$val) { | |
$val = str_replace('.', '__', $val); | |
}); | |
- $requirements = array_intersect_key($this->systemManager->listRequirements(), array_flip($enabled_requirements)); | |
+ $requirements = array_diff_key($this->systemManager->listRequirements(), array_flip($disabled_requirements)); | |
$requirements = array_filter($requirements, function ($requirement) use ($severity) { | |
return isset($requirement['severity']) && $requirement['severity'] === $severity; | |
}); | |
@@ -124,8 +124,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'type' => 'exceeds', | |
'critical' => 1, | |
]; | |
- $all_requirements = $this->getAllRequirements(); | |
- $default_config['settings']['requirements'] = array_combine(array_keys($all_requirements), array_keys($all_requirements)); | |
+ $default_config['settings']['requirements'] = []; | |
return $default_config; | |
} | |
@@ -140,15 +139,17 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'#default_value' => $this->sensorConfig->getSetting('severity') ?? SystemManager::REQUIREMENT_ERROR, | |
'#options' => $this->getSeverityOptions(), | |
'#title' => $this->t('Requirements Severity'), | |
+ '#description' => $this->t('Requirements of selected severity are checked in this sensor.'), | |
'#required' => TRUE, | |
'#weight' => -1, | |
]; | |
$all_requirements = $this->getAllRequirements(); | |
- $form['requirements'] = [ | |
+ $form['disabled_requirements'] = [ | |
'#type' => 'checkboxes', | |
- '#default_value' => $this->sensorConfig->getSetting('requirements') ?? array_combine(array_keys($all_requirements), array_keys($all_requirements)), | |
+ '#default_value' => $this->sensorConfig->getSetting('disabled_requirements') ?? [], | |
'#options' => $all_requirements, | |
- '#title' => $this->t('Enabled requirements for sensor'), | |
+ '#title' => $this->t('Requirements to ignore'), | |
+ '#description' => $this->t('Select requirements that should not be checked when this sensor is run.'), | |
'#weight' => 0, | |
]; | |
return $form; | |
-- | |
GitLab | |
From 97381d70c7de544d72090412fdb1a19f5bd1dcf3 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Tue, 11 Oct 2022 17:39:17 +0200 | |
Subject: [PATCH 10/13] Small fix in default config. | |
--- | |
.../monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | 2 +- | |
1 file changed, 1 insertion(+), 1 deletion(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 9f8473c..55b81f3 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -124,7 +124,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'type' => 'exceeds', | |
'critical' => 1, | |
]; | |
- $default_config['settings']['requirements'] = []; | |
+ $default_config['settings']['disabled_requirements'] = []; | |
return $default_config; | |
} | |
-- | |
GitLab | |
From edbaf5d2ab0468d2c1445e8131a4373a909a02dc Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Wed, 12 Oct 2022 14:22:22 +0200 | |
Subject: [PATCH 11/13] Make sure disabled requirements is an array. | |
--- | |
.../monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | 2 +- | |
1 file changed, 1 insertion(+), 1 deletion(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 55b81f3..82da098 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -42,7 +42,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
*/ | |
public function runSensor(SensorResultInterface $sensor_result) { | |
$severity = (int) $this->sensorConfig->getSetting('severity'); | |
- $disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements'); | |
+ $disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements', []); | |
// Configuration keys can't have dots in them and are replaced with `__`. | |
array_walk($disabled_requirements, function (&$val) { | |
$val = str_replace('.', '__', $val); | |
-- | |
GitLab | |
From e7a007ef620171de96ea0aae7cdda2c89bf806d6 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Mon, 24 Oct 2022 21:13:36 +0200 | |
Subject: [PATCH 12/13] Implement berdirs PR review comments. Caching, better | |
disabled sensor config. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 42 ++++++++++++++----- | |
1 file changed, 32 insertions(+), 10 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 82da098..7e9070b 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -22,6 +22,8 @@ use Drupal\monitoring\SensorPlugin\SensorPluginBase; | |
*/ | |
class RequirementsRecapSensorPlugin extends SensorPluginBase implements ExtendedInfoSensorPluginInterface { | |
+ const REQUIREMENTS_RECAP_CACHE_ID = 'monitoring_requirements_recap_cache'; | |
+ | |
/** | |
* Sensor manager service. | |
* | |
@@ -29,11 +31,19 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
*/ | |
protected $systemManager; | |
+ /** | |
+ * The cache object associated with the default bin. | |
+ * | |
+ * @var \Drupal\Core\Cache\CacheBackendInterface | |
+ */ | |
+ protected $cache; | |
+ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(SensorConfig $sensor_config, $plugin_id, $plugin_definition) { | |
$this->systemManager = $this->getService('system.manager'); | |
+ $this->cache = $this->getService('cache.default'); | |
parent::__construct($sensor_config, $plugin_id, $plugin_definition); | |
} | |
@@ -42,11 +52,13 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
*/ | |
public function runSensor(SensorResultInterface $sensor_result) { | |
$severity = (int) $this->sensorConfig->getSetting('severity'); | |
+ $all_requirements = $this->cache->get(self::REQUIREMENTS_RECAP_CACHE_ID); | |
+ if (!$all_requirements) { | |
+ $all_requirements = $this->systemManager->listRequirements(); | |
+ $caching_time = $this->sensorConfig->getCachingTime() ?? 0; | |
+ $this->cache->set(self::REQUIREMENTS_RECAP_CACHE_ID, $all_requirements, time() + $caching_time); | |
+ } | |
$disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements', []); | |
- // Configuration keys can't have dots in them and are replaced with `__`. | |
- array_walk($disabled_requirements, function (&$val) { | |
- $val = str_replace('.', '__', $val); | |
- }); | |
$requirements = array_diff_key($this->systemManager->listRequirements(), array_flip($disabled_requirements)); | |
$requirements = array_filter($requirements, function ($requirement) use ($severity) { | |
return isset($requirement['severity']) && $requirement['severity'] === $severity; | |
@@ -124,7 +136,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'type' => 'exceeds', | |
'critical' => 1, | |
]; | |
- $default_config['settings']['disabled_requirements'] = []; | |
+ $default_config['settings']['disabled_requirements'] = ['cron', 'update']; | |
return $default_config; | |
} | |
@@ -144,9 +156,12 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'#weight' => -1, | |
]; | |
$all_requirements = $this->getAllRequirements(); | |
+ if ($disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements')) { | |
+ $default_value = array_combine($disabled_requirements, $disabled_requirements); | |
+ } | |
$form['disabled_requirements'] = [ | |
'#type' => 'checkboxes', | |
- '#default_value' => $this->sensorConfig->getSetting('disabled_requirements') ?? [], | |
+ '#default_value' => $default_value ?? [], | |
'#options' => $all_requirements, | |
'#title' => $this->t('Requirements to ignore'), | |
'#description' => $this->t('Select requirements that should not be checked when this sensor is run.'), | |
@@ -155,6 +170,16 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
return $form; | |
} | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
+ $values = $form_state->getValues()['settings']['disabled_requirements']; | |
+ $disabled_requirements = array_values(array_filter($values ?? [])); | |
+ $this->sensorConfig->settings['disabled_requirements'] = $disabled_requirements; | |
+ } | |
+ | |
+ | |
/** | |
* List of all requirements used of form options. | |
* | |
@@ -165,10 +190,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
$requirements = $this->systemManager->listRequirements(); | |
$all_requirements = []; | |
foreach ($requirements as $id => $requirement) { | |
- if (strpos($id, '.') !== FALSE) { | |
- $id = str_replace('.', '__', $id); | |
- } | |
- $all_requirements[$id] = is_string($requirement['title']) ? $requirement['title'] : $requirement['title']->__toString(); | |
+ $all_requirements[$id] = (string) $requirement['title']; | |
} | |
return $all_requirements; | |
} | |
-- | |
GitLab | |
From 2626fb23ab26ed5440b39c77ebf77cc06e81dd33 Mon Sep 17 00:00:00 2001 | |
From: Lio Novelli <liopold@drunomics.com> | |
Date: Wed, 26 Oct 2022 15:54:53 +0200 | |
Subject: [PATCH 13/13] Improve caching and output. | |
--- | |
.../RequirementsRecapSensorPlugin.php | 58 +++++++++++-------- | |
1 file changed, 35 insertions(+), 23 deletions(-) | |
diff --git a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
index 7e9070b..ff1442c 100644 | |
--- a/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
+++ b/src/Plugin/monitoring/SensorPlugin/RequirementsRecapSensorPlugin.php | |
@@ -50,16 +50,11 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
/** | |
* {@inheritdoc} | |
*/ | |
- public function runSensor(SensorResultInterface $sensor_result) { | |
+ public function runSensor(SensorResultInterface $sensor_result): void { | |
$severity = (int) $this->sensorConfig->getSetting('severity'); | |
- $all_requirements = $this->cache->get(self::REQUIREMENTS_RECAP_CACHE_ID); | |
- if (!$all_requirements) { | |
- $all_requirements = $this->systemManager->listRequirements(); | |
- $caching_time = $this->sensorConfig->getCachingTime() ?? 0; | |
- $this->cache->set(self::REQUIREMENTS_RECAP_CACHE_ID, $all_requirements, time() + $caching_time); | |
- } | |
+ $all_requirements = $this->getAllRequirements(); | |
$disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements', []); | |
- $requirements = array_diff_key($this->systemManager->listRequirements(), array_flip($disabled_requirements)); | |
+ $requirements = array_diff_key($all_requirements, array_flip($disabled_requirements)); | |
$requirements = array_filter($requirements, function ($requirement) use ($severity) { | |
return isset($requirement['severity']) && $requirement['severity'] === $severity; | |
}); | |
@@ -67,9 +62,10 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
$sensor_result->setValue(count($requirements)); | |
if (count($requirements)) { | |
foreach ($requirements as $requirement) { | |
- $value = isset($requirement['value']) ? Html::decodeEntities($requirement['value']) : ''; | |
+ $value = isset($requirement['value']) ? strip_tags(Html::decodeEntities($requirement['value'])) : ''; | |
+ $title = $requirement['title'] ? "{$requirement['title']}" : ''; | |
$sensor_result->addStatusMessage(' @title@value', [ | |
- '@title' => $requirement['title'] ? "{$requirement['title']}" : '', | |
+ '@title' => $title, | |
'@value' => $value ? ": {$value}" : '', | |
]); | |
} | |
@@ -79,7 +75,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
/** | |
* {@inheritdoc} | |
*/ | |
- public function resultVerbose(SensorResultInterface $result) { | |
+ public function resultVerbose(SensorResultInterface $result): array { | |
if (!$requirements = $result->requirements) { | |
return []; | |
} | |
@@ -124,7 +120,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
/** | |
* {@inheritdoc} | |
*/ | |
- public function getDefaultConfiguration() { | |
+ public function getDefaultConfiguration(): array { | |
$default_config = parent::getDefaultConfiguration(); | |
$default_config['settings']['severity'] = SystemManager::REQUIREMENT_ERROR; | |
$default_config['category'] = $this->t('Requirements'); | |
@@ -143,7 +139,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
/** | |
* Adds UI for variables config object and key. | |
*/ | |
- public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { | |
$form = parent::buildConfigurationForm($form, $form_state); | |
// Add weight to display config key before expected value. | |
$form['severity'] = [ | |
@@ -155,7 +151,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
'#required' => TRUE, | |
'#weight' => -1, | |
]; | |
- $all_requirements = $this->getAllRequirements(); | |
+ $all_requirements = $this->getAllRequirementsList(); | |
if ($disabled_requirements = $this->sensorConfig->getSetting('disabled_requirements')) { | |
$default_value = array_combine($disabled_requirements, $disabled_requirements); | |
} | |
@@ -173,26 +169,25 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
/** | |
* {@inheritdoc} | |
*/ | |
- public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
+ public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { | |
$values = $form_state->getValues()['settings']['disabled_requirements']; | |
$disabled_requirements = array_values(array_filter($values ?? [])); | |
$this->sensorConfig->settings['disabled_requirements'] = $disabled_requirements; | |
} | |
- | |
/** | |
* List of all requirements used of form options. | |
* | |
* @return array | |
* All requirements. | |
*/ | |
- protected function getAllRequirements() { | |
- $requirements = $this->systemManager->listRequirements(); | |
- $all_requirements = []; | |
- foreach ($requirements as $id => $requirement) { | |
- $all_requirements[$id] = (string) $requirement['title']; | |
+ protected function getAllRequirementsList(): array { | |
+ $all_requirements = $this->getAllRequirements(); | |
+ $requirements_options = []; | |
+ foreach ($all_requirements as $id => $requirement) { | |
+ $requirements_options[$id] = (string) $requirement['title']; | |
} | |
- return $all_requirements; | |
+ return $requirements_options; | |
} | |
/** | |
@@ -200,7 +195,7 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
* | |
* @return array | |
*/ | |
- protected function getSeverityOptions() { | |
+ protected function getSeverityOptions(): array { | |
return [ | |
SystemManager::REQUIREMENT_ERROR => $this->t('Error'), | |
SystemManager::REQUIREMENT_WARNING => $this->t('Warning'), | |
@@ -208,4 +203,21 @@ class RequirementsRecapSensorPlugin extends SensorPluginBase implements Extended | |
]; | |
} | |
+ /** | |
+ * Get all requirements from cache or from system manager. | |
+ * | |
+ * @return array | |
+ * All requirements. | |
+ */ | |
+ protected function getAllRequirements(): array { | |
+ $all_requirements = $this->cache->get(self::REQUIREMENTS_RECAP_CACHE_ID); | |
+ if (!isset($all_requirements->data)) { | |
+ $all_requirements = $this->systemManager->listRequirements(); | |
+ $caching_time = $this->sensorConfig->getCachingTime() ?? 0; | |
+ $this->cache->set(self::REQUIREMENTS_RECAP_CACHE_ID, $all_requirements, time() + $caching_time); | |
+ return $all_requirements; | |
+ } | |
+ return $all_requirements->data; | |
+ } | |
+ | |
} | |
-- | |
GitLab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment