You are here

public function SensorResult::compile in Monitoring 7

Same name and namespace in other branches
  1. 8 src/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\Sensor\SensorInterface::runSensor() for details.

Throws

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

Overrides SensorResultInterface::compile

File

lib/Drupal/monitoring/Result/SensorResult.php, line 171
Contains \Drupal\monitoring\Result\SensorResult.

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
      ->getSensorInfo()
      ->isDefiningThresholds()) {
      $threshold_message = $this
        ->assessThresholds();
    }
    elseif ($this
      ->getExpectedValue() !== NULL) {
      $this
        ->assessComparison();
    }
  }
  if ($this
    ->getSensorInfo()
    ->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 = format_string($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
        ->getSensorName(),
      '!formatted_value' => $this
        ->getFormattedValue(),
      '@time' => $this
        ->getTimestamp(),
      '!expected' => $msg_expected,
      '!time_interval' => format_interval($this
        ->getSensorInfo()
        ->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 we append the info to the
      // message.
      if ($this
        ->getSensorInfo()
        ->getTimeIntervalValue()) {
        $messages[] = format_string('!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[] = format_string('expected !expected', $default_variables);
    }

    // Set the threshold message if there is any.
    if ($threshold_message !== NULL) {
      $messages[] = $threshold_message;
    }

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