Skip to content

Commit

Permalink
FRW-8766 Order matrix performance improvement. (#11032)
Browse files Browse the repository at this point in the history
FRW-8766 Order matrix performance improvement.
  • Loading branch information
kraal-spryker authored Aug 29, 2024
1 parent ebe34fa commit 52bd9a8
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Spryker/Shared/Oms/Transfer/oms.transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,32 @@
<property name="idSalesOrder" type="int"/>
<property name="eventData" type="array" singular="eventData"/>
</transfer>

<transfer name="OrderMatrixCriteria" strict="true">
<property name="orderMatrixConditions" type="OrderMatrixConditions"/>
<property name="pagination" type="Pagination"/>
</transfer>

<transfer name="OrderMatrixConditions" strict="true">
<property name="processIds" type="int[]" singular="processId"/>
</transfer>

<transfer name="Pagination">
<property name="limit" type="int"/>
<property name="offset" type="int"/>
</transfer>

<transfer name="OrderMatrix" strict="true">
<property name="processName" type="string"/>
<property name="stateName" type="string"/>
<property name="itemsCount" type="int"/>
<property name="dateWindow" type="string"/>
<property name="idProcess" type="int"/>
<property name="idState" type="int"/>
</transfer>

<transfer name="OrderMatrixCollection" strict="true">
<property name="orderMatrices" type="OrderMatrix[]" singular="orderMatrix"/>
</transfer>

</transfers>
30 changes: 30 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Generated\Shared\Transfer\OmsCheckTimeoutsQueryCriteriaTransfer;
use Generated\Shared\Transfer\OmsStateCollectionTransfer;
use Generated\Shared\Transfer\OrderItemFilterTransfer;
use Generated\Shared\Transfer\OrderMatrixCollectionTransfer;
use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer;
use Generated\Shared\Transfer\OrderTransfer;
use Generated\Shared\Transfer\ReservationRequestTransfer;
use Generated\Shared\Transfer\ReservationResponseTransfer;
Expand Down Expand Up @@ -469,13 +471,41 @@ public function triggerEventForOneItem($eventId, $orderItem, array $logContext,
*
* @api
*
* @deprecated Use {@link \Spryker\Zed\Oms\Business\OmsFacadeInterface::getOrderMatrixCollection()} instead.
*
* @return array
*/
public function getOrderItemMatrix()
{
return $this->getFactory()->createUtilOrderItemMatrix()->getMatrix();
}

/**
* {@inheritDoc}
*
* @api
*
* @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer
*
* @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer
*/
public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer
{
return $this->getRepository()->getOrderMatrixCollection($orderMatrixCriteriaTransfer);
}

/**
* {@inheritDoc}
*
* @api
*
* @return array<int, string>
*/
public function getProcessNamesIndexedByIdOmsOrderProcess(): array
{
return $this->getRepository()->getProcessNamesIndexedByIdOmsOrderProcess();
}

/**
* {@inheritDoc}
*
Expand Down
26 changes: 26 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Generated\Shared\Transfer\OmsCheckTimeoutsQueryCriteriaTransfer;
use Generated\Shared\Transfer\OmsStateCollectionTransfer;
use Generated\Shared\Transfer\OrderItemFilterTransfer;
use Generated\Shared\Transfer\OrderMatrixCollectionTransfer;
use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer;
use Generated\Shared\Transfer\OrderTransfer;
use Generated\Shared\Transfer\ReservationRequestTransfer;
use Generated\Shared\Transfer\ReservationResponseTransfer;
Expand Down Expand Up @@ -454,10 +456,34 @@ public function triggerEventForOneItem($eventId, $orderItem, array $logContext,
*
* @api
*
* @deprecated Use {@link \Spryker\Zed\Oms\Business\OmsFacadeInterface::getOrderMatrixCollection()} instead.
*
* @return array
*/
public function getOrderItemMatrix();

/**
* Specification:
* - Retrieves a batch of matrix order items based on provided criteria filter.
*
* @api
*
* @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer
*
* @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer
*/
public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer;

/**
* Specification:
* - Retrieves all active processes from the database.
*
* @api
*
* @return array<int, string>
*/
public function getProcessNamesIndexedByIdOmsOrderProcess(): array;

/**
* Specification:
* - Reads all order manual event from persistence
Expand Down
3 changes: 3 additions & 0 deletions src/Spryker/Zed/Oms/Business/Util/OrderItemMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface;
use Spryker\Zed\Oms\Persistence\OmsRepositoryInterface;

/**
* @deprecated Will be removed without replacement.
*/
class OrderItemMatrix
{
/**
Expand Down
12 changes: 10 additions & 2 deletions src/Spryker/Zed/Oms/Persistence/OmsPersistenceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery;
use Spryker\Zed\Kernel\Persistence\AbstractPersistenceFactory;
use Spryker\Zed\Oms\OmsDependencyProvider;
use Spryker\Zed\Oms\Persistence\Propel\Indexer\ProcessIndexer;
use Spryker\Zed\Oms\Persistence\Propel\Indexer\ProcessIndexerInterface;
use Spryker\Zed\Oms\Persistence\Propel\Mapper\OmsMapper;
use Spryker\Zed\Oms\Persistence\Propel\Mapper\OrderItemMapper;
use Spryker\Zed\Oms\Persistence\Propel\Mapper\OrderItemMapperInterface;
Expand Down Expand Up @@ -128,8 +130,6 @@ public function getSalesQueryContainer()
}

/**
* @deprecated Use {@link \Spryker\Zed\Oms\Persistence\OmsPersistenceFactory::getSalesQueryContainer()} to get the required query instead.
*
* @return \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery
*/
public function getSalesOrderItemPropelQuery(): SpySalesOrderItemQuery
Expand All @@ -144,4 +144,12 @@ public function createOmsMapper(): OmsMapper
{
return new OmsMapper();
}

/**
* @return \Spryker\Zed\Oms\Persistence\Propel\Indexer\ProcessIndexerInterface
*/
public function createProcessIndexer(): ProcessIndexerInterface
{
return new ProcessIndexer();
}
}
4 changes: 4 additions & 0 deletions src/Spryker/Zed/Oms/Persistence/OmsQueryContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ public function querySalesOrderById($idOrder)
*
* @api
*
* @deprecated Will be removed without replacement.
*
* @param array<string> $activeProcesses
*
* @return \Orm\Zed\Oms\Persistence\SpyOmsOrderProcessQuery
Expand All @@ -576,6 +578,8 @@ public function queryActiveProcesses(array $activeProcesses)
*
* @api
*
* @deprecated Will be removed without replacement.
*
* @param array $orderItemStates
*
* @return \Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public function querySalesOrderById($idOrder);
*
* @api
*
* @deprecated Will be removed without replacement.
*
* @param array<string> $activeProcesses
*
* @return \Orm\Zed\Oms\Persistence\SpyOmsOrderProcessQuery
Expand All @@ -210,6 +212,8 @@ public function queryActiveProcesses(array $activeProcesses);
*
* @api
*
* @deprecated Will be removed without replacement.
*
* @param array $orderItemStates
*
* @return \Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery
Expand Down
158 changes: 158 additions & 0 deletions src/Spryker/Zed/Oms/Persistence/OmsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

namespace Spryker\Zed\Oms\Persistence;

use DateTime;
use Generated\Shared\Transfer\OmsProductReservationTransfer;
use Generated\Shared\Transfer\OrderItemFilterTransfer;
use Generated\Shared\Transfer\OrderMatrixCollectionTransfer;
use Generated\Shared\Transfer\OrderMatrixCriteriaTransfer;
use Generated\Shared\Transfer\ReservationRequestTransfer;
use Generated\Shared\Transfer\ReservationResponseTransfer;
use Generated\Shared\Transfer\SalesOrderItemStateAggregationTransfer;
Expand All @@ -27,12 +30,69 @@
*/
class OmsRepository extends AbstractRepository implements OmsRepositoryInterface
{
/**
* @var string
*/
protected const ITEMS_COUNT = 'itemsCount';

/**
* @var string
*/
protected const DATE_WINDOW = 'dateWindow';

/**
* @var string
*/
protected const COL_PRODUCT_RESERVATION_TOTAL_QUANTITY = 'productReservationTotalQuantity';

/**
* @var string
*/
protected const STATE_NAME = 'stateName';

/**
* @var string
*/
protected const PROCESS_NAME = 'processName';

/**
* @var string
*/
protected const DATE_CASE_EXPRESSION = "(CASE WHEN %s > '%s' THEN 'day' WHEN %s > '%s' THEN 'week' ELSE 'other' END)";

/**
* @var string
*/
protected const DATE_FORMAT = 'Y-m-d H:i:s';

/**
* @var string
*/
protected const FK_OMS_ORDER_ITEM_STATE = 'fk_oms_order_item_state';

/**
* @var string
*/
protected const FK_OMS_ORDER_PROCESS = 'fk_oms_order_process';

/**
* @var string
*/
protected const SUBQUERY_ALIAS = 'sub';

/**
* @var string
*/
protected const SUBQUERY_FIELD_PLACEHOLDER = '%s.%s';

/**
* @var string
*/
protected const COUNT_ALL_KEYWORD = 'COUNT(*)';

/**
* @deprecated Use {@link \Spryker\Zed\Oms\Persistence\OmsRepositoryInterface::getOrderMatrixCollection()} instead.
*
* @param array<int> $processIds
* @param array<int> $stateBlackList
*
Expand All @@ -49,6 +109,104 @@ public function getMatrixOrderItems(array $processIds, array $stateBlackList): a
->mapOrderItemMatrix($orderItemsMatrixResult->getArrayCopy());
}

/**
* @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer
*
* @return \Generated\Shared\Transfer\OrderMatrixCollectionTransfer
*/
public function getOrderMatrixCollection(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): OrderMatrixCollectionTransfer
{
$subQuery = $this->getOrderMatrixSubquery($orderMatrixCriteriaTransfer);

$query = $this->getFactory()
->getSalesOrderItemPropelQuery()
->addSelectQuery($subQuery, static::SUBQUERY_ALIAS)
->select([
sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_ITEM_STATE),
sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_PROCESS),
])
->withColumn(static::COUNT_ALL_KEYWORD, static::ITEMS_COUNT)
->withColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::DATE_WINDOW), static::DATE_WINDOW)
->withColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::PROCESS_NAME), static::PROCESS_NAME)
->withColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::STATE_NAME), static::STATE_NAME)
->groupBy([
sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_ITEM_STATE),
sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::FK_OMS_ORDER_PROCESS),
])
->addGroupByColumn(sprintf(static::SUBQUERY_FIELD_PLACEHOLDER, static::SUBQUERY_ALIAS, static::DATE_WINDOW));

