Show in server info page which natural sorting method is used

This should ease debugging
This commit is contained in:
Jan Böhmer 2024-07-28 14:13:34 +02:00
parent 5ede61118c
commit d6ff22fc44
5 changed files with 129 additions and 5 deletions

View file

@ -22,16 +22,17 @@ declare(strict_types=1);
*/ */
namespace App\Controller; namespace App\Controller;
use Symfony\Component\Runtime\SymfonyRuntime;
use App\Services\Attachments\AttachmentSubmitHandler; use App\Services\Attachments\AttachmentSubmitHandler;
use App\Services\Attachments\AttachmentURLGenerator; use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\Attachments\BuiltinAttachmentsFinder; use App\Services\Attachments\BuiltinAttachmentsFinder;
use App\Services\Doctrine\DBInfoHelper;
use App\Services\Doctrine\NatsortDebugHelper;
use App\Services\Misc\GitVersionInfo; use App\Services\Misc\GitVersionInfo;
use App\Services\Misc\DBInfoHelper;
use App\Services\System\UpdateAvailableManager; use App\Services\System\UpdateAvailableManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Runtime\SymfonyRuntime;
#[Route(path: '/tools')] #[Route(path: '/tools')]
class ToolsController extends AbstractController class ToolsController extends AbstractController
@ -45,7 +46,7 @@ class ToolsController extends AbstractController
} }
#[Route(path: '/server_infos', name: 'tools_server_infos')] #[Route(path: '/server_infos', name: 'tools_server_infos')]
public function systemInfos(GitVersionInfo $versionInfo, DBInfoHelper $DBInfoHelper, public function systemInfos(GitVersionInfo $versionInfo, DBInfoHelper $DBInfoHelper, NatsortDebugHelper $natsortDebugHelper,
AttachmentSubmitHandler $attachmentSubmitHandler, UpdateAvailableManager $updateAvailableManager): Response AttachmentSubmitHandler $attachmentSubmitHandler, UpdateAvailableManager $updateAvailableManager): Response
{ {
$this->denyAccessUnlessGranted('@system.server_infos'); $this->denyAccessUnlessGranted('@system.server_infos');
@ -93,6 +94,8 @@ class ToolsController extends AbstractController
'db_size' => $DBInfoHelper->getDatabaseSize(), 'db_size' => $DBInfoHelper->getDatabaseSize(),
'db_name' => $DBInfoHelper->getDatabaseName() ?? 'Unknown', 'db_name' => $DBInfoHelper->getDatabaseName() ?? 'Unknown',
'db_user' => $DBInfoHelper->getDatabaseUsername() ?? 'Unknown', 'db_user' => $DBInfoHelper->getDatabaseUsername() ?? 'Unknown',
'db_natsort_method' => $natsortDebugHelper->getNaturalSortMethod(),
'db_natsort_slow_allowed' => $natsortDebugHelper->isSlowNaturalSortAllowed(),
//New version section //New version section
'new_version_available' => $updateAvailableManager->isUpdateAvailable(), 'new_version_available' => $updateAvailableManager->isUpdateAvailable(),

View file

@ -55,6 +55,15 @@ class Natsort extends FunctionNode
self::$allowSlowNaturalSort = $allow; self::$allowSlowNaturalSort = $allow;
} }
/**
* Check if the slow natural sort is allowed
* @return bool
*/
public static function isSlowNaturalSortAllowed(): bool
{
return self::$allowSlowNaturalSort;
}
/** /**
* Check if the MariaDB version which is connected to supports the natural sort (meaning it has a version of 10.7.0 or higher) * Check if the MariaDB version which is connected to supports the natural sort (meaning it has a version of 10.7.0 or higher)
* The result is cached in memory. * The result is cached in memory.

View file

@ -1,4 +1,22 @@
<?php <?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1); declare(strict_types=1);
@ -20,10 +38,10 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
namespace App\Services\Misc; namespace App\Services\Doctrine;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Platforms\SQLitePlatform;

View file

@ -0,0 +1,86 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Services\Doctrine;
use App\Doctrine\Functions\Natsort;
use App\Entity\Parts\Part;
use Doctrine\ORM\EntityManagerInterface;
/**
* This service allows to debug the natsort function by showing various information about the current state of
* the natsort function.
*/
class NatsortDebugHelper
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
// This is a dummy constructor
}
/**
* Check if the slow natural sort is allowed on the Natsort function.
* If it is not, then the request handler might need to be adjusted.
* @return bool
*/
public function isSlowNaturalSortAllowed(): bool
{
return Natsort::isSlowNaturalSortAllowed();
}
public function getNaturalSortMethod(): string
{
//Construct a dummy query which uses the Natsort function
$query = $this->entityManager->createQuery('SELECT natsort(1) FROM ' . Part::class . ' p');
$sql = $query->getSQL();
//Remove the leading SELECT and the trailing semicolon
$sql = substr($sql, 7, -1);
//Remove AS and everything afterwards
$sql = preg_replace('/\s+AS\s+.*/', '', $sql);
//If just 1 is returned, then we use normal (non-natural sorting)
if ($sql === '1') {
return 'Disabled';
}
if (str_contains( $sql, 'COLLATE numeric')) {
return 'Native (PostgreSQL)';
}
if (str_contains($sql, 'NATURAL_SORT_KEY')) {
return 'Native (MariaDB)';
}
if (str_contains($sql, 'COLLATE NATURAL_CMP')) {
return 'Emulation via PHP (SQLite)';
}
if (str_contains($sql, 'NatSortKey')) {
return 'Emulation via custom function (MySQL)';
}
return 'Unknown ('. $sql . ')';
}
}

View file

@ -21,5 +21,13 @@
<td>Database User</td> <td>Database User</td>
<td>{{ db_user }}</td> <td>{{ db_user }}</td>
</tr> </tr>
<tr>
<td>Natural sort method</td>
<td>{{ db_natsort_method }}</td>
</tr>
<tr>
<td>Slow natural sort allowed</td>
<td>{{ helper.boolean_badge(db_natsort_slow_allowed) }}</td>
</tr>
</tbody> </tbody>
</table> </table>