class TopController in MongoDB 8.2
The Top403/Top404 controllers.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\mongodb_watchdog\Controller\ControllerBase uses \Psr\Log\LoggerAwareTrait
- class \Drupal\mongodb_watchdog\Controller\TopController
 
 
 - class \Drupal\mongodb_watchdog\Controller\ControllerBase uses \Psr\Log\LoggerAwareTrait
 
Expanded class hierarchy of TopController
File
- modules/
mongodb_watchdog/ src/ Controller/ TopController.php, line 19  
Namespace
Drupal\mongodb_watchdog\ControllerView source
class TopController extends ControllerBase {
  const TYPES = [
    'page not found',
    'access denied',
  ];
  const TYPE_MAP = [
    'root' => TopResult::class,
  ];
  /**
   * The database holding the logger collections.
   *
   * @var \MongoDB\Database
   */
  protected $database;
  /**
   * TopController constructor.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service, to log intervening events.
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
   *   The MongoDB logger, to load stored events.
   * @param \Drupal\Core\Config\ImmutableConfig $config
   *   The module configuration.
   * @param \MongoDB\Database $database
   *   Needed because there is no group() command in phplib yet.
   * @param \Drupal\Core\Pager\PagerManagerInterface $pagerManager
   *   The core pager.manager service.
   *
   * @see https://jira.mongodb.org/browse/PHPLIB-177
   */
  public function __construct(LoggerInterface $logger, Logger $watchdog, ImmutableConfig $config, Database $database, PagerManagerInterface $pagerManager) {
    parent::__construct($logger, $watchdog, $pagerManager, $config);
    $this->database = $database;
  }
  /**
   * Controller.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param string $type
   *   The type of top report to produce.
   *
   * @return array
   *   A render array.
   */
  public function build(Request $request, string $type) : array {
    $top = $this
      ->getTop();
    $rows = $this
      ->getRowData($request, $type);
    $main = empty($rows) ? $this
      ->buildEmpty($this
      ->t('No "%type" message found', [
      '%type' => $type,
    ])) : $this
      ->buildMainTable($rows);
    $ret = $this
      ->buildDefaults($main, $top);
    return $ret;
  }
  /**
   * Build the main table.
   *
   * @param array $rows
   *   The event data.
   *
   * @return array
   *   A render array for the main table.
   */
  protected function buildMainTable(array $rows) : array {
    $ret = [
      '#header' => $this
        ->buildMainTableHeader(),
      '#rows' => $this
        ->buildMainTableRows($rows),
      '#type' => 'table',
    ];
    return $ret;
  }
  /**
   * Build the main table header.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
   *   A table header array.
   */
  protected function buildMainTableHeader() : array {
    $header = [
      $this
        ->t('#'),
      $this
        ->t('Paths'),
    ];
    return $header;
  }
  /**
   * Build the main table rows.
   *
   * @param array[] $counts
   *   The array of counts per 403/404 page.
   *
   * @return array
   *   A render array for a table.
   */
  protected function buildMainTableRows(array $counts) : array {
    $rows = [];
    /** @var \Drupal\mongodb_watchdog\Controller\TopResult $result */
    foreach ($counts as $result) {
      $row = [
        $result->count,
        $result->uri,
      ];
      $rows[] = $row;
    }
    return $rows;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : self {
    /** @var \Psr\Log\LoggerInterface $logger */
    $logger = $container
      ->get('logger.channel.mongodb_watchdog');
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
    $watchdog = $container
      ->get(Logger::SERVICE_LOGGER);
    /** @var \Drupal\Core\Config\ImmutableConfig $config */
    $config = $container
      ->get('config.factory')
      ->get('mongodb_watchdog.settings');
    /** @var \MongoDB\Database $database */
    $database = $container
      ->get('mongodb.watchdog_storage');
    /** @var \Drupal\Core\Pager\PagerManagerInterface $pagerManager */
    $pagerManager = $container
      ->get('pager.manager');
    return new static($logger, $watchdog, $config, $database, $pagerManager);
  }
  /**
   * Obtain the data from the logger.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request. Needed for paging.
   * @param string $type
   *   The type of top list to retrieve.
   *
   * @return array
   *   The data array.
   */
  protected function getRowData(Request $request, string $type) : array {
    // Find _id for the error type.
    $templateCollection = $this->watchdog
      ->templateCollection();
    $template = $templateCollection
      ->findOne([
      'type' => $type,
    ], [
      '_id',
    ]);
    if (empty($template)) {
      return [];
    }
    // Find occurrences of error type.
    $collectionName = $template['_id'];
    $eventCollection = $this->watchdog
      ->eventCollection($collectionName);
    $counts = $this
      ->group($eventCollection, 'variables.@uri', []);
    $page = $this
      ->setupPager($request, count($counts));
    $skip = $page * $this->itemsPerPage;
    $counts = array_slice($counts, $skip, $this->itemsPerPage);
    return $counts;
  }
  /**
   * Command wrapper for removed MongoDB group() method/command.
   *
   * @param \MongoDB\Collection $collection
   *   The collection on which to perform the command.
   * @param string $key
   *   The grouping key.
   * @param array $cond
   *   The condition.
   *
   * @return array
   *   An array of stdClass rows with the following properties:
   *   - _id: the URL
   *   - count: the number of occurrences.
   *   It may be empty.
   *
   * @throws \MongoDB\Driver\Exception\RuntimeException
   * @throws \MongoDB\Exception\InvalidArgumentException
   * @throws \MongoDB\Exception\UnexpectedValueException
   * @throws \MongoDB\Exception\UnsupportedException
   */
  public function group(Collection $collection, string $key, array $cond) : array {
    $pipeline = [];
    if (!empty($cond)) {
      $pipeline[] = [
        '$match' => $cond,
      ];
    }
    if (!empty($key)) {
      $pipeline[] = [
        '$group' => [
          '_id' => "\${$key}",
          'count' => [
            '$sum' => 1,
          ],
        ],
      ];
    }
    $pipeline[] = [
      '$sort' => [
        'count' => -1,
        '_id' => 1,
      ],
    ];
    // Aggregate always returns a cursor since MongoDB 3.6.
    /** @var \MongoDB\Driver\CursorInterface $res */
    $res = $collection
      ->aggregate($pipeline);
    $res
      ->setTypeMap(static::TYPE_MAP);
    $ret = $res
      ->toArray();
    return $ret;
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            ControllerBase:: | 
                  protected | property | The configuration factory. | |
| 
            ControllerBase:: | 
                  protected | property | The current user service. | 1 | 
| 
            ControllerBase:: | 
                  protected | property | The entity form builder. | |
| 
            ControllerBase:: | 
                  protected | property | The entity manager. | |
| 
            ControllerBase:: | 
                  protected | property | The entity type manager. | |
| 
            ControllerBase:: | 
                  protected | property | The form builder. | 2 | 
| 
            ControllerBase:: | 
                  protected | property | The items_per_page configuration value. | |
| 
            ControllerBase:: | 
                  protected | property | The key-value storage. | 1 | 
| 
            ControllerBase:: | 
                  protected | property | The language manager. | 1 | 
| 
            ControllerBase:: | 
                  protected | property | The module handler. | 2 | 
| 
            ControllerBase:: | 
                  protected | property | The pager.manager service. | |
| 
            ControllerBase:: | 
                  protected | property | The state service. | |
| 
            ControllerBase:: | 
                  protected | property | The MongoDB logger, to load events. | |
| 
            ControllerBase:: | 
                  protected | function | The default build() implementation. | |
| 
            ControllerBase:: | 
                  protected | function | Build markup for a message about the lack of results. | |
| 
            ControllerBase:: | 
                  protected | function | Returns the requested cache bin. | |
| 
            ControllerBase:: | 
                  protected | function | Retrieves a configuration object. | |
| 
            ControllerBase:: | 
                  private | function | Returns the service container. | |
| 
            ControllerBase:: | 
                  protected | function | Returns the current user. | 1 | 
| 
            ControllerBase:: | 
                  protected | function | Retrieves the entity form builder. | |
| 
            ControllerBase:: | 
                  protected | function | Retrieves the entity manager service. | |
| 
            ControllerBase:: | 
                  protected | function | Retrieves the entity type manager. | |
| 
            ControllerBase:: | 
                  protected | function | Returns the form builder service. | 2 | 
| 
            ControllerBase:: | 
                  public static | function | Return a reliable page number based on available data. | |
| 
            ControllerBase:: | 
                  protected | function | Return the top element: empty by default. | 3 | 
| 
            ControllerBase:: | 
                  protected | function | Returns a key/value storage collection. | 1 | 
| 
            ControllerBase:: | 
                  protected | function | Returns the language manager service. | 1 | 
| 
            ControllerBase:: | 
                  protected | function | Returns the module handler. | 2 | 
| 
            ControllerBase:: | 
                  protected | function | 
            Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | 
                  |
| 
            ControllerBase:: | 
                  public | function | Set up the pager. | |
| 
            ControllerBase:: | 
                  protected | function | Returns the state storage service. | |
| 
            LinkGeneratorTrait:: | 
                  protected | property | The link generator. | 1 | 
| 
            LinkGeneratorTrait:: | 
                  protected | function | Returns the link generator. | |
| 
            LinkGeneratorTrait:: | 
                  protected | function | Renders a link to a route given a route name and its parameters. | |
| 
            LinkGeneratorTrait:: | 
                  public | function | Sets the link generator service. | |
| 
            LoggerChannelTrait:: | 
                  protected | property | The logger channel factory service. | |
| 
            LoggerChannelTrait:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            LoggerChannelTrait:: | 
                  public | function | Injects the logger channel factory. | |
| 
            MessengerTrait:: | 
                  protected | property | The messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Gets the messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Sets the messenger. | |
| 
            RedirectDestinationTrait:: | 
                  protected | property | The redirect destination service. | 1 | 
| 
            RedirectDestinationTrait:: | 
                  protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| 
            RedirectDestinationTrait:: | 
                  protected | function | Returns the redirect destination service. | |
| 
            RedirectDestinationTrait:: | 
                  public | function | Sets the redirect destination service. | |
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | 1 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. | |
| 
            TopController:: | 
                  protected | property | The database holding the logger collections. | |
| 
            TopController:: | 
                  public | function | Controller. | |
| 
            TopController:: | 
                  protected | function | Build the main table. | |
| 
            TopController:: | 
                  protected | function | Build the main table header. | |
| 
            TopController:: | 
                  protected | function | Build the main table rows. | |
| 
            TopController:: | 
                  public static | function | 
            Instantiates a new instance of this class. Overrides ControllerBase:: | 
                  |
| 
            TopController:: | 
                  protected | function | Obtain the data from the logger. | |
| 
            TopController:: | 
                  public | function | Command wrapper for removed MongoDB group() method/command. | |
| 
            TopController:: | 
                  constant | |||
| 
            TopController:: | 
                  constant | |||
| 
            TopController:: | 
                  public | function | 
            TopController constructor. Overrides ControllerBase:: | 
                  |
| 
            UrlGeneratorTrait:: | 
                  protected | property | The url generator. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Returns the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  public | function | Sets the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Generates a URL or path for a specific route based on the given parameters. |