View source
<?php
namespace Drupal\monitoring\Result;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\monitoring\Entity\SensorResultDataInterface;
use Drupal\monitoring\Sensor\SensorCompilationException;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\monitoring\Sensor\Thresholds;
class SensorResult implements SensorResultInterface {
protected $sensorConfig;
protected $isCached = FALSE;
protected $data = array();
protected $statusMessages = array();
protected $sensorMessage = array();
protected $verboseOutput;
protected $previousResult = NULL;
function __construct(SensorConfig $sensor_config, array $cached_data = array()) {
$this->sensorConfig = $sensor_config;
if ($cached_data) {
$this->data = $cached_data;
$this->isCached = TRUE;
}
$this->data += array(
'sensor_status' => SensorResultInterface::STATUS_UNKNOWN,
'sensor_message' => NULL,
'sensor_expected_value' => NULL,
'sensor_value' => NULL,
'execution_time' => 0,
'timestamp' => \Drupal::time()
->getRequestTime(),
);
}
protected function setResultData($key, $value) {
$this->data[$key] = $value;
$this->isCached = FALSE;
}
protected function getResultData($key) {
return $this->data[$key];
}
public function getStatus() {
return $this
->getResultData('sensor_status');
}
public static function getStatusLabels() {
return [
self::STATUS_CRITICAL => t('Critical'),
self::STATUS_WARNING => t('Warning'),
self::STATUS_INFO => t('Info'),
self::STATUS_OK => t('OK'),
self::STATUS_UNKNOWN => t('Unknown'),
];
}
public function getStatusLabel() {
$labels = self::getStatusLabels();
return $labels[$this
->getResultData('sensor_status')];
}
public function getMessage() {
return $this
->getResultData('sensor_message');
}
public function setMessage($message, array $variables = array()) {
$this->sensorMessage = array(
'message' => $message,
'variables' => $variables,
);
}
public function addStatusMessage($message, array $variables = array()) {
$this->statusMessages[] = array(
'message' => $message,
'variables' => $variables,
);
}
public function compile() {
$threshold_message = NULL;
if ($this
->isUnknown()) {
if ($this
->getSensorConfig()
->isDefiningThresholds()) {
$threshold_message = $this
->assessThresholds();
}
elseif ($this
->getExpectedValue() !== NULL) {
$this
->assessComparison();
}
elseif ($this
->getSensorConfig()
->isNumeric()) {
$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)) {
$message = new FormattableMarkup($this->sensorMessage['message'], $this->sensorMessage['variables']);
}
else {
$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()),
);
$messages = array();
if ($this
->getValue() !== NULL) {
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';
}
if ($this
->isCritical() && $this
->getExpectedValue() !== NULL) {
$messages[] = new FormattableMarkup('expected @expected', $default_variables);
}
if ($threshold_message !== NULL) {
$messages[] = $threshold_message;
}
$renderer = \Drupal::service('renderer');
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);
}
protected function assessComparison() {
if ($this
->getValue() != $this
->getExpectedValue()) {
$this
->setStatus(SensorResultInterface::STATUS_CRITICAL);
}
else {
$this
->setStatus(SensorResultInterface::STATUS_OK);
}
}
protected function assessThresholds() {
$thresholds = new Thresholds($this->sensorConfig);
$matched_threshold = $thresholds
->getMatchedThreshold($this
->getValue());
$this
->setStatus($matched_threshold);
return $thresholds
->getStatusMessage();
}
public function getFormattedValue($value) {
$value_type = $this
->getSensorConfig()
->getValueType();
if (!empty($value_type)) {
$value_types = monitoring_value_types();
if (!isset($value_types[$value_type])) {
throw new SensorCompilationException(new FormattableMarkup('Invalid value type @type', array(
'@type' => $value_type,
)));
}
elseif (empty($value_types[$value_type]['formatter_callback']) && ($label = $this
->getSensorConfig()
->getValueLabel())) {
$label = mb_strtolower($label);
return new FormattableMarkup('@value @label', array(
'@value' => $value,
'@label' => $label,
));
}
elseif (isset($value_types[$value_type]['formatter_callback']) && !function_exists($value_types[$value_type]['formatter_callback'])) {
throw new SensorCompilationException(new FormattableMarkup('Formatter callback @callback for @type does not exist', array(
'@callback' => $value_types[$value_type]['formatter_callback'],
'@type' => $value_type,
)));
}
elseif (isset($value_types[$value_type]['formatter_callback'])) {
$callback = $value_types[$value_type]['formatter_callback'];
return $callback($this);
}
}
if ($label = $this
->getSensorConfig()
->getValueLabel()) {
$label = mb_strtolower($label);
return new FormattableMarkup('@value @label', array(
'@value' => $value,
'@label' => $label,
));
}
return new FormattableMarkup('Value @value', array(
'@value' => $value,
));
}
public function getValue() {
if ($this
->getSensorConfig()
->isBool()) {
return (bool) $this
->getResultData('sensor_value');
}
return $this
->getResultData('sensor_value');
}
public function getExpectedValue() {
if ($this
->getSensorConfig()
->isBool()) {
return (bool) $this
->getResultData('sensor_expected_value');
}
return $this
->getResultData('sensor_expected_value');
}
public function getExecutionTime() {
return round($this
->getResultData('execution_time'), 2);
}
public function setStatus($sensor_status) {
$this
->setResultData('sensor_status', $sensor_status);
}
public function setValue($sensor_value) {
$this
->setResultData('sensor_value', $sensor_value);
}
public function setExpectedValue($sensor_value) {
$this
->setResultData('sensor_expected_value', $sensor_value);
}
public function setExecutionTime($execution_time) {
$this
->setResultData('execution_time', $execution_time);
}
public function toNumber() {
$sensor_value = $this
->getValue();
if (is_numeric($sensor_value)) {
return $sensor_value;
}
return (int) $sensor_value;
}
public function isWarning() {
return $this
->getStatus() == SensorResultInterface::STATUS_WARNING;
}
public function isCritical() {
return $this
->getStatus() == SensorResultInterface::STATUS_CRITICAL;
}
public function isUnknown() {
return $this
->getStatus() == SensorResultInterface::STATUS_UNKNOWN;
}
public function isOk() {
return $this
->getStatus() == SensorResultInterface::STATUS_OK;
}
public function toArray() {
return array(
'sensor_name' => $this
->getSensorId(),
'value' => $this
->getValue(),
'expected_value' => $this
->getExpectedValue(),
'numeric_value' => $this
->toNumber(),
'status' => $this
->getStatus(),
'message' => $this
->getMessage(),
'execution_time' => $this
->getExecutionTime(),
'timestamp' => $this
->getTimestamp(),
);
}
public function isCached() {
return $this->isCached;
}
public function getTimestamp() {
return $this
->getResultData('timestamp');
}
public function getSensorId() {
return $this->sensorConfig
->id();
}
public function getSensorConfig() {
return $this->sensorConfig;
}
public function setVerboseOutput($verbose_output) {
$this->verboseOutput = $verbose_output;
}
public function getVerboseOutput() {
return $this->verboseOutput;
}
public function setPreviousResult(SensorResultDataInterface $previous_result = NULL) {
$this->previousResult = $previous_result;
}
public function getPreviousResult() {
return $this->previousResult;
}
}