diff --git a/modules/monitoring_mail/monitoring_mail.module b/modules/monitoring_mail/monitoring_mail.module index 75fd491..daf299b 100644 --- a/modules/monitoring_mail/monitoring_mail.module +++ b/modules/monitoring_mail/monitoring_mail.module @@ -92,10 +92,9 @@ function monitoring_mail_monitoring_run_sensors(array $results) { continue; } - $status_old = SensorResultInterface::STATUS_UNKNOWN; - // Try to load the previous log result for this sensor. - if ($previous_result = monitoring_sensor_result_second_last($result->getSensorId())) { + $status_old = SensorResultInterface::STATUS_UNKNOWN; + if ($previous_result = $result->getPreviousResult()) { $status_old = $previous_result->getStatus(); } @@ -107,7 +106,6 @@ function monitoring_mail_monitoring_run_sensors(array $results) { 'sensor_config' => $result->getSensorConfig(), 'status_old' => $status_old, 'status_new' => $status_new, - 'last_result' => $previous_result, ]; // Trigger result transition mail. diff --git a/monitoring.module b/monitoring.module index ab5a7c5..6835099 100644 --- a/monitoring.module +++ b/monitoring.module @@ -266,30 +266,6 @@ function monitoring_sensor_result_last($sensor_name) { } /** - * Gets second last sensor result. - * - * @param string $sensor_name - * The name of the sensor. - * - * @return \Drupal\monitoring\Entity\SensorResultEntity|null - * A SensorResultEntity representing the second last sensor result. - */ -function monitoring_sensor_result_second_last($sensor_name) { - $result = \Drupal::entityQuery('monitoring_sensor_result') - ->condition('sensor_name', $sensor_name) - ->sort('timestamp', 'DESC') - ->sort('record_id', 'DESC') - ->range(1, 1) - ->execute(); - - if (!empty($result)) { - return entity_load('monitoring_sensor_result', reset($result)); - } - - return NULL; -} - -/** * Implements hook_views_pre_render(). * * Alters the views page title. diff --git a/src/Result/SensorResult.php b/src/Result/SensorResult.php index a245459..e12d209 100644 --- a/src/Result/SensorResult.php +++ b/src/Result/SensorResult.php @@ -1,13 +1,10 @@ verboseOutput; } + /** + * {@inheritdoc} + */ + public function setPreviousResult(SensorResultEntity $previous_result = NULL) { + $this->previousResult = $previous_result; + } + + /** + * {@inheritdoc} + */ + public function getPreviousResult() { + return $this->previousResult; + } } diff --git a/src/Result/SensorResultInterface.php b/src/Result/SensorResultInterface.php index b9b1da2..d740161 100644 --- a/src/Result/SensorResultInterface.php +++ b/src/Result/SensorResultInterface.php @@ -1,12 +1,9 @@ $sensor_config) { if ($result = $this->runSensor($sensor_config)) { + $result->setPreviousResult(monitoring_sensor_result_last($result->getSensorId())); $results[$name] = $result; } } @@ -224,8 +221,8 @@ class SensorRunner { $old_status = NULL; // Try to load the previous log result for this sensor. - if ($last_result = monitoring_sensor_result_last($result->getSensorId())) { - $old_status = $last_result->getStatus(); + if ($result->getPreviousResult()) { + $old_status = $result->getPreviousResult()->getStatus(); } // Check if we need to log the result. diff --git a/tests/src/Kernel/MonitoringCoreKernelTest.php b/tests/src/Kernel/MonitoringCoreKernelTest.php index e0f2c96..281440d 100644 --- a/tests/src/Kernel/MonitoringCoreKernelTest.php +++ b/tests/src/Kernel/MonitoringCoreKernelTest.php @@ -9,6 +9,9 @@ namespace Drupal\Tests\monitoring\Kernel; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Logger\RfcLogLevel; use Drupal\monitoring\Entity\SensorConfig; +use Drupal\monitoring\Entity\SensorResultEntity; +use Drupal\monitoring\Result\SensorResult; +use Drupal\monitoring\Result\SensorResultInterface; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; @@ -770,4 +773,50 @@ class MonitoringCoreKernelTest extends MonitoringUnitTestBase { $this->assertTrue($result->isCritical()); } + /** + * Tests if the previous sensor result retrieved is the expected one. + */ + public function testPreviousSensorResult() { + // Allow running all enabled sensors. + \Drupal::configFactory() + ->getEditable('monitoring.settings') + ->set('cron_run_sensors', TRUE) + ->save(); + + \Drupal::cache('default')->deleteAll(); + $sensor_runner = \Drupal::service('monitoring.sensor_runner'); + // There should be no logged sensor result at the moment. + $sensorConfig = SensorConfig::load('test_sensor_falls'); + $sensor_result_0 = monitoring_sensor_result_last($sensorConfig->id()); + $this->assertNull($sensor_result_0); + + // Run a sensor that is CRITICAL and check there is no previous result. + /** @var \Drupal\monitoring\Result\SensorResult $sensor_result_1 */ + $sensor_result_1 = $sensor_runner->runSensors([$sensorConfig])[0]; + $previous_result_1 = $sensor_result_1->getPreviousResult(); + $this->assertEquals(SensorResultInterface::STATUS_CRITICAL, $sensor_result_1->getStatus()); + $this->assertEquals($sensor_result_0, $previous_result_1); + $this->assertNull($previous_result_1); + + // Run the same sensor and check the previous result status is + // same as $sensor_result_1. + /** @var \Drupal\monitoring\Result\SensorResult $sensor_result_2 */ + $sensor_result_2 = $sensor_runner->runSensors([$sensorConfig])[0]; + $previous_result_2 = $sensor_result_2->getPreviousResult(); + $this->assertEquals(SensorResultInterface::STATUS_CRITICAL, $sensor_result_2->getStatus()); + $this->assertEquals($sensor_result_1->getStatus(), $previous_result_2->getStatus()); + $this->assertNotNull($previous_result_2); + + // Change sensor threshold settings so that the sensor switches to WARNING. + $sensorConfig->thresholds['critical'] = 0; + + // Run it again, a new log should be stored because the status has changed. + /** @var \Drupal\monitoring\Result\SensorResult $sensor_result_3 */ + $sensor_result_3 = $sensor_runner->runSensors([$sensorConfig])[0]; + $previous_result_3 = $sensor_result_3->getPreviousResult(); + $this->assertEquals(SensorResultInterface::STATUS_WARNING, $sensor_result_3->getStatus()); + $this->assertEquals($sensor_result_2->getStatus(), $previous_result_3->getStatus()); + $this->assertNotNull($previous_result_3); + } + }