Skip to content

Commit

Permalink
[WIP][FEATURE] Visualize rendering process, see #216
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbltr committed Mar 22, 2024
1 parent 21c2357 commit 620b2cd
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 48 deletions.
20 changes: 8 additions & 12 deletions Classes/Controller/BackendModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,16 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
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 .= '<div id="kesearch-indexer-running-warning" class="alert alert-danger">';
$content .= '<p>WARNING!</p>';
$content .= '<p>The indexer is already running and can not be started twice.</p>';
$content .= 'The indexer is already running and can not be started twice.';
$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 id="kesearch-indexer-running-warning" class="alert alert-info">';
$content .= 'The indexer is already running and can not be started twice.';
$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>';
Expand All @@ -212,13 +212,6 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
]
);
$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 Down Expand Up @@ -256,6 +249,9 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
}
}

$moduleUrl = $uriBuilder->buildUriFromRoute('web_KeSearchBackendModule', ['id' => $this->pageId]);
$content .= ' <a class="btn btn-default" id="kesearch-button-reload" href="' . $moduleUrl . '">Reload</a>';

$this->addMainMenu($request, $moduleTemplate, 'startIndexing');

if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 12) {
Expand Down Expand Up @@ -459,7 +455,7 @@ public function getLastIndexingReport()
*/
public function printIndexerConfigurations($indexerConfigurations)
{
$content = '<div id="kesearch-startindexing-indexers"><h2>Indexers</h2>';
$content = '<div id="kesearch-startindexing-indexers"><h2>Indexer configurations</h2>';
// show indexer names
if ($indexerConfigurations) {
$content .= '<div class="row"><div class="col-md-8">';
Expand Down
14 changes: 9 additions & 5 deletions Classes/Controller/IndexerStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ public function getStatusAction(ServerRequestInterface $request): ResponseInterf
{
$isRunning = $this->indexerStatusService->isRunning();
$indexerStatus = [];
$html = 'Indexer is idle.';
$html = '<div class="alert alert-notice">Indexer is idle.</div>';

if ($isRunning) {
$indexerStartTime = $this->indexerStatusService->getIndexerStartTime();
$indexerStatus = $this->indexerStatusService->getIndexerStatus();
$html = 'Indexer is running since ' . TimeUtility::getRunningTimeHumanReadable($indexerStartTime) . '.';
$html = '<div class="alert alert-success">Indexer is running.</div>';
if ($indexerStatus['indexers'] ?? false) {
$html .= '<br /><br /><strong>Indexers:</strong><br />';
$html .= '<div class="table-fit"><table class="table table-striped table-hover">';
foreach ($indexerStatus['indexers'] as $singleIndexerStatus) {
$statusLine = htmlspecialchars($singleIndexerStatus['statusText'], ENT_QUOTES, 'UTF-8') . '<br />';
$statusLine = htmlspecialchars($singleIndexerStatus['statusText'], ENT_QUOTES, 'UTF-8');
if ($singleIndexerStatus['status'] == $this->indexerStatusService::INDEXER_STATUS_RUNNING) {
$statusLine = '<strong>' . $statusLine . '</strong>';
$statusLine = '<tr class="table-success"><td><strong>' . $statusLine . '</strong></td></tr>';
} else {
$statusLine = '<tr><td>' . $statusLine . '</td></tr>';
}
$html .= $statusLine;
}
$html .= '</table></div>';
$html .= '<div class="alert alert-notice">Indexer is running since ' . TimeUtility::getRunningTimeHumanReadable($indexerStartTime) . '.' . '</div>';
}
}

Expand Down
26 changes: 14 additions & 12 deletions Classes/Indexer/IndexerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
$this->prepareStatements();

$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>';
$content .= '<tr><th>Indexer configuration</th><th>Mode</th><th>Info</th><th>Time</th></tr>';
foreach ($configurations as $indexerConfig) {
$this->indexerStatusService->setScheduledStatus($indexerConfig);
}
Expand All @@ -188,7 +188,7 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
$className .= GeneralUtility::underscoredToUpperCamelCase($this->indexerConfig['type']);
if (class_exists($className)) {
$this->logger->info(
'Indexer "' . $this->indexerConfig['title'] . '" started ',
'Running indexer configuration "' . $this->indexerConfig['title'] . '"',
$this->indexerConfig
);
$searchObj = GeneralUtility::makeInstance($className, $this);
Expand Down Expand Up @@ -217,7 +217,8 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['ke_search']['customIndexer'] as $_classRef) {
$searchObj = GeneralUtility::makeInstance($_classRef, $this);
$this->logger->info(
'Custom indexer "' . $this->indexerConfig['title'] . '" started',
'Running indexer configuration "' . $this->indexerConfig['title'] . '": '
. 'Trying to start custom indexer "' . $_classRef,
$this->indexerConfig
);
if ($indexingMode == IndexerBase::INDEXING_MODE_FULL || !method_exists($searchObj, 'startIncrementalIndexing')) {
Expand Down Expand Up @@ -245,14 +246,15 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index

// log finishing
$indexingTime = $this->endTime - $this->startTime;
$content .= '<div class="alert alert-info">';
$content .= '<div class="alert alert-success">';
$content .= '<h3>Finished</h3>';

$message = 'Indexing finished at ' . SearchHelper::formatTimestamp($this->endTime) . ' (took ' . $this->formatTime($indexingTime) . ').';
$content .= '<p>' . $message . '</p>';
$content .= $message;
$this->logger->info($message);

$message = 'Index contains ' . $this->indexRepository->getTotalNumberOfRecords() . ' entries.';
$content .= '<p>' . $message . '</p>';
$message = '<br /><i>Index contains ' . $this->indexRepository->getTotalNumberOfRecords() . ' entries.</i>';
$content .= $message;
$this->logger->info($message);

$content .= '</div>';
Expand Down Expand Up @@ -498,7 +500,7 @@ public function cleanUpProcessAfterIndexing()
*/
public function cleanUpIndex(int $indexingMode)
{
$content = '<div class="alert alert-info">';
$content = '<div class="alert alert-notice">';
$content .= '<h3>Cleanup</h3>';
if ($indexingMode == IndexerBase::INDEXING_MODE_FULL) {
$this->logger->info('Cleanup started');
Expand Down Expand Up @@ -533,19 +535,19 @@ public function cleanUpIndex(int $indexingMode)
->where($where)
->executeStatement();

$content .= '<p><strong>' . $count . '</strong> entries deleted.</p>' . "\n";
$content .= '<strong>' . $count . '</strong> entries deleted.' . "<br />\n";
$this->logger->info('CleanUpIndex: ' . $count . ' entries deleted.');

// rotate Sphinx Index (ke_search_premium function)
$content .= $this->rotateSphinxIndex();

// calculate duration of indexing process
$duration = ceil((microtime(true) - $startMicrotime) * 1000);
$content .= '<p><i>Cleanup process took ' . $duration . ' ms.</i></p>' . "\n";
$content .= '<i>Cleanup process took ' . $duration . ' ms.</i>' . "\n";
} else {
$message = 'Skipping cleanup in incremental mode';
$message = 'Skipping cleanup in incremental mode.';
$this->logger->info($message);
$content .= '<p>' . $message . '</p>';
$content .= $message;
}
$content .= '</div>';
return $content;
Expand Down
8 changes: 7 additions & 1 deletion Classes/Indexer/Types/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Tpwd\KeSearch\Indexer\IndexerRunner;
use Tpwd\KeSearch\Lib\Fileinfo;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\IndexerStatusService;
use Tpwd\KeSearch\Utility\FileUtility;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Resource\Folder;
Expand Down Expand Up @@ -82,6 +83,8 @@ class File extends IndexerBase
*/
private $indexRepository;

protected IndexerStatusService $indexerStatusService;

/**
* Initializes indexer for files
*
Expand All @@ -94,6 +97,7 @@ public function __construct(IndexerRunner $pObj)
$this->extConf = SearchHelper::getExtConf();
$this->fileInfo = GeneralUtility::makeInstance(Fileinfo::class);
$this->indexRepository = GeneralUtility::makeInstance(IndexRepository::class);
$this->indexerStatusService = GeneralUtility::makeInstance(IndexerStatusService::class);
}

/**
Expand Down Expand Up @@ -255,8 +259,10 @@ public function getAbsoluteDirectoryPath(array $directoryArray): array
public function extractContentAndSaveToIndex(array $files): int
{
$counter = 0;
if (count($files)) {
$totalCount = count($files);
if ($totalCount > 0) {
foreach ($files as $file) {
$this->indexerStatusService->setRunningStatus($this->indexerConfig, $counter, $totalCount);
if ($this->fileInfo->setFile($file)) {
if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
$filePath = $file->getForLocalProcessing(false);
Expand Down
5 changes: 1 addition & 4 deletions Classes/Indexer/Types/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
*/
class News extends IndexerBase
{
/**
* @var IndexerStatusService
*/
private $indexerStatusService;
protected IndexerStatusService $indexerStatusService;

/**
* Initializes indexer for news
Expand Down
19 changes: 13 additions & 6 deletions Classes/Indexer/Types/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Page extends IndexerBase
*/
protected AdditionalContentService $additionalContentService;

private IndexerStatusService $indexerStatusService;
protected IndexerStatusService $indexerStatusService;

/**
* tx_kesearch_indexer_types_page constructor.
Expand Down Expand Up @@ -270,6 +270,7 @@ public function startIndexing()
foreach ($indexPids as $uid) {
$this->indexerStatusService->setRunningStatus($this->indexerConfig, $counter, count($indexPids));
$this->getPageContent($uid);
$counter++;
}

$logMessage = 'Indexer "' . $this->indexerConfig['title'] . '" finished'
Expand All @@ -286,12 +287,18 @@ public function startIndexing()
}

// show indexer content
return
count($indexPids) . ' ' . $this->indexedElementsName . ' have been selected for indexing in the main language.' . chr(10)
$result =
count($indexPids) . ' pages have been selected for indexing in the main language.' . chr(10)
. count($this->sysLanguages) . ' languages (' . $languageTitles . ') have been found.' . chr(10)
. $this->counter . ' ' . $this->indexedElementsName . ' have been indexed. ' . chr(10)
. $this->counterWithoutContent . ' had no content or the content was not indexable.' . chr(10)
. $this->fileCounter . ' files have been indexed.';
. $this->counter . ' ' . $this->indexedElementsName . ' have been indexed. ' . chr(10);

if ($this->counterWithoutContent) {
$result .= $this->counterWithoutContent . ' had no content or the content was not indexable.' . chr(10);
}

$result .= $this->fileCounter . ' files have been indexed.';

return $result;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions Classes/Indexer/Types/TtAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Tpwd\KeSearch\Indexer\IndexerBase;
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\IndexerStatusService;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -35,12 +36,15 @@
*/
class TtAddress extends IndexerBase
{
protected IndexerStatusService $indexerStatusService;

/**
* Initializes indexer for tt_address
*/
public function __construct($pObj)
{
parent::__construct($pObj);
$this->indexerStatusService = GeneralUtility::makeInstance(IndexerStatusService::class);
}

/**
Expand Down Expand Up @@ -81,12 +85,15 @@ public function startIndexing()
->fetchAllAssociative();

// no address records found
if (!count($addressRows)) {
$totalCount = count($addressRows);
if ($totalCount == 0) {
$content = 'No address records found!';
return $content;
}
$counter = 0;
foreach ($addressRows as $addressRow) {
$shouldBeIndexed = true;
$this->indexerStatusService->setRunningStatus($this->indexerConfig, $counter, $totalCount);

if (!$this->recordIsLive($addressRow)) {
$shouldBeIndexed = false;
Expand Down Expand Up @@ -257,9 +264,10 @@ public function startIndexing()
false, // debug only?
$additionalFields // additional fields added by hooks
);
$counter++;
}

return count($addressRows) . ' address records have been indexed.';
return $counter . ' address records have been indexed.';
}

/**
Expand Down
5 changes: 5 additions & 0 deletions Classes/Indexer/Types/TtNews.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Tpwd\KeSearch\Indexer\IndexerRunner;
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\IndexerStatusService;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -38,6 +39,8 @@ class TtNews extends IndexerBase
/** @var int $fileCounter */
protected $fileCounter = 0;

protected IndexerStatusService $indexerStatusService;

/**
* Initializes indexer for tt_news
*
Expand All @@ -48,6 +51,7 @@ public function __construct($pObj)
parent::__construct($pObj);
$this->pObj = $pObj;
$this->fileRepository = GeneralUtility::makeInstance(FileRepository::class);
$this->indexerStatusService = GeneralUtility::makeInstance(IndexerStatusService::class);
}

/**
Expand Down Expand Up @@ -96,6 +100,7 @@ public function startIndexing()

if ($resCount) {
while (($newsRecord = $res->fetchAssociative())) {
$this->indexerStatusService->setRunningStatus($this->indexerConfig, $indexedNewsCounter, $resCount);
$shouldBeIndexed = true;

if (!$this->recordIsLive($newsRecord)) {
Expand Down
5 changes: 4 additions & 1 deletion Classes/Service/IndexerStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ public function setRunningStatus(array $indexerConfig, int $currentRecordCount =
'statusText' =>
'"' . $indexerConfig['title'] . '"'
. ' is running'
. ' (' . $currentRecordCount . ' / ' . $totalRecordCount . ' records)'
];
if ($currentRecordCount >= 0 && $totalRecordCount >= 0) {
$indexerStatus['indexers'][$indexerConfig['uid']]['statusText'] .=
' (' . $currentRecordCount . ' / ' . $totalRecordCount . ' records)';
}
$this->setIndexerStatus($indexerStatus);
}

Expand Down
4 changes: 2 additions & 2 deletions Classes/Utility/TimeUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TimeUtility
{
public static function getRunningTime(int $indexerStartTime): int
{
return $indexerStartTime ? (time() - $indexerStartTime) : 0;
return $indexerStartTime ? (time() - $indexerStartTime) : -1;
}

public static function getTimeHoursMinutesSeconds(int $time): array
Expand All @@ -23,7 +23,7 @@ public static function getTimeHoursMinutesSeconds(int $time): array
public static function getRunningTimeHumanReadable(int $indexerStartTime): string
{
$indexerRunningTime = TimeUtility::getRunningTime($indexerStartTime);
if ($indexerRunningTime < 1) {
if ($indexerRunningTime < 0) {
return '';
}
$indexerRunningTimeHMS = TimeUtility::getTimeHoursMinutesSeconds($indexerRunningTime);
Expand Down
4 changes: 2 additions & 2 deletions Resources/Private/Templates/BackendModule/StartIndexing.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<f:section name="Content">
<div class="row">
<div class="col-md-8">
<div id="kesearch-indexer-status" class="alert alert-notice">Waiting for indexer status.</div>
<div class="col-md-8" id="kesearch-indexer-status">
<div class="alert alert-notice">Waiting for indexer status.</div>
</div>
</div>
<f:format.raw>{content}</f:format.raw>
Expand Down
Loading

0 comments on commit 620b2cd

Please sign in to comment.