ReportNewRelicCron.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Model\Cron;
  7. use Magento\NewRelicReporting\Model\Config;
  8. use Magento\NewRelicReporting\Model\Module\Collect;
  9. use Magento\NewRelicReporting\Model\Counter;
  10. use Magento\NewRelicReporting\Model\CronEventFactory;
  11. use Magento\NewRelicReporting\Model\Apm\DeploymentsFactory;
  12. /**
  13. * Class ReportNewRelicCron
  14. */
  15. class ReportNewRelicCron
  16. {
  17. /**
  18. * @var Config
  19. */
  20. protected $config;
  21. /**
  22. * @var Collect
  23. */
  24. protected $collect;
  25. /**
  26. * @var Counter
  27. */
  28. protected $counter;
  29. /**
  30. * @var CronEventFactory
  31. */
  32. protected $cronEventFactory;
  33. /**
  34. * @var DeploymentsFactory
  35. */
  36. protected $deploymentsFactory;
  37. /**
  38. * Parameters to be sent to Insights
  39. * @var array
  40. */
  41. protected $customParameters = [];
  42. /**
  43. * Constructor
  44. *
  45. * @param Config $config
  46. * @param Collect $collect
  47. * @param Counter $counter
  48. * @param CronEventFactory $cronEventFactory
  49. * @param DeploymentsFactory $deploymentsFactory
  50. */
  51. public function __construct(
  52. Config $config,
  53. Collect $collect,
  54. Counter $counter,
  55. CronEventFactory $cronEventFactory,
  56. DeploymentsFactory $deploymentsFactory
  57. ) {
  58. $this->config = $config;
  59. $this->collect = $collect;
  60. $this->counter = $counter;
  61. $this->cronEventFactory = $cronEventFactory;
  62. $this->deploymentsFactory = $deploymentsFactory;
  63. }
  64. /**
  65. * Queue up custom parameters to send in API call to Insights Events
  66. *
  67. * @param array $data
  68. * @return void
  69. */
  70. public function addCustomParameters(array $data)
  71. {
  72. foreach ($data as $key => $value) {
  73. $this->customParameters[$key] = $value;
  74. }
  75. }
  76. /**
  77. * Reports current total module counts to Insights
  78. *
  79. * @return void
  80. */
  81. protected function reportModules()
  82. {
  83. $moduleData = $this->collect->getModuleData(false);
  84. $moduleDataChanges = $moduleData['changes'];
  85. if (count($moduleDataChanges) > 0) {
  86. $enabledChangeArray = [];
  87. $disabledChangeArray = [];
  88. $installedChangeArray = [];
  89. $uninstalledChangeArray = [];
  90. foreach ($moduleDataChanges as $change) {
  91. switch ($change['type']) {
  92. case Config::ENABLED:
  93. $enabledChangeArray[] = $change['name'] . '-' . $change['setup_version'];
  94. break;
  95. case Config::DISABLED:
  96. $disabledChangeArray[] = $change['name'] . '-' . $change['setup_version'];
  97. break;
  98. case Config::INSTALLED:
  99. $installedChangeArray[] = $change['name'] . '-' . $change['setup_version'];
  100. break;
  101. case Config::UNINSTALLED:
  102. $uninstalledChangeArray[] = $change['name'] . '-' . $change['setup_version'];
  103. break;
  104. }
  105. }
  106. $this->setModuleChangeStatusDeployment($enabledChangeArray, 'Modules Enabled');
  107. $this->setModuleChangeStatusDeployment($disabledChangeArray, 'Modules Disabled');
  108. $this->setModuleChangeStatusDeployment($installedChangeArray, 'Modules Installed');
  109. $this->setModuleChangeStatusDeployment($uninstalledChangeArray, 'Modules Uninstalled');
  110. }
  111. $this->addCustomParameters([Config::MODULES_ENABLED => $moduleData[Config::ENABLED]]);
  112. $this->addCustomParameters([Config::MODULES_DISABLED => $moduleData[Config::DISABLED]]);
  113. $this->addCustomParameters([Config::MODULES_INSTALLED => $moduleData[Config::INSTALLED]]);
  114. }
  115. /**
  116. * Reports current module change status via deployment marker
  117. *
  118. * @param array $changesArray
  119. * @param string $deploymentText
  120. * @return void
  121. */
  122. protected function setModuleChangeStatusDeployment(array $changesArray, $deploymentText = '')
  123. {
  124. if (count($changesArray) > 0) {
  125. foreach ($changesArray as $change) {
  126. $this->deploymentsFactory->create()->setDeployment(
  127. $deploymentText,
  128. $change,
  129. 'cron'
  130. );
  131. }
  132. }
  133. }
  134. /**
  135. * Reports counts info to New Relic
  136. *
  137. * @return void
  138. * @throws \Exception
  139. */
  140. protected function reportCounts()
  141. {
  142. $this->addCustomParameters([
  143. Config::PRODUCT_COUNT => $this->counter->getAllProductsCount(),
  144. Config::CONFIGURABLE_COUNT => $this->counter->getConfigurableCount(),
  145. Config::ACTIVE_COUNT => $this->counter->getActiveCatalogSize(),
  146. Config::CATEGORY_COUNT => $this->counter->getCategoryCount(),
  147. Config::WEBSITE_COUNT => $this->counter->getWebsiteCount(),
  148. Config::STORE_VIEW_COUNT => $this->counter->getStoreViewsCount(),
  149. Config::CUSTOMER_COUNT => $this->counter->getCustomerCount(),
  150. ]);
  151. if (!empty($this->customParameters)) {
  152. $this->cronEventFactory->create()
  153. ->addData($this->customParameters)
  154. ->sendRequest();
  155. }
  156. }
  157. /**
  158. * Reports info to New Relic by Cron
  159. *
  160. * @return \Magento\NewRelicReporting\Model\Cron\ReportCounts
  161. */
  162. public function report()
  163. {
  164. if ($this->config->isNewRelicEnabled()) {
  165. $this->reportCounts();
  166. }
  167. return $this;
  168. }
  169. }