$orderItemEntities = $query->find()->getArrayCopy();

return $this->getFactory()
->createOrderItemMapper()
->mapSalesOrderItemEntitiesToOrderMatrixCollectionTransfer($orderItemEntities, new OrderMatrixCollectionTransfer());
}

/**
* @return array<int, string>
*/
public function getProcessNamesIndexedByIdOmsOrderProcess(): array
{
$activeProcesses = $this->getFactory()
->getConfig()
->getActiveProcesses();
$query = $this->getFactory()
->createOmsOrderProcessQuery();

$processEntities = $query->filterByName($activeProcesses, Criteria::IN)
->find()
->getArrayCopy();

return $this->getFactory()
->createProcessIndexer()
->getProcessNamesIndexedByIdOmsOrderProcess($processEntities);
}

/**
* @param \Generated\Shared\Transfer\OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer
*
* @return \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery
*/
protected function getOrderMatrixSubquery(OrderMatrixCriteriaTransfer $orderMatrixCriteriaTransfer): SpySalesOrderItemQuery
{
/** @var \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery $subQuery */
$subQuery = $this->getFactory()
->getSalesOrderItemPropelQuery();

/** @var \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery $subQuery */
$subQuery = $subQuery->filterByFkOmsOrderProcess($orderMatrixCriteriaTransfer->getOrderMatrixConditions()->getProcessIds(), Criteria::IN)
->useStateQuery()
->withColumn(SpyOmsOrderItemStateTableMap::COL_NAME, static::STATE_NAME)
->endUse();

$subQuery->useProcessQuery()
->withColumn(SpyOmsOrderProcessTableMap::COL_NAME, static::PROCESS_NAME)
->endUse()
->withColumn(sprintf(
static::DATE_CASE_EXPRESSION,
SpySalesOrderItemTableMap::COL_LAST_STATE_CHANGE,
(new DateTime('-1 day'))->format(static::DATE_FORMAT),
SpySalesOrderItemTableMap::COL_LAST_STATE_CHANGE,
(new DateTime('-7 day'))->format(static::DATE_FORMAT),
), static::DATE_WINDOW)
->withColumn(SpySalesOrderItemTableMap::COL_FK_OMS_ORDER_PROCESS, static::FK_OMS_ORDER_PROCESS)
->withColumn(SpySalesOrderItemTableMap::COL_FK_OMS_ORDER_ITEM_STATE, static::FK_OMS_ORDER_ITEM_STATE);

if ($orderMatrixCriteriaTransfer->getPagination() && $orderMatrixCriteriaTransfer->getPagination()->getLimit()) {
$subQuery->limit($orderMatrixCriteriaTransfer->getPagination()->getLimit())
->offset($orderMatrixCriteriaTransfer->getPagination()->getOffset());
}
$stateBlackList = $this->getFactory()->getConfig()->getStateBlacklist();

if ($stateBlackList) {
$subQuery->useStateQuery()
->filterByName($stateBlackList, Criteria::NOT_IN)
->endUse();
}

return $subQuery;
}

/**
* @param array<string> $stateNames
* @param string $sku
Expand Down
Loading

0 comments on commit 52bd9a8

Please sign in to comment.