From ba553d707a7d4870145a7edfdd608766bc1d5de7 Mon Sep 17 00:00:00 2001 From: Romain Ruaud Date: Thu, 9 Mar 2023 17:48:09 +0100 Subject: [PATCH] Fixing Datetime issues --- .../Model/IndexStatusProvider.php | 21 +++++++------------ .../WorkingIndexer/Collection.php | 5 ++++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/module-elasticsuite-indices/Model/IndexStatusProvider.php b/src/module-elasticsuite-indices/Model/IndexStatusProvider.php index afca79552..be00cbfe9 100644 --- a/src/module-elasticsuite-indices/Model/IndexStatusProvider.php +++ b/src/module-elasticsuite-indices/Model/IndexStatusProvider.php @@ -118,7 +118,7 @@ private function isRebuilding(string $indexName, $indexDate): bool foreach (array_keys($this->workingIndexers) as $indexKey) { if (strpos((string) $indexName, $indexKey) !== false) { $today = new DateTime('Ymd'); - $day = new DateTime($indexDate); + $day = new DateTime($indexDate); return ($today == $day); } @@ -149,13 +149,14 @@ private function isExternal(string $indexName): bool /** * Returns if index is ghost. * - * @param DateInterval|false $indexDate Index updated date. + * @param DateTime $indexDate Index updated date. + * * @return bool */ private function isGhost($indexDate): bool { try { - return (new DateTime())->sub($indexDate)->getTimestamp() / self::SECONDS_IN_DAY >= self::NUMBER_DAYS_AFTER_INDEX_IS_GHOST; + return (new DateTime())->diff($indexDate)->days >= self::NUMBER_DAYS_AFTER_INDEX_IS_GHOST; } catch (Exception $e) { return false; } @@ -174,6 +175,7 @@ private function isLive($alias): bool /** * Get index updated date from index name. + * @SuppressWarnings(PHPMD.StaticAccess) * * @param string $indexName Index name. * @param string $alias Index alias. @@ -189,24 +191,17 @@ private function getIndexUpdatedDateFromIndexName($indexName, $alias) return false; } - $count = 0; $format = ''; foreach ($matches[1] as $value) { - $count += strlen($value); $format .= $value; } try { // Remove alias from index name since next preg_replace would fail if alias is containing numbers. - $indexName = str_replace($alias ?? '', '', $indexName); - $date = substr(preg_replace('/[^0-9]/', '', $indexName), -$count); - - // Tracking indices are built monthly and does not fit with standard pattern containing datetime with hours. - if (strlen($date) !== 14) { - return false; - } + $indexName = str_replace($alias ?? $this->indexSettingsHelper->getIndexAlias(), '', $indexName); + $date = preg_replace('/[^0-9]/', '', $indexName); - return (new \DateTime($date))->format($format); + return DateTime::createFromFormat($format, $date); } catch (Exception $e) { return false; } diff --git a/src/module-elasticsuite-indices/Model/ResourceModel/WorkingIndexer/Collection.php b/src/module-elasticsuite-indices/Model/ResourceModel/WorkingIndexer/Collection.php index 9a684b567..66a7bce45 100644 --- a/src/module-elasticsuite-indices/Model/ResourceModel/WorkingIndexer/Collection.php +++ b/src/module-elasticsuite-indices/Model/ResourceModel/WorkingIndexer/Collection.php @@ -90,7 +90,10 @@ public function loadData($printQuery = false, $logQuery = false): Collection if ($indexer->getStatus() === StateInterface::STATUS_WORKING) { $item = $this->prepareItem($indexer); if (array_key_exists($item['indexer_id'], $indicesMapping)) { - $indexUpdateDate = DateTime::createFromFormat(DateTimeInterface::ISO8601, $item['indexer_updated']); + $indexUpdateDate = DateTime::createFromFormat( + \Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT, + $item['indexer_updated'] + ); $indexNameSuffix = $this->indexSettings->getIndexNameSuffix($indexUpdateDate); foreach ($indicesMapping[$item['indexer_id']] as $index) {