Details.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Model\Operation;
  7. use Magento\Framework\Bulk\OperationInterface;
  8. use Magento\Framework\Bulk\BulkStatusInterface;
  9. class Details
  10. {
  11. /**
  12. * @var array
  13. */
  14. private $operationCache = [];
  15. /**
  16. * @var \Magento\Framework\Bulk\BulkStatusInterface
  17. */
  18. private $bulkStatus;
  19. /**
  20. * @var null
  21. */
  22. private $bulkUuid;
  23. /**
  24. * Map between status codes and human readable indexes
  25. *
  26. * @var array
  27. */
  28. private $statusMap = [
  29. OperationInterface::STATUS_TYPE_COMPLETE => 'operations_successful',
  30. OperationInterface::STATUS_TYPE_RETRIABLY_FAILED => 'failed_retriable',
  31. OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED => 'failed_not_retriable',
  32. OperationInterface::STATUS_TYPE_OPEN => 'open',
  33. OperationInterface::STATUS_TYPE_REJECTED => 'rejected',
  34. ];
  35. /**
  36. * Init dependencies.
  37. *
  38. * @param \Magento\Framework\Bulk\BulkStatusInterface $bulkStatus
  39. * @param null $bulkUuid
  40. */
  41. public function __construct(
  42. BulkStatusInterface $bulkStatus,
  43. $bulkUuid = null
  44. ) {
  45. $this->bulkStatus = $bulkStatus;
  46. $this->bulkUuid = $bulkUuid;
  47. }
  48. /**
  49. * Collect operations statistics for the bulk
  50. *
  51. * @param string $bulkUuid
  52. * @return array
  53. */
  54. public function getDetails($bulkUuid)
  55. {
  56. $details = [
  57. 'operations_total' => 0,
  58. 'operations_successful' => 0,
  59. 'operations_failed' => 0,
  60. 'failed_retriable' => 0,
  61. 'failed_not_retriable' => 0,
  62. 'rejected' => 0,
  63. ];
  64. if (array_key_exists($bulkUuid, $this->operationCache)) {
  65. return $this->operationCache[$bulkUuid];
  66. }
  67. foreach ($this->statusMap as $statusCode => $readableKey) {
  68. $details[$readableKey] = $this->bulkStatus->getOperationsCountByBulkIdAndStatus(
  69. $bulkUuid,
  70. $statusCode
  71. );
  72. }
  73. $details['operations_total'] = array_sum($details);
  74. $details['operations_failed'] = $details['failed_retriable'] + $details['failed_not_retriable'];
  75. $this->operationCache[$bulkUuid] = $details;
  76. return $details;
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. public function getOperationsTotal()
  82. {
  83. $this->getDetails($this->bulkUuid);
  84. return $this->operationCache[$this->bulkUuid]['operations_total'];
  85. }
  86. /**
  87. * @inheritDoc
  88. */
  89. public function getOpen()
  90. {
  91. $this->getDetails($this->bulkUuid);
  92. $statusKey = $this->statusMap[OperationInterface::STATUS_TYPE_OPEN];
  93. return $this->operationCache[$this->bulkUuid][$statusKey];
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function getOperationsSuccessful()
  99. {
  100. $this->getDetails($this->bulkUuid);
  101. $statusKey = $this->statusMap[OperationInterface::STATUS_TYPE_COMPLETE];
  102. return $this->operationCache[$this->bulkUuid][$statusKey];
  103. }
  104. /**
  105. * @inheritDoc
  106. */
  107. public function getTotalFailed()
  108. {
  109. $this->getDetails($this->bulkUuid);
  110. return $this->operationCache[$this->bulkUuid]['operations_failed'];
  111. }
  112. /**
  113. * @inheritDoc
  114. */
  115. public function getFailedNotRetriable()
  116. {
  117. $statusKey = $this->statusMap[OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED];
  118. return $this->operationCache[$this->bulkUuid][$statusKey];
  119. }
  120. /**
  121. * @inheritDoc
  122. */
  123. public function getFailedRetriable()
  124. {
  125. $this->getDetails($this->bulkUuid);
  126. $statusKey = $this->statusMap[OperationInterface::STATUS_TYPE_RETRIABLY_FAILED];
  127. return $this->operationCache[$this->bulkUuid][$statusKey];
  128. }
  129. /**
  130. * @inheritDoc
  131. */
  132. public function getRejected()
  133. {
  134. $this->getDetails($this->bulkUuid);
  135. $statusKey = $this->statusMap[OperationInterface::STATUS_TYPE_REJECTED];
  136. return $this->operationCache[$this->bulkUuid][$statusKey];
  137. }
  138. }