Skip to content

Commit 8509d75

Browse files
authored
Merge pull request #222 from magento-commerce/MDEE-376-read-on-demand-export
MDEE-376: create repository for reading exported orders
2 parents 000a448 + ebb1fa2 commit 8509d75

5 files changed

Lines changed: 156 additions & 43 deletions

File tree

SalesOrdersDataExporter/Console/Command/ExportOnDemand.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
1414
use Magento\Framework\Console\Cli;
1515
use Magento\Framework\Intl\DateTimeFactory;
16-
use Magento\SalesOrdersDataExporter\Console\Command\Link;
1716
use Magento\SalesOrdersDataExporter\Model\Indexer\DateTimeRangeOrderProcessor;
1817
use Symfony\Component\Console\Command\Command;
1918
use Symfony\Component\Console\Input\InputArgument;
@@ -22,6 +21,7 @@
2221

2322
/**
2423
* Command export orders since certain time in the past
24+
* @deprecated will be removed
2525
*/
2626
class ExportOnDemand extends Command
2727
{
@@ -45,8 +45,7 @@ public function __construct(
4545
DateTimeRangeOrderProcessor $processor,
4646
DateTimeFactory $dateTimeFactory,
4747
Link $link
48-
)
49-
{
48+
) {
5049
$this->logger = $logger;
5150
$this->metadata = $metadata;
5251
$this->processor = $processor;
@@ -93,9 +92,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9392
private function ensureAssignedUuids(DateTime $from, DateTime $to, OutputInterface $output): int
9493
{
9594
$returnCode = $this->linkCommand->
96-
prepareForExport(
95+
assignUuidsToOrderEntities(
9796
$this->metadata->getBatchSize(),
98-
$output,
9997
$from->format(DateTimeInterface::W3C),
10098
$to->format(DateTimeInterface::W3C)
10199
);

SalesOrdersDataExporter/Console/Command/Link.php

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
135135
? (new \DateTime($input->getOption(self::OPTION_TO)))->format(\DateTimeInterface::W3C)
136136
: null;
137137

138-
return $this->prepareForExport($batchSize, $output, $from, $to, $state);
138+
$output->writeln(
139+
sprintf(
140+
'<info>Start updating UUID with parameters [state=%s, from=%s, to=%s, batch_size=%s]</info>',
141+
$state ?: 'all',
142+
$from ?: date(\DateTimeInterface::W3C, 0),
143+
$to ?: date(\DateTimeInterface::W3C),
144+
$batchSize
145+
)
146+
);
147+
148+
$updatedEntities = $this->assignUuidsToOrderEntities($batchSize, $from, $to, $state);
149+
150+
$output->writeln("<info>Assign UUID completed successfully, $updatedEntities entities updated</info>");
151+
152+
return CLI::RETURN_SUCCESS;
139153
} catch (\Throwable $e) {
140154
$output->writeln('<error>Failed to update UUID. Check logs</error>');
141155
$this->logger->error(
@@ -150,51 +164,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
150164
* Updating UUID
151165
*
152166
* @param int $batchSize
153-
* @param OutputInterface $output
154-
* @param string $from
155-
* @param string $to
167+
* @param string|null $from
168+
* @param string|null $to
156169
* @param string $state
157170
* @return int
158171
*/
159-
public function prepareForExport(
172+
public function assignUuidsToOrderEntities(
160173
int $batchSize,
161-
OutputInterface $output,
162174
string $from = null,
163175
string $to = null,
164176
string $state = ''
165-
) {
166-
try {
167-
$output->writeln(
168-
sprintf(
169-
'<info>Start updating UUID with parameters [state=%s, from=%s, to=%s, batch_size=%s]</info>',
170-
$state ?: 'all',
171-
$from ?: date(\DateTimeInterface::W3C, 0),
172-
$to ?: date(\DateTimeInterface::W3C),
173-
$batchSize
174-
)
175-
);
176-
177-
$updatedEntities = 0;
178-
foreach ($this->getOrders($state, $batchSize, $from, $to) as $type => $entityIds) {
179-
$this->uuidManager->assignBulk($entityIds, $type);
180-
$updatedEntities += count($entityIds);
181-
}
182-
$output->writeln(
183-
sprintf(
184-
'<info>Update completed successfully, %s entities updated</info>',
185-
$updatedEntities
186-
)
187-
);
188-
return Cli::RETURN_SUCCESS;
189-
190-
} catch (\Throwable $e) {
191-
$output->writeln('<error>Failed to update UUID. Check logs</error>');
192-
$this->logger->error(
193-
sprintf('Command "%s" failed. Error message: %s', self::COMMAND_NAME, $e->getMessage())
194-
);
195-
196-
return Cli::RETURN_FAILURE;
177+
): int {
178+
$updatedEntities = 0;
179+
foreach ($this->getOrders($state, $batchSize, $from, $to) as $type => $entityIds) {
180+
$this->uuidManager->assignBulk($entityIds, $type);
181+
$updatedEntities += count($entityIds);
197182
}
183+
return $updatedEntities;
198184
}
199185

200186
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesOrdersDataExporter\Model;
9+
10+
use Generator;
11+
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\DB\Query\BatchIteratorFactory;
14+
15+
class OnDemandExportOrdersRepository
16+
{
17+
private FeedIndexMetadata $metadata;
18+
private ResourceConnection $resourceConnection;
19+
private BatchIteratorFactory $batchIteratorFactory;
20+
21+
public function __construct(
22+
FeedIndexMetadata $metadata,
23+
ResourceConnection $resourceConnection,
24+
BatchIteratorFactory $batchIteratorFactory
25+
) {
26+
$this->metadata = $metadata;
27+
$this->resourceConnection = $resourceConnection;
28+
$this->batchIteratorFactory = $batchIteratorFactory;
29+
}
30+
31+
public function countOrders(): int
32+
{
33+
$tableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
34+
$connection = $this->resourceConnection->getConnection();
35+
36+
$select = $connection->select()
37+
->from(
38+
['order' => $tableName],
39+
['count' => 'COUNT(*)']
40+
);
41+
$row = $connection->fetchRow($select);
42+
return intval($row['count']);
43+
}
44+
45+
public function fetchOrders(): Generator
46+
{
47+
$tableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
48+
$connection = $this->resourceConnection->getConnection();
49+
50+
$select = $connection->select()
51+
->from(
52+
['order' => $tableName],
53+
['id', 'feed_data']
54+
);
55+
56+
$iterator = $this->batchIteratorFactory->create(
57+
[
58+
'select' => $select,
59+
'batchSize' => $this->metadata->getBatchSize(),
60+
'correlationName' => 'order',
61+
'rangeField' => 'id',
62+
'rangeFieldAlias' => 'id',
63+
]
64+
);
65+
66+
foreach ($iterator as $batchSelect) {
67+
yield $connection->fetchAll($batchSelect);
68+
}
69+
}
70+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesOrdersDataExporter\Model;
9+
10+
use DateTime;
11+
use DateTimeInterface;
12+
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
13+
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
14+
use Magento\SalesOrdersDataExporter\Console\Command\Link;
15+
use Magento\SalesOrdersDataExporter\Model\Indexer\DateTimeRangeOrderProcessor;
16+
17+
class OnDemandOrdersExporter
18+
{
19+
private CommerceDataExportLoggerInterface $logger;
20+
private FeedIndexMetadata $metadata;
21+
private DateTimeRangeOrderProcessor $processor;
22+
private Link $linkCommand;
23+
24+
public function __construct(
25+
CommerceDataExportLoggerInterface $logger,
26+
FeedIndexMetadata $metadata,
27+
DateTimeRangeOrderProcessor $processor,
28+
Link $linkCommand
29+
) {
30+
$this->logger = $logger;
31+
$this->metadata = $metadata;
32+
$this->processor = $processor;
33+
$this->linkCommand = $linkCommand;
34+
}
35+
36+
public function export(DateTime $from, DateTime $to): void
37+
{
38+
$this->linkCommand->assignUuidsToOrderEntities(
39+
$this->metadata->getBatchSize(),
40+
$from->format(DateTimeInterface::W3C),
41+
$to->format(DateTimeInterface::W3C)
42+
);
43+
$this->processor->fullReindex($this->metadata, $from, $to);
44+
// TODO: would be interesting here to return the amount of orders indexed but impl needs further thinking
45+
}
46+
47+
}

SalesOrdersDataExporter/etc/di.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@
6969
</arguments>
7070
</type>
7171

72+
<type name="Magento\SalesOrdersDataExporter\Model\OnDemandOrdersExporter">
73+
<arguments>
74+
<argument name="metadata" xsi:type="object">Magento\SalesOrdersDataExporter\Model\Indexer\OnDemandOrdersFeedIndexMetadata</argument>
75+
</arguments>
76+
</type>
77+
78+
<type name="Magento\SalesOrdersDataExporter\Model\OnDemandExportOrdersRepository">
79+
<arguments>
80+
<argument name="metadata" xsi:type="object">Magento\SalesOrdersDataExporter\Model\Indexer\OnDemandOrdersFeedIndexMetadata</argument>
81+
</arguments>
82+
</type>
83+
7284
<!-- Providers -->
7385
<virtualType name="Magento\SalesOrdersDataExporter\Model\Provider\OrderItems" type="Magento\DataExporter\Model\Provider\QueryDataProvider">
7486
<arguments>

0 commit comments

Comments
 (0)