mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Analytics
/
Controller
/
StepStatisticController.php
AutomationTimeSpanController.php
2 months ago
OverviewStatisticsController.php
2 months ago
StepStatisticController.php
2 months ago
index.php
3 years ago
StepStatisticController.php
98 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Controller; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\AutomationRun; |
| 10 | use MailPoet\Automation\Engine\Data\AutomationRunLog; |
| 11 | use MailPoet\Automation\Engine\Data\Step; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationRunLogStorage; |
| 13 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 14 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\Query; |
| 15 | |
| 16 | class StepStatisticController { |
| 17 | |
| 18 | /** @var AutomationRunStorage */ |
| 19 | private $automationRunStorage; |
| 20 | |
| 21 | /** @var AutomationRunLogStorage */ |
| 22 | private $automationRunLogStorage; |
| 23 | |
| 24 | public function __construct( |
| 25 | AutomationRunStorage $automationRunStorage, |
| 26 | AutomationRunLogStorage $automationRunLogStorage |
| 27 | ) { |
| 28 | $this->automationRunStorage = $automationRunStorage; |
| 29 | $this->automationRunLogStorage = $automationRunLogStorage; |
| 30 | } |
| 31 | |
| 32 | public function getWaitingStatistics(Automation $automation, Query $query): array { |
| 33 | $rawData = $this->automationRunStorage->getAutomationStepStatisticForTimeFrame( |
| 34 | $automation->getId(), |
| 35 | AutomationRun::STATUS_RUNNING, |
| 36 | $query->getAfter(), |
| 37 | $query->getBefore(), |
| 38 | $query->getVersionId() |
| 39 | ); |
| 40 | |
| 41 | $data = []; |
| 42 | foreach ($automation->getSteps() as $step) { |
| 43 | foreach ($rawData as $rawDatum) { |
| 44 | if ($rawDatum['next_step_id'] === $step->getId()) { |
| 45 | $data[$step->getId()] = (int)$rawDatum['count']; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return $data; |
| 50 | } |
| 51 | |
| 52 | public function getFailedStatistics(Automation $automation, Query $query): array { |
| 53 | $rawData = $this->automationRunStorage->getAutomationStepStatisticForTimeFrame( |
| 54 | $automation->getId(), |
| 55 | AutomationRun::STATUS_FAILED, |
| 56 | $query->getAfter(), |
| 57 | $query->getBefore(), |
| 58 | $query->getVersionId() |
| 59 | ); |
| 60 | |
| 61 | $data = []; |
| 62 | foreach ($automation->getSteps() as $step) { |
| 63 | foreach ($rawData as $rawDatum) { |
| 64 | if ($rawDatum['next_step_id'] === $step->getId()) { |
| 65 | $data[$step->getId()] = (int)$rawDatum['count']; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return $data; |
| 70 | } |
| 71 | |
| 72 | public function getCompletedStatistics(Automation $automation, Query $query): array { |
| 73 | $statistics = $this->automationRunLogStorage->getAutomationRunStatisticsForAutomationInTimeFrame( |
| 74 | $automation->getId(), |
| 75 | AutomationRunLog::STATUS_COMPLETE, |
| 76 | $query->getAfter(), |
| 77 | $query->getBefore(), |
| 78 | $query->getVersionId() |
| 79 | ); |
| 80 | |
| 81 | $data = []; |
| 82 | |
| 83 | foreach ($automation->getSteps() as $step) { |
| 84 | if ($step->getType() === Step::TYPE_ROOT) { |
| 85 | continue; |
| 86 | } |
| 87 | $data[$step->getId()] = 0; |
| 88 | foreach ($statistics as $stat) { |
| 89 | if ($stat['step_id'] === $step->getId()) { |
| 90 | $data[$step->getId()] = (int)$stat['count']; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return $data; |
| 96 | } |
| 97 | } |
| 98 |