From 79e72c6c485e8e4f49edb0e08414aff930215855 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 19 Nov 2024 14:37:01 +0100 Subject: [PATCH] Make logController dynamic to which database-protocol is used --- api/src/Controller/LogController.php | 47 ++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/api/src/Controller/LogController.php b/api/src/Controller/LogController.php index 03e01ccd2..0d5adeb67 100644 --- a/api/src/Controller/LogController.php +++ b/api/src/Controller/LogController.php @@ -4,6 +4,9 @@ namespace App\Controller; +use CommonGateway\CoreBundle\Service\Cache\MongoDbClient; +use CommonGateway\CoreBundle\Service\Cache\MongoDbCollection; +use CommonGateway\CoreBundle\Service\Cache\PostgresqlClient; use CommonGateway\CoreBundle\Service\CacheService; use CommonGateway\CoreBundle\Service\RequestService; use DateTime; @@ -13,6 +16,7 @@ use MongoDB\Client; use MongoDB\Driver\Exception\InvalidArgumentException; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -30,13 +34,16 @@ */ class LogController extends AbstractController { - private CacheService $cacheService; - private RequestService $requestService; - public function __construct(CacheService $cacheService, RequestService $requestService) + private string $databaseType = 'mongodb'; + + public function __construct( + private readonly CacheService $cacheService, + private readonly RequestService $requestService, + private readonly ParameterBagInterface $parameterBag, + + ) { - $this->cacheService = $cacheService; - $this->requestService = $requestService; } /** @@ -47,12 +54,20 @@ public function __construct(CacheService $cacheService, RequestService $requestS public function logAction(Request $request): Response { $status = 200; - $client = new Client($this->getParameter('cache_url')); + + if (substr($this->parameterBag->get('cache_url', false), offset: 0, length: 5) === 'mongo') { + $client = new MongoDbClient($this->parameterBag->get('cache_url'), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService, cacheLogger: $this->logger); + $this->databaseType = 'mongodb'; + } + if (substr($this->parameterBag->get('cache_url', false), offset: 4, length: 5) === 'pgsql' || substr($this->parameters->get('cache_url', false), offset: 4, length: 4) === 'psql') { + $client = new PostgresqlClient($this->parameterBag->get('cache_url')); + $this->databaseType = 'postgresql'; + } $filter = $this->requestService->realRequestQueryAll($request->getQueryString()); $completeFilter = $filter; - if (isset($filter['_id'])) { + if (isset($filter['_id']) && $this->databaseType === 'mongodb') { try { $filter['_id'] = new ObjectId($filter['_id']); } catch (InvalidArgumentException $exception) { @@ -86,8 +101,22 @@ public function logAction(Request $request): Response $collection = $client->logs->logs; - $results = $collection->find($filter, ['limit' => $limit, 'skip' => $start, 'sort' => $order])->toArray(); - $total = $collection->countDocuments($filter); + if($collection instanceof MongoDbCollection === true) { + $results = $collection->find($filter, ['limit' => $limit, 'skip' => $start, 'sort' => $order])->toArray(); + $total = $collection->count($filter); + } else { + $results = $collection->find(filter: $completeFilter); + $total = $collection->count(filter: $filter); + + $results = array_map( + function ($value) { + $value['datetime'] = ['$date' => ['$numberLong' => (new DateTime($value['datetime']))->format('Uv')]]; + $value['_id'] = ['$oid' => $value['_id']]; + return $value; + }, + iterator_to_array($results) + ); + } $content = json_encode($this->cacheService->handleResultPagination($completeFilter, $results, $total));