meilisearch/Classes/Controller/Backend/Search/InfoModuleController.php

297 lines
9.9 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\Controller\Backend\Search;
/***************************************************************
* Copyright notice
*
2021-04-17 21:20:54 +02:00
* (c) 2010-2017 dkd Internet Service GmbH <meilisearch-support@dkd.de>
2021-04-17 00:26:33 +02:00
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
2021-04-24 04:44:44 +02:00
use TYPO3\CMS\Core\Utility\DebugUtility;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\Api;
use WapplerSystems\Meilisearch\ConnectionManager;
use WapplerSystems\Meilisearch\Domain\Search\Statistics\StatisticsRepository;
2021-04-24 04:44:44 +02:00
use WapplerSystems\Meilisearch\Domain\Search\MeilisearchDocument\Repository;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Validator\Path;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Registry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
/**
* Info Module
*/
class InfoModuleController extends AbstractModuleController
{
/**
* @var ConnectionManager
*/
2021-04-17 21:20:54 +02:00
protected $meilisearchConnectionManager;
2021-04-17 00:26:33 +02:00
/**
* @var Repository
*/
2021-04-17 21:20:54 +02:00
protected $apacheMeilisearchDocumentRepository;
2021-04-17 00:26:33 +02:00
/**
* Initializes the controller before invoking an action method.
*/
protected function initializeAction()
{
parent::initializeAction();
2021-04-17 21:20:54 +02:00
$this->meilisearchConnectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
//$this->apacheMeilisearchDocumentRepository = GeneralUtility::makeInstance(Repository::class);
2021-04-17 00:26:33 +02:00
}
/**
* Set up the doc header properly here
*
* @param ViewInterface $view
* @return void
*/
protected function initializeView(ViewInterface $view)
{
parent::initializeView($view);
/* @var ModuleTemplate $module */ // holds the state of chosen tab
$module = GeneralUtility::makeInstance(ModuleTemplate::class);
$coreOptimizationTabs = $module->getDynamicTabMenu([], 'coreOptimization');
$this->view->assign('tabs', $coreOptimizationTabs);
}
/**
2021-04-17 21:20:54 +02:00
* Index action, shows an overview of the state of the Meilisearch index
2021-04-17 00:26:33 +02:00
*
* @return void
*/
public function indexAction()
{
if ($this->selectedSite === null) {
$this->view->assign('can_not_proceed', true);
return;
}
$this->collectConnectionInfos();
$this->collectStatistics();
2021-04-24 04:44:44 +02:00
//$this->collectIndexFieldsInfo();
//$this->collectIndexInspectorInfo();
2021-04-17 00:26:33 +02:00
}
/**
* @param string $type
* @param int $uid
* @param int $pageId
* @param int $languageUid
* @return void
*/
public function documentsDetailsAction(string $type, int $uid, int $pageId, int $languageUid)
{
2021-04-17 21:20:54 +02:00
$documents = $this->apacheMeilisearchDocumentRepository->findByTypeAndPidAndUidAndLanguageId($type, $uid, $pageId, $languageUid);
2021-04-17 00:26:33 +02:00
$this->view->assign('documents', $documents);
}
/**
2021-04-17 21:20:54 +02:00
* Checks whether the configured Meilisearch server can be reached and provides a
2021-04-17 00:26:33 +02:00
* flash message according to the result of the check.
*
* @return void
*/
protected function collectConnectionInfos()
{
$connectedHosts = [];
$missingHosts = [];
$invalidPaths = [];
2021-04-24 04:44:44 +02:00
$connection = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
2021-04-17 00:26:33 +02:00
2021-04-24 04:44:44 +02:00
if (empty($connection)) {
2021-04-17 00:26:33 +02:00
$this->view->assign('can_not_proceed', true);
return;
}
2021-04-24 04:44:44 +02:00
$coreAdmin = $connection->getAdminService();
2021-04-17 00:26:33 +02:00
2021-04-24 04:44:44 +02:00
if ($coreAdmin->ping()) {
$connectedHosts[] = $coreAdmin;
} else {
$missingHosts[] = $coreAdmin;
2021-04-17 00:26:33 +02:00
}
2021-04-24 04:44:44 +02:00
2021-04-17 00:26:33 +02:00
$this->view->assignMultiple([
'site' => $this->selectedSite,
'apiKey' => Api::getApiKey(),
'connectedHosts' => $connectedHosts,
'missingHosts' => $missingHosts,
'invalidPaths' => $invalidPaths
]);
}
/**
2021-04-17 21:20:54 +02:00
* Index action, shows an overview of the state of the Meilisearch index
2021-04-17 00:26:33 +02:00
*
* @return void
*/
protected function collectStatistics()
{
// TODO make time frame user adjustable, for now it's last 30 days
$siteRootPageId = $this->selectedSite->getRootPageId();
/* @var StatisticsRepository $statisticsRepository */
$statisticsRepository = GeneralUtility::makeInstance(StatisticsRepository::class);
// @TODO: Do we want Typoscript constants to restrict the results?
$this->view->assign(
'top_search_phrases',
$statisticsRepository->getTopKeyWordsWithHits($siteRootPageId, 30, 5)
);
$this->view->assign(
'top_search_phrases_without_hits',
$statisticsRepository->getTopKeyWordsWithoutHits($siteRootPageId, 30, 5)
);
$this->view->assign(
'search_phrases_statistics',
$statisticsRepository->getSearchStatistics($siteRootPageId, 30, 100)
);
$labels = [];
$data = [];
$chartData = $statisticsRepository->getQueriesOverTime($siteRootPageId, 30, 86400);
foreach ($chartData as $bucket) {
$labels[] = strftime('%x', $bucket['timestamp']);
$data[] = (int)$bucket['numQueries'];
}
$this->view->assign('queriesChartLabels', json_encode($labels));
$this->view->assign('queriesChartData', json_encode($data));
}
/**
* Gets Luke meta data for the currently selected core and provides a list
* of that data.
*
* @return void
*/
protected function collectIndexFieldsInfo()
{
$indexFieldsInfoByCorePaths = [];
2021-04-24 04:44:44 +02:00
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
foreach ($meilisearchCoreConnections as $i => $meilisearchCoreConnection) {
2021-04-17 21:20:54 +02:00
$coreAdmin = $meilisearchCoreConnection->getAdminService();
2021-04-17 00:26:33 +02:00
if ($coreAdmin->ping()) {
$indexFieldsInfo['noError'] = 'OK';
2021-04-24 04:44:44 +02:00
$indexFieldsInfo['coreMetrics'] = 'dedede';
2021-04-17 00:26:33 +02:00
} else {
$indexFieldsInfo['noError'] = null;
$this->addFlashMessage(
'',
2021-04-17 21:20:54 +02:00
'Unable to contact Meilisearch server: ' . $this->selectedSite->getLabel() . ' ' . $coreAdmin->getCorePath(),
2021-04-17 00:26:33 +02:00
FlashMessage::ERROR
);
}
2021-04-24 04:44:44 +02:00
$indexFieldsInfoByCorePaths[$i] = $indexFieldsInfo;
2021-04-17 00:26:33 +02:00
}
$this->view->assign('indexFieldsInfoByCorePaths', $indexFieldsInfoByCorePaths);
}
/**
* Retrieves the information for the index inspector.
*
* @return void
*/
protected function collectIndexInspectorInfo()
{
2021-04-24 04:44:44 +02:00
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
2021-04-17 00:26:33 +02:00
$documentsByCoreAndType = [];
2021-04-17 21:20:54 +02:00
foreach ($meilisearchCoreConnections as $languageId => $meilisearchCoreConnection) {
$coreAdmin = $meilisearchCoreConnection->getAdminService();
$documents = $this->apacheMeilisearchDocumentRepository->findByPageIdAndByLanguageId($this->selectedPageUID, $languageId);
2021-04-17 00:26:33 +02:00
$documentsByType = [];
foreach ($documents as $document) {
$documentsByType[$document['type']][] = $document;
}
$documentsByCoreAndType[$languageId]['core'] = $coreAdmin;
$documentsByCoreAndType[$languageId]['documents'] = $documentsByType;
}
$this->view->assignMultiple([
'pageId' => $this->selectedPageUID,
'indexInspectorDocumentsByLanguageAndType' => $documentsByCoreAndType
]);
}
/**
* Gets field metrics.
*
* @param ResponseAdapter $lukeData Luke index data
* @param string $limitNote Note to display if there are too many documents in the index to show number of terms for a field
*
* @return array An array of field metrics
*/
protected function getFields(ResponseAdapter $lukeData, $limitNote)
{
$rows = [];
$fields = (array)$lukeData->fields;
foreach ($fields as $name => $field) {
$rows[$name] = [
'name' => $name,
'type' => $field->type,
'docs' => isset($field->docs) ? $field->docs : 0,
'terms' => isset($field->distinct) ? $field->distinct : $limitNote
];
}
ksort($rows);
return $rows;
}
/**
* Gets general core metrics.
*
* @param ResponseAdapter $lukeData Luke index data
* @param array $fields Fields metrics
*
* @return array An array of core metrics
*/
protected function getCoreMetrics(ResponseAdapter $lukeData, array $fields)
{
$coreMetrics = [
'numberOfDocuments' => $lukeData->index->numDocs,
'numberOfDeletedDocuments' => $lukeData->index->deletedDocs,
'numberOfTerms' => $lukeData->index->numTerms,
'numberOfFields' => count($fields)
];
return $coreMetrics;
}
}