Skip to content

Commit 0675bde

Browse files
committed
MDEE-342: limit feed full reindex by days.
1 parent 5208440 commit 0675bde

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

DataExporter/Model/Indexer/AllIdsResolver.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\DataExporter\Model\Indexer;
99

10+
use DateTime;
1011
use Magento\Framework\App\ResourceConnection;
1112
use Magento\Framework\DB\Select;
1213

@@ -45,8 +46,10 @@ public function getAllIds(FeedIndexMetadata $metadata): ?\Generator
4546
$connection = $this->resourceConnection->getConnection();
4647
$lastKnownId = -1;
4748
$continueReindex = true;
49+
$dateTime = date_create();
50+
4851
while ($continueReindex) {
49-
$ids = $connection->fetchAll($this->getIdsSelect((int)$lastKnownId, $metadata));
52+
$ids = $connection->fetchAll($this->getIdsSelect((int)$lastKnownId, $metadata, $dateTime));
5053
if (empty($ids)) {
5154
$continueReindex = false;
5255
} else {
@@ -61,25 +64,37 @@ public function getAllIds(FeedIndexMetadata $metadata): ?\Generator
6164
*
6265
* @param int $lastKnownId
6366
* @param FeedIndexMetadata $metadata
67+
* @param DateTime $dateTime
6468
* @return Select
6569
*/
66-
private function getIdsSelect(int $lastKnownId, FeedIndexMetadata $metadata): Select
70+
private function getIdsSelect(int $lastKnownId, FeedIndexMetadata $metadata, DateTime $dateTime): Select
6771
{
6872
$connection = $this->resourceConnection->getConnection();
6973
$tableName = $this->resourceConnection->getTableName($metadata->getSourceTableName());
7074

7175
$columnExpression = sprintf('s.%s', $metadata->getSourceTableIdentityField());
7276
$whereClause = sprintf('s.%s > ?', $metadata->getSourceTableIdentityField());
7377

74-
return $connection->select()
78+
$select = $connection->select()
7579
->from(
7680
['s' => $tableName],
7781
[
7882
$metadata->getFeedIdentity() => 's.' . $metadata->getSourceTableField(),
7983
self::IDENTITY_FIELD_NAME => 's.' . $metadata->getSourceTableIdentityField()
8084
]
8185
)
82-
->where($whereClause, $lastKnownId)
86+
->where($whereClause, $lastKnownId);
87+
88+
if ($metadata->getFullReIndexDaysLimit() != 0) {
89+
$select->where(sprintf(
90+
"s.%s >= DATE_SUB(STR_TO_DATE('%s', '%%Y-%%m-%%d %%H:%%i:%%s'), INTERVAL %d DAY)",
91+
$metadata->getSourceTableFieldOnFullReIndexDaysLimit(),
92+
$dateTime->format('Y-m-d H:i:s'),
93+
$metadata->getFullReIndexDaysLimit()
94+
));
95+
}
96+
97+
return $select
8398
->order($columnExpression)
8499
->limit($metadata->getBatchSize());
85100
}

DataExporter/Model/Indexer/FeedIndexMetadata.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class FeedIndexMetadata
5959
*/
6060
private $sourceTableIdentityField;
6161

62+
/**
63+
* @var int
64+
*/
65+
private $fullReindexDaysLimit;
66+
67+
/**
68+
* @var string
69+
*/
70+
private $sourceTableFieldOnFullReIndexDaysLimit;
71+
6272
/**
6373
* @param string $feedName
6474
* @param string $sourceTableName
@@ -79,7 +89,9 @@ public function __construct(
7989
string $feedTableField,
8090
array $feedTableMutableColumns,
8191
int $batchSize = 100,
82-
string $sourceTableIdentityField = null
92+
string $sourceTableIdentityField = null,
93+
int $fullReIndexDaysLimit = 0,
94+
string $sourceTableFieldOnFullReIndexDaysLimit = 'updated_at'
8395
) {
8496
$this->feedName = $feedName;
8597
$this->sourceTableName = $sourceTableName;
@@ -90,6 +102,8 @@ public function __construct(
90102
$this->feedTableMutableColumns = $feedTableMutableColumns;
91103
$this->batchSize = $batchSize;
92104
$this->sourceTableIdentityField = $sourceTableIdentityField ?? $sourceTableField;
105+
$this->fullReindexDaysLimit = $fullReIndexDaysLimit;
106+
$this->sourceTableFieldOnFullReIndexDaysLimit = $sourceTableFieldOnFullReIndexDaysLimit;
93107
}
94108

95109
/**
@@ -181,4 +195,22 @@ public function getFeedTableMutableColumns(): array
181195
{
182196
return $this->feedTableMutableColumns;
183197
}
198+
199+
/**
200+
* Determines the amount of days back when triggering a full reindex
201+
* @return int the amount in days, 0 means no limit
202+
*/
203+
public function getFullReIndexDaysLimit(): int
204+
{
205+
return $this->fullReindexDaysLimit;
206+
}
207+
208+
/**
209+
* Table field name to use when full reindex is limited by days back (see fullReindexDaysLimit)
210+
* @return string the field name
211+
*/
212+
public function getSourceTableFieldOnFullReIndexDaysLimit(): string
213+
{
214+
return $this->sourceTableFieldOnFullReIndexDaysLimit;
215+
}
184216
}

SalesOrdersDataExporter/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<argument name="feedTableMutableColumns" xsi:type="array">
2020
<item name="feed_data" xsi:type="string">feed_data</item>
2121
</argument>
22+
<argument name="fullReIndexDaysLimit" xsi:type="number">7</argument>
23+
<argument name="sourceTableFieldOnFullReIndexDaysLimit" xsi:type="string">updated_at</argument>
2224
</arguments>
2325
</virtualType>
2426
<virtualType name="Magento\SalesOrdersDataExporter\Model\Indexer\OrdersFeedIndexer" type="Magento\DataExporter\Model\Indexer\FeedIndexer">

0 commit comments

Comments
 (0)