Skip to content

Commit

Permalink
[FEATURE] Visualize rendering process, see #216
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbltr committed Mar 15, 2024
1 parent a59ae80 commit 21c2357
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 53 deletions.
29 changes: 21 additions & 8 deletions Classes/Controller/BackendModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,33 +179,46 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
// show information about indexer configurations and number of records
// if action "start indexing" is not selected
if ($this->do != 'startindexer') {
$content .= '<div id="kesearch-indexer-overview">';
$content .= $this->printNumberOfRecords();
$content .= $this->printIndexerConfigurations($indexerConfigurations);
$content .= '</div>';
}

// show "start indexing" or "remove lock" button
if ($lockTime !== 0) {
if (!$this->getBackendUser()->isAdmin()) {
// print warning message for non-admins
$content .= '<div class="row"><div class="col-md-8">';
$content .= '<div class="alert alert-danger">';
$content .= '<p>WARNING!</p>';
$content .= '<p>The indexer is already running and can not be started twice.</p>';
$content .= '</div>';
$content .= '</div></div>';
} else {
// show 'remove lock' button for admins
$content .= '<div class="row"><div class="col-md-8">';
$content .= '<div class="alert alert-info">';
$content .= '<p>The indexer is already running and can not be started twice.</p>';
$content .= '</div>';
$content .= '<p>The indexing process was started at <strong>' . SearchHelper::formatTimestamp($lockTime) . '.</p></strong>';
$content .= '<p>You can remove the lock by clicking the following button.</p>';
$content .= '</div></div>';
$moduleUrl = $uriBuilder->buildUriFromRoute(
'web_KeSearchBackendModule',
[
'id' => $this->pageId,
'do' => 'rmLock',
]
);
$content .= '<p><a class="btn btn-danger" href="' . $moduleUrl . '">Remove Lock</a></p>';
$content .= '<a class="btn btn-danger" id="kesearch-button-removelock" href="' . $moduleUrl . '">Remove Lock</a> ';
$moduleUrl = $uriBuilder->buildUriFromRoute(
'web_KeSearchBackendModule',
[
'id' => $this->pageId
]
);
$content .= ' <a class="btn btn-default" id="kesearch-button-reload" href="' . $moduleUrl . '">Reload</a>';
}
} else {
// no lock set - show "start indexer" link if indexer configurations have been found
Expand All @@ -217,9 +230,9 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
'do' => 'startindexer',
]
);
$content .= '<a class="btn btn-primary" href="' . $moduleUrl . '">'
$content .= '<a class="btn btn-primary" id="kesearch-button-start-full" href="' . $moduleUrl . '">'
. LocalizationUtility::translate('backend.start_indexer_full', 'ke_search')
. '</a> ';
. '</a>';
$moduleUrl = $uriBuilder->buildUriFromRoute(
'web_KeSearchBackendModule',
[
Expand All @@ -228,7 +241,7 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
'indexingMode' => IndexerBase::INDEXING_MODE_INCREMENTAL,
]
);
$content .= '<a class="btn btn-default" href="' . $moduleUrl . '">'
$content .= ' <a class="btn btn-info" id="kesearch-button-start-incremental" href="' . $moduleUrl . '">'
. LocalizationUtility::translate('backend.start_indexer_incremental', 'ke_search')
. '</a>';
} else {
Expand Down Expand Up @@ -446,7 +459,7 @@ public function getLastIndexingReport()
*/
public function printIndexerConfigurations($indexerConfigurations)
{
$content = '<h2>Indexers</h2>';
$content = '<div id="kesearch-startindexing-indexers"><h2>Indexers</h2>';
// show indexer names
if ($indexerConfigurations) {
$content .= '<div class="row"><div class="col-md-8">';
Expand All @@ -468,7 +481,7 @@ public function printIndexerConfigurations($indexerConfigurations)
. '</tr>';
}
$content .= '</table></div>';
$content .= '</div></div>';
$content .= '</div></div></div>';
}

return $content;
Expand All @@ -481,7 +494,7 @@ public function printIndexerConfigurations($indexerConfigurations)
*/
public function printNumberOfRecords()
{
$content = '<h2>Index statistics</h2>';
$content = '<div id="kesearch-startindexing-statistics"><h2>Index statistics</h2>';
$numberOfRecords = $this->indexRepository->getTotalNumberOfRecords();

if ($numberOfRecords) {
Expand Down Expand Up @@ -526,7 +539,7 @@ public function printNumberOfRecords()
}

$content .= '</table></div>';
$content .= '</div></div>';
$content .= '</div></div></div>';
}

return $content;
Expand Down
60 changes: 60 additions & 0 deletions Classes/Controller/IndexerStatusController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tpwd\KeSearch\Controller;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tpwd\KeSearch\Service\IndexerStatusService;
use Tpwd\KeSearch\Utility\TimeUtility;

class IndexerStatusController
{
private ResponseFactoryInterface $responseFactory;
private IndexerStatusService $indexerStatusService;

public function __construct(ResponseFactoryInterface $responseFactory, IndexerStatusService $indexerStatusService) {
$this->responseFactory = $responseFactory;
$this->indexerStatusService = $indexerStatusService;
}

public function getStatusAction(ServerRequestInterface $request): ResponseInterface
{
$isRunning = $this->indexerStatusService->isRunning();
$indexerStatus = [];
$html = 'Indexer is idle.';

if ($isRunning) {
$indexerStartTime = $this->indexerStatusService->getIndexerStartTime();
$indexerStatus = $this->indexerStatusService->getIndexerStatus();
$html = 'Indexer is running since ' . TimeUtility::getRunningTimeHumanReadable($indexerStartTime) . '.';
if ($indexerStatus['indexers'] ?? false) {
$html .= '<br /><br /><strong>Indexers:</strong><br />';
foreach ($indexerStatus['indexers'] as $singleIndexerStatus) {
$statusLine = htmlspecialchars($singleIndexerStatus['statusText'], ENT_QUOTES, 'UTF-8') . '<br />';
if ($singleIndexerStatus['status'] == $this->indexerStatusService::INDEXER_STATUS_RUNNING) {
$statusLine = '<strong>' . $statusLine . '</strong>';
}
$html .= $statusLine;
}
}
}

$result = [
'running' => $isRunning,
'indexers' => $indexerStatus['indexers'] ?? [],
'html' => $html,
];

$response = $this->responseFactory->createResponse()
->withHeader('Content-Type', 'application/json; charset=utf-8');
$response->getBody()->write(
json_encode($result, JSON_THROW_ON_ERROR),
);

return $response;
}

}
44 changes: 19 additions & 25 deletions Classes/Indexer/IndexerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
use Tpwd\KeSearch\Event\ModifyFieldValuesBeforeStoringEvent;
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\IndexerStatusService;
use Tpwd\KeSearch\Utility\AdditionalWordCharactersUtility;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Registry;
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
Expand Down Expand Up @@ -69,11 +69,6 @@ class IndexerRunner
*/
public $currentRow = [];

/**
* @var Registry
*/
public $registry;

/**
* @var Logger
*/
Expand All @@ -86,21 +81,23 @@ class IndexerRunner

private EventDispatcherInterface $eventDispatcher;
private IndexRepository $indexRepository;
private IndexerStatusService $indexerStatusService;

/**
* Constructor of this class
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
IndexRepository $indexRepository
IndexRepository $indexRepository,
IndexerStatusService $indexerStatusService
) {
$this->eventDispatcher = $eventDispatcher;
$this->indexRepository = $indexRepository;
$this->indexerStatusService = $indexerStatusService;

// get extension configuration array
$this->extConf = SearchHelper::getExtConf();
$this->extConfPremium = SearchHelper::getExtConfPremium();
$this->registry = GeneralUtility::makeInstance(Registry::class);

// fetch the list of the default indexers which come with ke_search
foreach ($GLOBALS['TCA']['tx_kesearch_indexerconfig']['columns']['type']['config']['items'] as $indexerType) {
Expand All @@ -121,7 +118,7 @@ public function __construct(
*/
public function startIndexing($verbose = true, $extConf = [], $mode = '', $indexingMode = IndexerBase::INDEXING_MODE_FULL)
{
$content = '<div class="row"><div class="col-md-8">';
$content = '<div class="row" id="kesearch-indexer-report"><div class="col-md-8">';
$content .= '<div class="alert alert-info">';
$message = 'Running indexing process in '
. ($indexingMode == IndexerBase::INDEXING_MODE_FULL ? 'full' : 'incremental') . ' mode';
Expand All @@ -141,16 +138,16 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
// write starting timestamp into registry
// this is a helper to delete all records which are older than starting timestamp in registry
// this also prevents starting the indexer twice
if ($this->registry->get('tx_kesearch', 'startTimeOfIndexer') === null) {
$this->registry->set('tx_kesearch', 'startTimeOfIndexer', time());
if (!$this->indexerStatusService->isRunning()) {
$this->indexerStatusService->startIndexerTime();
} else {
// check lock time
$lockTime = $this->registry->get('tx_kesearch', 'startTimeOfIndexer');
$lockTime = $this->indexerStatusService->getIndexerStartTime();
$compareTime = time() - (60 * 60 * 12);
if ($lockTime < $compareTime) {
// lock is older than 12 hours - remove
$this->registry->remove('tx_kesearch', 'startTimeOfIndexer');
$this->registry->set('tx_kesearch', 'startTimeOfIndexer', time());
$this->indexerStatusService->clearIndexerStartTime();
$this->indexerStatusService->startIndexerTime();
$this->logger->notice('lock has been removed because it is older than 12 hours' . time());
} else {
$this->logger->warning('lock is set, you can\'t start indexer twice.');
Expand Down Expand Up @@ -179,6 +176,10 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
$content .= '<div class="table-fit"><table class="table table-striped table-hover">';
$content .= '<tr><th>Indexer</th><th>Mode</th><th>Info</th><th>Time</th></tr>';
foreach ($configurations as $indexerConfig) {
$this->indexerStatusService->setScheduledStatus($indexerConfig);
}
foreach ($configurations as $indexerConfig) {
$this->indexerStatusService->setRunningStatus($indexerConfig);
$this->indexerConfig = $indexerConfig;

// run default indexers shipped with ke_search
Expand Down Expand Up @@ -229,6 +230,7 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
}
}
}
$this->indexerStatusService->setFinishedStatus($indexerConfig);
}
$content .= '</table></div>';

Expand Down Expand Up @@ -256,11 +258,7 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
$content .= '</div>';
$content .= '</div></div>';

$this->registry->set(
'tx_kesearch',
'lastRun',
['startTime' => $this->startTime, 'endTime' => $this->endTime, 'indexingTime' => $indexingTime]
);
$this->indexerStatusService->setLastRunTime($this->startTime, $this->endTime, $indexingTime);

// create plaintext report
$plaintextReport = $this->createPlaintextReport($content);
Expand Down Expand Up @@ -491,8 +489,7 @@ public function cleanUpProcessAfterIndexing()
Db::getDatabaseConnection('tx_kesearch_index')
->exec('DEALLOCATE PREPARE insertStmt');

// remove all entries from ke_search registry
$this->registry->removeAllByNamespace('tx_kesearch');
$this->indexerStatusService->clearAll();
}

/**
Expand All @@ -512,10 +509,7 @@ public function cleanUpIndex(int $indexingMode)
$queryBuilder = Db::getQueryBuilder('tx_kesearch_index');
$where = $queryBuilder->expr()->lt(
'tstamp',
$queryBuilder->quote(
$this->registry->get('tx_kesearch', 'startTimeOfIndexer'),
PDO::PARAM_INT
)
$queryBuilder->quote($this->indexerStatusService->getIndexerStartTime(), PDO::PARAM_INT)
);

// hook for cleanup
Expand Down
8 changes: 8 additions & 0 deletions Classes/Indexer/Types/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Tpwd\KeSearch\Indexer\IndexerRunner;
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\IndexerStatusService;
use Tpwd\KeSearch\Utility\ContentUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand All @@ -38,6 +39,11 @@
*/
class News extends IndexerBase
{
/**
* @var IndexerStatusService
*/
private $indexerStatusService;

/**
* Initializes indexer for news
*
Expand All @@ -47,6 +53,7 @@ public function __construct($pObj)
{
parent::__construct($pObj);
$this->pObj = $pObj;
$this->indexerStatusService = GeneralUtility::makeInstance(IndexerStatusService::class);
}

/**
Expand Down Expand Up @@ -125,6 +132,7 @@ public function startIndexing()
if ($resCount) {
while (($newsRecord = $res->fetchAssociative())) {
$shouldBeIndexed = true;
$this->indexerStatusService->setRunningStatus($this->indexerConfig, $indexedNewsCounter, $resCount);

// get category data for this news record (list of
// assigned categories and single view from category, if it exists)
Expand Down
7 changes: 7 additions & 0 deletions Classes/Indexer/Types/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\AdditionalContentService;
use Tpwd\KeSearch\Service\IndexerStatusService;
use Tpwd\KeSearch\Utility\ContentUtility;
use Tpwd\KeSearch\Utility\FileUtility;
use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
Expand Down Expand Up @@ -155,6 +156,8 @@ class Page extends IndexerBase
*/
protected AdditionalContentService $additionalContentService;

private IndexerStatusService $indexerStatusService;

/**
* tx_kesearch_indexer_types_page constructor.
* @param IndexerRunner $pObj
Expand Down Expand Up @@ -213,6 +216,8 @@ public function __construct($pObj)

// make filesProcessor
$this->filesProcessor = GeneralUtility::makeInstance(FilesProcessor::class);

$this->indexerStatusService = GeneralUtility::makeInstance(IndexerStatusService::class);
}

/**
Expand Down Expand Up @@ -261,7 +266,9 @@ public function startIndexing()
$this->addTagsToRecords($indexPids, $where);

// loop through pids and collect page content and tags
$counter = 0;
foreach ($indexPids as $uid) {
$this->indexerStatusService->setRunningStatus($this->indexerConfig, $counter, count($indexPids));
$this->getPageContent($uid);
}

Expand Down
Loading

0 comments on commit 21c2357

Please sign in to comment.