meilisearch/Classes/Report/MeilisearchStatus.php

264 lines
9.2 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\Report;
/***************************************************************
* Copyright notice
*
* (c) 2009-2015 Ingo Renner <ingo@typo3.org>
* 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!
***************************************************************/
use WapplerSystems\Meilisearch\ConnectionManager;
use WapplerSystems\Meilisearch\Domain\Site\SiteRepository;
use WapplerSystems\Meilisearch\PingFailedException;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\Service\MeilisearchAdminService;
2021-04-17 00:26:33 +02:00
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Reports\Status;
use TYPO3\CMS\Reports\StatusProviderInterface;
/**
2021-04-17 21:20:54 +02:00
* Provides an status report about whether a connection to the Meilisearch server can
2021-04-17 00:26:33 +02:00
* be established.
*
* @author Ingo Renner <ingo@typo3.org>
*/
2021-04-17 21:20:54 +02:00
class MeilisearchStatus extends AbstractMeilisearchStatus
2021-04-17 00:26:33 +02:00
{
/**
* Site Repository
*
* @var SiteRepository
*/
protected $siteRepository = null;
/**
* Connection Manager
*
* @var ConnectionManager
*/
protected $connectionManager = null;
/**
* Holds the response status
*
* @var int
*/
protected $responseStatus = Status::OK;
/**
* Holds the response message build by the checks
*
* @var string
*/
protected $responseMessage = '';
/**
2021-04-17 21:20:54 +02:00
* MeilisearchStatus constructor.
2021-04-17 00:26:33 +02:00
* @param SiteRepository|null $siteRepository
* @param ConnectionManager|null $connectionManager
*/
public function __construct(SiteRepository $siteRepository = null, ConnectionManager $connectionManager = null)
{
$this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
$this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
}
/**
2021-04-17 21:20:54 +02:00
* Compiles a collection of status checks against each configured Meilisearch server.
2021-04-17 00:26:33 +02:00
*
*/
public function getStatus()
{
$reports = [];
foreach ($this->siteRepository->getAvailableSites() as $site) {
2021-04-24 04:44:44 +02:00
foreach ($site->getMeilisearchConnectionConfigurations() as $meilisearchConfiguration) {
2021-04-17 21:20:54 +02:00
$reports[] = $this->getConnectionStatus($meilisearchConfiguration);
2021-04-17 00:26:33 +02:00
}
}
return $reports;
}
/**
2021-04-17 21:20:54 +02:00
* Checks whether a Meilisearch server is available and provides some information.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @param array $meilisearchConnection Meilisearch connection parameters
* @return Status Status of the Meilisearch connection
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected function getConnectionStatus(array $meilisearchConnection)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$header = 'Your site has contacted the Meilisearch server.';
2021-04-17 00:26:33 +02:00
$this->responseStatus = Status::OK;
2021-04-17 21:20:54 +02:00
$meilisearchAdmin = $this->connectionManager
2021-04-24 04:44:44 +02:00
->getMeilisearchConnectionForNode($meilisearchConnection['read'], $meilisearchConnection['write'])
2021-04-17 00:26:33 +02:00
->getAdminService();
2021-04-17 21:20:54 +02:00
$meilisearchVersion = $this->checkMeilisearchVersion($meilisearchAdmin);
$accessFilter = $this->checkAccessFilter($meilisearchAdmin);
$pingTime = $this->checkPingTime($meilisearchAdmin);
$configName = $this->checkMeilisearchConfigName($meilisearchAdmin);
$schemaName = $this->checkMeilisearchSchemaName($meilisearchAdmin);
2021-04-17 00:26:33 +02:00
if ($this->responseStatus !== Status::OK) {
2021-04-17 21:20:54 +02:00
$header = 'Failed contacting the Meilisearch server.';
2021-04-17 00:26:33 +02:00
}
$variables = [
'header' => $header,
2021-04-17 21:20:54 +02:00
'connection' => $meilisearchConnection,
'meilisearch' => $meilisearchAdmin,
'meilisearchVersion' => $meilisearchVersion,
2021-04-17 00:26:33 +02:00
'pingTime' => $pingTime,
'configName' => $configName,
'schemaName' => $schemaName,
'accessFilter' => $accessFilter
];
2021-04-17 21:20:54 +02:00
$report = $this->getRenderedReport('MeilisearchStatus.html', $variables);
2021-04-17 00:26:33 +02:00
return GeneralUtility::makeInstance(
Status::class,
2021-04-17 21:20:54 +02:00
/** @scrutinizer ignore-type */ 'Meilisearch',
2021-04-17 00:26:33 +02:00
/** @scrutinizer ignore-type */ '',
/** @scrutinizer ignore-type */ $report,
/** @scrutinizer ignore-type */ $this->responseStatus
);
}
/**
2021-04-17 21:20:54 +02:00
* Checks the meilisearch version and adds it to the report.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchAdminService $meilisearch
* @return string meilisearch version
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected function checkMeilisearchVersion(MeilisearchAdminService $meilisearch)
2021-04-17 00:26:33 +02:00
{
try {
2021-04-17 21:20:54 +02:00
$meilisearchVersion = $this->formatMeilisearchVersion($meilisearch->getMeilisearchServerVersion());
2021-04-17 00:26:33 +02:00
} catch (\Exception $e) {
$this->responseStatus = Status::ERROR;
2021-04-17 21:20:54 +02:00
$meilisearchVersion = 'Error getting meilisearch version: ' . $e->getMessage();
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
return $meilisearchVersion;
2021-04-17 00:26:33 +02:00
}
/**
* Checks the access filter setup and adds it to the report.
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchAdminService $meilisearchAdminService
2021-04-17 00:26:33 +02:00
* @return string
*/
2021-04-17 21:20:54 +02:00
protected function checkAccessFilter(MeilisearchAdminService $meilisearchAdminService)
2021-04-17 00:26:33 +02:00
{
try {
$accessFilterPluginStatus = GeneralUtility::makeInstance(AccessFilterPluginInstalledStatus::class);
2021-04-17 21:20:54 +02:00
$accessFilterPluginVersion = $accessFilterPluginStatus->getInstalledPluginVersion($meilisearchAdminService);
2021-04-17 00:26:33 +02:00
$accessFilterMessage = $accessFilterPluginVersion;
} catch (\Exception $e) {
$this->responseStatus = Status::ERROR;
$accessFilterMessage = 'Error getting access filter: ' . $e->getMessage();
}
return $accessFilterMessage;
}
/**
* Checks the ping time and adds it to the report.
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchAdminService $meilisearchAdminService
2021-04-17 00:26:33 +02:00
* @return string
*/
2021-04-17 21:20:54 +02:00
protected function checkPingTime(MeilisearchAdminService $meilisearchAdminService)
2021-04-17 00:26:33 +02:00
{
try {
2021-04-17 21:20:54 +02:00
$pingQueryTime = $meilisearchAdminService->getPingRoundTripRuntime();
2021-04-17 00:26:33 +02:00
$pingMessage = (int)$pingQueryTime . ' ms';
} catch (PingFailedException $e) {
$this->responseStatus = Status::ERROR;
$pingMessage = 'Ping error: ' . $e->getMessage();
}
return $pingMessage;
}
/**
2021-04-17 21:20:54 +02:00
* Checks the meilisearch config name and adds it to the report.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchAdminService $meilisearchAdminService
2021-04-17 00:26:33 +02:00
* @return string
*/
2021-04-17 21:20:54 +02:00
protected function checkMeilisearchConfigName(MeilisearchAdminService $meilisearchAdminService)
2021-04-17 00:26:33 +02:00
{
try {
2021-04-17 21:20:54 +02:00
$meilisearchConfigMessage = $meilisearchAdminService->getMeilisearchconfigName();
2021-04-17 00:26:33 +02:00
} catch (\Exception $e) {
$this->responseStatus = Status::ERROR;
2021-04-17 21:20:54 +02:00
$meilisearchConfigMessage = 'Error determining meilisearch config: ' . $e->getMessage();
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
return $meilisearchConfigMessage;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Checks the meilisearch schema name and adds it to the report.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchAdminService $meilisearchAdminService
2021-04-17 00:26:33 +02:00
* @return string
*/
2021-04-17 21:20:54 +02:00
protected function checkMeilisearchSchemaName(MeilisearchAdminService $meilisearchAdminService)
2021-04-17 00:26:33 +02:00
{
try {
2021-04-17 21:20:54 +02:00
$meilisearchSchemaMessage = $meilisearchAdminService->getSchema()->getName();
2021-04-17 00:26:33 +02:00
} catch (\Exception $e) {
$this->responseStatus = Status::ERROR;
2021-04-17 21:20:54 +02:00
$meilisearchSchemaMessage = 'Error determining schema name: ' . $e->getMessage();
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
return $meilisearchSchemaMessage;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Formats the Meilisearch server version number. By default this is going
2021-04-17 00:26:33 +02:00
* to be the simple major.minor.patch-level version. Custom Builds provide
* more information though, in case of custom builds, their complete
* version will be added, too.
*
2021-04-17 21:20:54 +02:00
* @param string $meilisearchVersion Unformatted Meilisearch version number as provided by Meilisearch.
2021-04-17 00:26:33 +02:00
* @return string formatted short version number, in case of custom builds followed by the complete version number
*/
2021-04-17 21:20:54 +02:00
protected function formatMeilisearchVersion($meilisearchVersion)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$explodedMeilisearchVersion = explode('.', $meilisearchVersion);
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
$shortMeilisearchVersion = $explodedMeilisearchVersion[0]
. '.' . $explodedMeilisearchVersion[1]
. '.' . $explodedMeilisearchVersion[2];
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
$formattedMeilisearchVersion = $shortMeilisearchVersion;
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
if ($meilisearchVersion != $shortMeilisearchVersion) {
$formattedMeilisearchVersion .= ' (' . $meilisearchVersion . ')';
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
return $formattedMeilisearchVersion;
2021-04-17 00:26:33 +02:00
}
}