Query data from the correct automation versions
[MAILPOET-5433]
This commit is contained in:
@ -3,9 +3,10 @@
|
|||||||
namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Controller;
|
namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Controller;
|
||||||
|
|
||||||
use MailPoet\Automation\Engine\Data\Automation;
|
use MailPoet\Automation\Engine\Data\Automation;
|
||||||
use MailPoet\Automation\Engine\Data\Step;
|
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction;
|
use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare;
|
use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare;
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Entities\StatisticsClickEntity;
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
use MailPoet\Entities\StatisticsOpenEntity;
|
use MailPoet\Entities\StatisticsOpenEntity;
|
||||||
use MailPoet\Newsletter\NewslettersRepository;
|
use MailPoet\Newsletter\NewslettersRepository;
|
||||||
@ -23,18 +24,24 @@ class OverviewStatisticsController {
|
|||||||
/** @var NewsletterUrl */
|
/** @var NewsletterUrl */
|
||||||
private $newsletterUrl;
|
private $newsletterUrl;
|
||||||
|
|
||||||
|
/** @var AutomationStorage */
|
||||||
|
private $automationStorage;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
NewslettersRepository $newslettersRepository,
|
NewslettersRepository $newslettersRepository,
|
||||||
NewsletterStatisticsRepository $newsletterStatisticsRepository,
|
NewsletterStatisticsRepository $newsletterStatisticsRepository,
|
||||||
NewsletterUrl $newsletterUrl
|
NewsletterUrl $newsletterUrl,
|
||||||
|
AutomationStorage $automationStorage
|
||||||
) {
|
) {
|
||||||
$this->newslettersRepository = $newslettersRepository;
|
$this->newslettersRepository = $newslettersRepository;
|
||||||
$this->newsletterStatisticsRepository = $newsletterStatisticsRepository;
|
$this->newsletterStatisticsRepository = $newsletterStatisticsRepository;
|
||||||
$this->newsletterUrl = $newsletterUrl;
|
$this->newsletterUrl = $newsletterUrl;
|
||||||
|
$this->automationStorage = $automationStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStatisticsForAutomation(Automation $automation, QueryWithCompare $query): array {
|
public function getStatisticsForAutomation(Automation $automation, QueryWithCompare $query): array {
|
||||||
$emails = $this->getEmailsFromAutomation($automation);
|
$currentEmails = $this->getAutomationEmailsInTimeSpan($automation, $query->getAfter(), $query->getBefore());
|
||||||
|
$previousEmails = $this->getAutomationEmailsInTimeSpan($automation, $query->getCompareWithAfter(), $query->getCompareWithBefore());
|
||||||
$data = [
|
$data = [
|
||||||
'sent' => ['current' => 0, 'previous' => 0],
|
'sent' => ['current' => 0, 'previous' => 0],
|
||||||
'opened' => ['current' => 0, 'previous' => 0],
|
'opened' => ['current' => 0, 'previous' => 0],
|
||||||
@ -44,7 +51,7 @@ class OverviewStatisticsController {
|
|||||||
'revenue' => ['current' => 0, 'previous' => 0],
|
'revenue' => ['current' => 0, 'previous' => 0],
|
||||||
'emails' => [],
|
'emails' => [],
|
||||||
];
|
];
|
||||||
if (!$emails) {
|
if (!$currentEmails) {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +63,7 @@ class OverviewStatisticsController {
|
|||||||
];
|
];
|
||||||
|
|
||||||
$currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
$currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
||||||
$emails,
|
$currentEmails,
|
||||||
$query->getAfter(),
|
$query->getAfter(),
|
||||||
$query->getBefore(),
|
$query->getBefore(),
|
||||||
$requiredData
|
$requiredData
|
||||||
@ -82,13 +89,13 @@ class OverviewStatisticsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
$previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
|
||||||
$emails,
|
$previousEmails,
|
||||||
$query->getCompareWithAfter(),
|
$query->getCompareWithAfter(),
|
||||||
$query->getCompareWithBefore(),
|
$query->getCompareWithBefore(),
|
||||||
$requiredData
|
$requiredData
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($previousStatistics as $newsletterId => $statistic) {
|
foreach ($previousStatistics as $statistic) {
|
||||||
$data['sent']['previous'] += $statistic->getTotalSentCount();
|
$data['sent']['previous'] += $statistic->getTotalSentCount();
|
||||||
$data['opened']['previous'] += $statistic->getOpenCount();
|
$data['opened']['previous'] += $statistic->getOpenCount();
|
||||||
$data['clicked']['previous'] += $statistic->getClickCount();
|
$data['clicked']['previous'] += $statistic->getClickCount();
|
||||||
@ -104,27 +111,63 @@ class OverviewStatisticsController {
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getEmailsFromAutomation(Automation $automation): array {
|
private function getAutomationEmailsInTimeSpan(Automation $automation, \DateTimeImmutable $after, \DateTimeImmutable $before): array {
|
||||||
return array_filter(array_map(
|
$automationVersions = $this->automationStorage->getAutomationVersionDates($automation->getId());
|
||||||
function (Step $step) {
|
usort(
|
||||||
$emailId = $step->getArgs()['email_id'] ?? null;
|
$automationVersions,
|
||||||
if (!$emailId) {
|
function (array $a, array $b) {
|
||||||
return null;
|
return $a['created_at'] <=> $b['created_at'];
|
||||||
}
|
}
|
||||||
return $this->newslettersRepository->findOneById((int)$emailId);
|
);
|
||||||
},
|
|
||||||
|
// filter automations that were created before the after date
|
||||||
|
$versionIds = [];
|
||||||
|
foreach ($automationVersions as $automationVersion) {
|
||||||
|
if ($automationVersion['created_at'] > $before) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!$versionIds || $automationVersion['created_at'] < $after) {
|
||||||
|
$versionIds = [(int)$automationVersion['id']];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$versionIds[] = (int)$automationVersion['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$automations = $this->automationStorage->getAutomationWithDifferentVersions($versionIds);
|
||||||
|
return $this->getEmailsFromAutomations($automations);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Automation[] $automations
|
||||||
|
* @return NewsletterEntity[]
|
||||||
|
*/
|
||||||
|
private function getEmailsFromAutomations(array $automations): array {
|
||||||
|
$emailSteps = [];
|
||||||
|
foreach ($automations as $automation) {
|
||||||
|
$emailSteps = array_merge(
|
||||||
|
$emailSteps,
|
||||||
|
array_values(
|
||||||
|
array_filter(
|
||||||
|
$automation->getSteps(),
|
||||||
|
function($step) {
|
||||||
|
return $step->getKey() === SendEmailAction::KEY;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$emailIds = array_unique(
|
||||||
array_filter(
|
array_filter(
|
||||||
array_values($automation->getSteps()),
|
array_map(
|
||||||
function ($step) {
|
function($step) {
|
||||||
return in_array(
|
return $step->getArgs()['email_id'];
|
||||||
$step->getKey(),
|
},
|
||||||
[
|
$emailSteps
|
||||||
SendEmailAction::KEY,
|
)
|
||||||
],
|
|
||||||
true
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
));
|
);
|
||||||
|
|
||||||
|
return $this->newslettersRepository->findBy(['id' => $emailIds]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user