Skip to content

Commit

Permalink
Fixing Datetime issues
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Mar 9, 2023
1 parent 26397e2 commit ba553d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
21 changes: 8 additions & 13 deletions src/module-elasticsuite-indices/Model/IndexStatusProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ba553d7

Please sign in to comment.