You are here

public function SensorResult::compile in Monitoring 8

Same name and namespace in other branches
  1. 7 lib/Drupal/monitoring/Result/SensorResult.php \Drupal\monitoring\Result\SensorResult::compile()

Compiles added status messages sets the status.

If the status is STATUS_UNKNOWN, this will attempt to set the status based on expected value and threshold configurations. See \Drupal\monitoring\SensorPlugin\SensorPluginInterface::runSensor() for details.

Throws

\Drupal\monitoring\Sensor\SensorCompilationException Thrown if an error occurs during the sensor result compilation.

Overrides SensorResultInterface::compile

File

src/Result/SensorResult.php, line 182

Class

SensorResult
Generic container for the sensor result.

Namespace

Drupal\monitoring\Result

Code

public function compile() {

  // If the status is unknown we do the value assessment through
  // configurable thresholds.
  $threshold_message = NULL;
  if ($this
    ->isUnknown()) {
    if ($this
      ->getSensorConfig()
      ->isDefiningThresholds()) {
      $threshold_message = $this
        ->assessThresholds();
    }
    elseif ($this
      ->getExpectedValue() !== NULL) {
      $this
        ->assessComparison();
    }
    elseif ($this
      ->getSensorConfig()
      ->isNumeric()) {

      // Numeric sensors that do not have thresholds or an expected value
      // default to OK.
      $this
        ->setStatus(SensorResultInterface::STATUS_OK);
    }
  }
  if ($this
    ->getSensorConfig()
    ->getValueType() == 'bool') {
    $msg_expected = $this
      ->getExpectedValue() ? 'TRUE' : 'FALSE';
  }
  else {
    $msg_expected = $this
      ->getExpectedValue();
  }
  if (!empty($this->sensorMessage)) {

    // A message has been set by the sensor, use that as is and only do
    // placeholder replacements with the provided variables.
    $message = new FormattableMarkup($this->sensorMessage['message'], $this->sensorMessage['variables']);
  }
  else {

    // No message has been provided, attempt to build one.
    // Set the default message variables.
    $default_variables = array(
      '@sensor' => $this
        ->getSensorId(),
      '@formatted_value' => $this
        ->getFormattedValue($this
        ->getValue()),
      '@time' => $this
        ->getTimestamp(),
      '@expected' => $msg_expected,
      '@time_interval' => \Drupal::service('date.formatter')
        ->formatInterval($this
        ->getSensorConfig()
        ->getTimeIntervalValue()),
    );

    // Build an array of message parts.
    $messages = array();

    // Add the sensor value if provided.
    if ($this
      ->getValue() !== NULL) {

      // If the sensor defines time interval value we append
      // the info to the message.
      if ($this
        ->getSensorConfig()
        ->getTimeIntervalValue()) {
        $messages[] = new FormattableMarkup('@formatted_value in @time_interval', $default_variables);
      }
      else {
        $messages[] = $default_variables['@formatted_value'];
      }
    }
    elseif (empty($this->statusMessages)) {
      $messages[] = 'No value';
    }

    // Set the expected value message if the sensor did not match.
    if ($this
      ->isCritical() && $this
      ->getExpectedValue() !== NULL) {
      $messages[] = new FormattableMarkup('expected @expected', $default_variables);
    }

    // Set the threshold message if there is any.
    if ($threshold_message !== NULL) {
      $messages[] = $threshold_message;
    }
    $renderer = \Drupal::service('renderer');

    // Append all status messages which were added by the sensor.
    foreach ($this->statusMessages as $msg) {
      if (is_array($msg['message'])) {
        $messages[] = new FormattableMarkup($renderer
          ->renderPlain($msg['message']), array_merge($default_variables, $msg['variables']));
      }
      else {
        $messages[] = new FormattableMarkup($msg['message'], array_merge($default_variables, $msg['variables']));
      }
    }
    $message = strip_tags(implode(', ', $messages));
  }
  $this
    ->setResultData('sensor_message', $message);
}