BulkStatus.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Model;
  7. use Magento\AsynchronousOperations\Api\Data\OperationInterface;
  8. use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\AsynchronousOperations\Model\BulkStatus\CalculatedStatusSql;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. /**
  13. * Class BulkStatus
  14. */
  15. class BulkStatus implements \Magento\Framework\Bulk\BulkStatusInterface
  16. {
  17. /**
  18. * @var \Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory
  19. */
  20. private $bulkCollectionFactory;
  21. /**
  22. * @var \Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory
  23. */
  24. private $operationCollectionFactory;
  25. /**
  26. * @var ResourceConnection
  27. */
  28. private $resourceConnection;
  29. /**
  30. * @var CalculatedStatusSql
  31. */
  32. private $calculatedStatusSql;
  33. /**
  34. * @var MetadataPool
  35. */
  36. private $metadataPool;
  37. /**
  38. * BulkStatus constructor.
  39. * @param ResourceModel\Bulk\CollectionFactory $bulkCollection
  40. * @param ResourceModel\Operation\CollectionFactory $operationCollection
  41. * @param ResourceConnection $resourceConnection
  42. * @param CalculatedStatusSql $calculatedStatusSql
  43. * @param MetadataPool $metadataPool
  44. */
  45. public function __construct(
  46. \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory $bulkCollection,
  47. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory $operationCollection,
  48. ResourceConnection $resourceConnection,
  49. CalculatedStatusSql $calculatedStatusSql,
  50. MetadataPool $metadataPool
  51. ) {
  52. $this->operationCollectionFactory = $operationCollection;
  53. $this->bulkCollectionFactory = $bulkCollection;
  54. $this->resourceConnection = $resourceConnection;
  55. $this->calculatedStatusSql = $calculatedStatusSql;
  56. $this->metadataPool = $metadataPool;
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. public function getFailedOperationsByBulkId($bulkUuid, $failureType = null)
  62. {
  63. $failureCodes = $failureType
  64. ? [$failureType]
  65. : [
  66. OperationInterface::STATUS_TYPE_RETRIABLY_FAILED,
  67. OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED
  68. ];
  69. $operations = $this->operationCollectionFactory->create()
  70. ->addFieldToFilter('bulk_uuid', $bulkUuid)
  71. ->addFieldToFilter('status', $failureCodes)
  72. ->getItems();
  73. return $operations;
  74. }
  75. /**
  76. * @inheritDoc
  77. */
  78. public function getOperationsCountByBulkIdAndStatus($bulkUuid, $status)
  79. {
  80. if ($status === OperationInterface::STATUS_TYPE_OPEN) {
  81. /**
  82. * Total number of operations that has been scheduled within the given bulk
  83. */
  84. $allOperationsQty = $this->getOperationCount($bulkUuid);
  85. /**
  86. * Number of operations that has been processed (i.e. operations with any status but 'open')
  87. */
  88. $allProcessedOperationsQty = (int)$this->operationCollectionFactory->create()
  89. ->addFieldToFilter('bulk_uuid', $bulkUuid)
  90. ->getSize();
  91. return $allOperationsQty - $allProcessedOperationsQty;
  92. }
  93. /** @var \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection $collection */
  94. $collection = $this->operationCollectionFactory->create();
  95. return $collection->addFieldToFilter('bulk_uuid', $bulkUuid)
  96. ->addFieldToFilter('status', $status)
  97. ->getSize();
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function getBulksByUser($userId)
  103. {
  104. /** @var ResourceModel\Bulk\Collection $collection */
  105. $collection = $this->bulkCollectionFactory->create();
  106. $operationTableName = $this->resourceConnection->getTableName('magento_operation');
  107. $statusesArray = [
  108. OperationInterface::STATUS_TYPE_RETRIABLY_FAILED,
  109. OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED,
  110. BulkSummaryInterface::NOT_STARTED,
  111. OperationInterface::STATUS_TYPE_OPEN,
  112. OperationInterface::STATUS_TYPE_COMPLETE
  113. ];
  114. $select = $collection->getSelect();
  115. $select->columns(['status' => $this->calculatedStatusSql->get($operationTableName)])
  116. ->order(new \Zend_Db_Expr('FIELD(status, ' . implode(',', $statusesArray) . ')'));
  117. $collection->addFieldToFilter('user_id', $userId)
  118. ->addOrder('start_time');
  119. return $collection->getItems();
  120. }
  121. /**
  122. * @inheritDoc
  123. */
  124. public function getBulkStatus($bulkUuid)
  125. {
  126. /**
  127. * Number of operations that has been processed (i.e. operations with any status but 'open')
  128. */
  129. $allProcessedOperationsQty = (int)$this->operationCollectionFactory->create()
  130. ->addFieldToFilter('bulk_uuid', $bulkUuid)
  131. ->getSize();
  132. if ($allProcessedOperationsQty == 0) {
  133. return BulkSummaryInterface::NOT_STARTED;
  134. }
  135. /**
  136. * Total number of operations that has been scheduled within the given bulk
  137. */
  138. $allOperationsQty = $this->getOperationCount($bulkUuid);
  139. /**
  140. * Number of operations that has not been started yet (i.e. operations with status 'open')
  141. */
  142. $allOpenOperationsQty = $allOperationsQty - $allProcessedOperationsQty;
  143. /**
  144. * Number of operations that has been completed successfully
  145. */
  146. $allCompleteOperationsQty = $this->operationCollectionFactory->create()
  147. ->addFieldToFilter('bulk_uuid', $bulkUuid)->addFieldToFilter(
  148. 'status',
  149. OperationInterface::STATUS_TYPE_COMPLETE
  150. )->getSize();
  151. if ($allCompleteOperationsQty == $allOperationsQty) {
  152. return BulkSummaryInterface::FINISHED_SUCCESSFULLY;
  153. }
  154. if ($allOpenOperationsQty > 0 && $allOpenOperationsQty !== $allOperationsQty) {
  155. return BulkSummaryInterface::IN_PROGRESS;
  156. }
  157. return BulkSummaryInterface::FINISHED_WITH_FAILURE;
  158. }
  159. /**
  160. * Get total number of operations that has been scheduled within the given bulk.
  161. *
  162. * @param string $bulkUuid
  163. * @return int
  164. */
  165. private function getOperationCount($bulkUuid)
  166. {
  167. $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
  168. $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
  169. return (int)$connection->fetchOne(
  170. $connection->select()
  171. ->from($metadata->getEntityTable(), 'operation_count')
  172. ->where('uuid = ?', $bulkUuid)
  173. );
  174. }
  175. }