Spaces:
No application file
No application file
File size: 5,454 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
<?php
declare(strict_types=1);
namespace Mautic\CampaignBundle\Model;
use Mautic\CampaignBundle\Entity\LeadEventLog;
use Mautic\CampaignBundle\Entity\LeadEventLogRepository;
use Mautic\CampaignBundle\Entity\Summary;
use Mautic\CampaignBundle\Entity\SummaryRepository;
use Mautic\CoreBundle\Helper\ProgressBarHelper;
use Mautic\CoreBundle\Model\AbstractCommonModel;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @extends AbstractCommonModel<Summary>
*/
class SummaryModel extends AbstractCommonModel
{
private array $logData = [];
/**
* Collapse Event Log entities into insert/update queries for the campaign summary.
*
* @throws \Doctrine\DBAL\Exception
*/
public function updateSummary(iterable $logs): void
{
$now = new \DateTime();
/** @var LeadEventLog $log */
foreach ($logs as $log) {
if (!$log->getDateTriggered()) {
// This shouldn't normally happen but it's possible to have a log without a date triggered
// as it is a nullable field and it can be created without date triggered for example via API.
continue;
}
$timestamp = $log->getDateTriggered()->getTimestamp();
$timestamp -= ($timestamp % 3600);
$dateFrom = $now->setTimestamp($timestamp);
$dateTo = (clone $dateFrom)->modify('+1 hour -1 second');
$campaign = $log->getCampaign();
$event = $log->getEvent();
$key = $campaign->getId().'.'.$event->getId().'.'.$timestamp;
$this->logData[$key] = [
'campaignId' => $campaign->getId(),
'eventId' => $event->getId(),
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
];
}
if (count($this->logData) >= 100) {
$this->persistSummaries();
}
}
public function getRepository(): SummaryRepository
{
return $this->em->getRepository(Summary::class);
}
public function getPermissionBase(): string
{
return 'campaign:campaigns';
}
/**
* Summarize all of history.
*
* @throws \Doctrine\DBAL\Exception
*/
public function summarize(OutputInterface $output, int $hoursPerBatch = 1, int $maxHours = null, bool $rebuild = false): void
{
$start = null;
if (!$rebuild) {
$start = $this->getRepository()->getOldestTriggeredDate();
}
// Start with the current hour.
$start ??= new \DateTime('+1 hour');
$start->setTimestamp($start->getTimestamp() - ($start->getTimestamp() % 3600));
$end = $this->getCampaignLeadEventLogRepository()->getOldestTriggeredDate();
if (!$end) {
$output->writeln('There are no records in the campaign lead event log table. Nothing to summarize.');
return;
}
$end = $end->setTimestamp($end->getTimestamp() - ($end->getTimestamp() % 3600));
$startedAt = new \DateTime();
$output->writeln('<comment>Started at: '.$startedAt->format('Y-m-d H:i:s').'</comment>');
if ($end <= $start) {
$hours = ($end->diff($start)->days * 24) + $end->diff($start)->h;
if ($maxHours && $hours > $maxHours) {
$end = clone $start;
$end = $end->sub(new \DateInterval('PT'.$maxHours.'H'));
}
$progressBar = ProgressBarHelper::init($output, $hours);
$progressBar->start();
$interval = new \DateInterval('PT'.$hoursPerBatch.'H');
$dateFrom = clone $start;
$dateTo = (clone $start)->modify('-1 second');
do {
$dateFrom = $dateFrom->sub($interval);
$dateFromFormatted = $dateFrom->format('Y-m-d H:i:s');
$dateToFormatted = $dateTo->format('Y-m-d H:i:s');
$output->write("\t".$dateFromFormatted.' - '.$dateToFormatted);
$this->getRepository()->summarize($dateFrom, $dateTo);
$progressBar->advance($hoursPerBatch);
$dateTo = $dateTo->sub($interval);
} while ($end < $dateFrom);
$progressBar->finish();
$output->writeln("\n".'<info>Updating summary for log counts processed</info>');
}
$this->outputProcessTime($startedAt, $output);
}
public function getCampaignLeadEventLogRepository(): LeadEventLogRepository
{
return $this->em->getRepository(LeadEventLog::class);
}
/**
* @throws \Doctrine\DBAL\Exception
*/
public function persistSummaries(): void
{
foreach ($this->logData as $log) {
$dateFrom = $log['dateFrom'];
$dateTo = $log['dateTo'];
$campaignId = $log['campaignId'];
$eventId = $log['eventId'];
$this->getRepository()->summarize($dateFrom, $dateTo, $campaignId, $eventId);
}
}
private function outputProcessTime(\DateTime $startedAt, OutputInterface $output): void
{
$endedAt = new \DateTime();
$output->writeln("\n".'<comment>Ended at: '.$endedAt->format('Y-m-d H:i:s').'</comment>');
$completedInterval = $startedAt->diff($endedAt);
$output->writeln('<info>Summary completed in: '.$completedInterval->format('%H:%I:%S').'</info>');
}
}
|