You are here

class SensorList in Monitoring 8

Hierarchy

Expanded class hierarchy of SensorList

File

src/Controller/SensorList.php, line 12

Namespace

Drupal\monitoring\Controller
View source
class SensorList extends ControllerBase {

  /**
   * The sensor runner.
   *
   * @var \Drupal\monitoring\SensorRunner
   */
  protected $sensorRunner;

  /**
   * The sensor manager.
   *
   * @var \Drupal\monitoring\Sensor\SensorManager
   */
  protected $sensorManager;

  /**
   * Constructs a \Drupal\monitoring\Form\SensorDetailForm object.
   *
   * @param \Drupal\monitoring\SensorRunner $sensor_runner
   *   The factory for configuration objects.
   * @param \Drupal\monitoring\Sensor\SensorManager $sensor_manager
   *   The sensor manager service.
   */
  public function __construct(SensorRunner $sensor_runner, SensorManager $sensor_manager) {
    $this->sensorRunner = $sensor_runner;
    $this->sensorManager = $sensor_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('monitoring.sensor_runner'), $container
      ->get('monitoring.sensor_manager'));
  }
  public function content() {
    $rows = array();
    $results = $this->sensorRunner
      ->runSensors();
    $status_overview = array(
      SensorResultInterface::STATUS_OK => 0,
      SensorResultInterface::STATUS_INFO => 0,
      SensorResultInterface::STATUS_WARNING => 0,
      SensorResultInterface::STATUS_CRITICAL => 0,
      SensorResultInterface::STATUS_UNKNOWN => 0,
    );
    $total_execution_time = 0;
    $non_cached_execution_time = 0;

    // Oldest sensor age in seconds.
    $oldest_sensor_age = 0;

    // Oldest sensor config.
    $oldest_sensor_config = NULL;
    foreach ($this->sensorManager
      ->getSensorConfigByCategories() as $category => $category_sensor_config) {

      // Category grouping row.
      $rows[] = array(
        'data' => array(
          'label' => array(
            'data' => array(
              '#markup' => '<h3>' . $category . '</h3>',
            ),
            'colspan' => 7,
          ),
        ),
      );
      $ok_row_count = 0;

      /** @var \Drupal\monitoring\SensorConfigInterface $sensor_config */
      foreach ($category_sensor_config as $sensor_name => $sensor_config) {
        if (!isset($results[$sensor_name])) {
          continue;
        }

        /** @var \Drupal\monitoring\Result\SensorResultInterface $sensor_result */
        $sensor_result = $results[$sensor_name];
        $called_before = \Drupal::time()
          ->getRequestTime() - $sensor_result
          ->getTimestamp();
        if ($called_before > $oldest_sensor_age) {
          $oldest_sensor_config = $sensor_config;
          $oldest_sensor_age = $called_before;
        }
        $row['data']['label']['data'] = array(
          '#markup' => '<span title="' . $sensor_config
            ->getDescription() . '">' . $sensor_config
            ->getLabel() . '</span>',
        );
        $row['data']['sensor_status'] = array(
          'data' => $sensor_result
            ->getStatus(),
          'class' => array(
            'status',
          ),
        );
        $row['data']['timestamp'] = \Drupal::service('date.formatter')
          ->formatInterval(\Drupal::time()
          ->getRequestTime() - $sensor_result
          ->getTimestamp());
        $row['data']['execution_time'] = array(
          'data' => $sensor_result
            ->getExecutionTime() . 'ms',
          'class' => array(
            'execution-time',
          ),
        );
        $row['data']['sensor_status_message'] = Unicode::truncate(strip_tags($sensor_result
          ->getMessage()), 200, TRUE, TRUE);
        $row['class'] = array(
          'monitoring-' . strtolower($sensor_result
            ->getStatus()),
        );
        $links = array();
        $links['details'] = array(
          'title' => t('Details'),
          'url' => $sensor_config
            ->toUrl('details-form'),
        );

        // Display a force execution link for any sensor that can be cached.
        if ($sensor_config
          ->getCachingTime() && $this
          ->currentUser()
          ->hasPermission('monitoring force run')) {
          $links['force_execution'] = array(
            'title' => t('Force execution'),
            'url' => $sensor_config
              ->toUrl('force-run-sensor'),
          );
        }
        $links['edit'] = array(
          'title' => t('Edit'),
          'url' => $sensor_config
            ->toUrl('edit-form'),
          'query' => array(
            'destination' => 'admin/reports/monitoring',
          ),
        );
        \Drupal::moduleHandler()
          ->alter('monitoring_sensor_links', $links, $sensor_config);
        $row['data']['actions'] = array();
        if (!empty($links)) {
          $row['data']['actions']['data'] = array(
            '#type' => 'dropbutton',
            '#links' => $links,
          );
        }
        $rows[] = $row;
        $status_overview[$sensor_result
          ->getStatus()]++;
        $total_execution_time += $sensor_result
          ->getExecutionTime();
        if (!$sensor_result
          ->isCached()) {
          $non_cached_execution_time += $sensor_result
            ->getExecutionTime();
        }
        if ($sensor_result
          ->getStatus() == SensorResultInterface::STATUS_OK) {
          $ok_row_count++;
        }
        else {
          $ok_row_count = -1;
        }
      }

      // Add special class if all sensors of a category are ok.
      if ($ok_row_count >= 0) {
        $index = count($rows) - $ok_row_count - 1;
        $rows[$index]['class'][] = 'sensor-category-ok';
      }
    }
    $output['summary'] = array(
      '#theme' => 'monitoring_overview_summary',
      '#status_overview' => $status_overview,
      '#total_execution_time' => $total_execution_time,
      '#non_cached_execution_time' => $non_cached_execution_time,
    );

    // We can add the oldest_sensor_* data only if there are sensor results cached.
    if (!empty($oldest_sensor_config)) {
      $output['summary']['#oldest_sensor_label'] = $oldest_sensor_config
        ->getLabel();
      $output['summary']['#oldest_sensor_category'] = $oldest_sensor_config
        ->getCategory();
      $output['summary']['#oldest_sensor_called_before'] = \Drupal::service('date.formatter')
        ->formatInterval($oldest_sensor_age);
    }
    $header = array(
      t('Sensor name'),
      array(
        'data' => t('Status'),
        'class' => array(
          'status',
        ),
      ),
      t('Called before'),
      t('Execution time'),
      t('Status Message'),
      array(
        'data' => t('Actions'),
        'class' => array(
          'actions',
        ),
      ),
    );
    $monitoring_escalated_sensors = $status_overview[SensorResultInterface::STATUS_WARNING] + $status_overview[SensorResultInterface::STATUS_CRITICAL] + $status_overview[SensorResultInterface::STATUS_UNKNOWN];
    $output['table'] = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#sticky' => TRUE,
      '#attributes' => array(
        'class' => array(
          'monitoring-severity-colors',
        ),
        'id' => 'monitoring-sensors-overview',
      ),
      '#attached' => [
        'drupalSettings' => [
          'monitoring_escalated_sensors' => $monitoring_escalated_sensors,
        ],
        'library' => [
          'monitoring/monitoring',
        ],
      ],
    );
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SensorList::$sensorManager protected property The sensor manager.
SensorList::$sensorRunner protected property The sensor runner.
SensorList::content public function
SensorList::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
SensorList::__construct public function Constructs a \Drupal\monitoring\Form\SensorDetailForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.