Spaces:
No application file
No application file
File size: 4,271 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 |
<?php
namespace Mautic\LeadBundle\Segment\Stat\ChartQuery;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Mautic\CoreBundle\Helper\ArrayHelper;
use Mautic\CoreBundle\Helper\Chart\ChartQuery;
use Mautic\LeadBundle\Entity\LeadEventLog;
use Mautic\LeadBundle\Segment\Exception\SegmentNotFoundException;
class SegmentContactsLineChartQuery extends ChartQuery
{
/**
* @var int
*/
private $segmentId;
private ?array $addedEventLogStats = null;
private ?array $removedEventLogStats = null;
/**
* @param string|null $unit
*
* @throws SegmentNotFoundException
*/
public function __construct(
Connection $connection,
\DateTime $dateFrom,
\DateTime $dateTo,
private array $filters = [],
$unit = null
) {
$this->connection = $connection;
$this->dateFrom = $dateFrom;
$this->dateTo = $dateTo;
$this->unit = $unit;
if (!isset($this->filters['leadlist_id']['value'])) {
throw new SegmentNotFoundException('Segment ID required');
}
$this->segmentId = $this->filters['leadlist_id']['value'];
parent::__construct($connection, $dateFrom, $dateTo, $unit);
}
public function setDateRange(\DateTimeInterface $dateFrom, \DateTimeInterface $dateTo): void
{
parent::setDateRange($dateFrom, $dateTo);
$this->init();
}
public function getTotalStats(int $total): array
{
$totalCountDateTo = $this->getTotalToDateRange($total);
// count array SUM and then reverse
// require start from end and substract added/removed logs
$sums = array_reverse(ArrayHelper::sub($this->getAddedEventLogStats(), $this->getRemovedEventLogStats()));
$totalSum = 0;
$totals = array_map(function ($sum) use ($totalCountDateTo, &$totalSum) {
$total = $totalCountDateTo - $totalSum;
$totalSum += $sum;
if ($total > -1) {
return $total;
} else {
return 0;
}
}, $sums);
return array_reverse($totals);
}
/**
* Return total of contact to date end of graph.
*/
private function getTotalToDateRange(int $total): int
{
$queryForTotal = clone $this;
// try figure out total count in dateTo
$queryForTotal->setDateRange($this->dateTo, new \DateTime());
return $total - array_sum(ArrayHelper::sub($queryForTotal->getAddedEventLogStats(), $queryForTotal->getRemovedEventLogStats()));
}
/**
* Get data about add/remove from segment based on LeadEventLog.
*
* @param string $action
*/
public function getDataFromLeadEventLog($action): array
{
$qb = $this->prepareTimeDataQuery(
'lead_event_log',
'date_added',
[
'object' => 'segment',
'bundle' => 'lead',
'action' => $action,
'object_id' => $this->segmentId,
]
);
$qb = $this->optimizeSearchInLeadEventLog($qb);
return $this->loadAndBuildTimeData($qb);
}
/**
* @return int
*/
public function getSegmentId()
{
return $this->segmentId;
}
/**
* @return array
*/
public function getAddedEventLogStats()
{
return $this->addedEventLogStats;
}
/**
* @return array
*/
public function getRemovedEventLogStats()
{
return $this->removedEventLogStats;
}
/**
* Init basic stats.
*/
private function init(): void
{
$this->addedEventLogStats = $this->getDataFromLeadEventLog('added');
$this->removedEventLogStats = $this->getDataFromLeadEventLog('removed');
}
private function optimizeSearchInLeadEventLog(QueryBuilder $qb): QueryBuilder
{
$fromPart = $qb->getQueryPart('from');
$fromPart[0]['alias'] = sprintf('%s USE INDEX (%s)', $fromPart[0]['alias'], MAUTIC_TABLE_PREFIX.LeadEventLog::INDEX_SEARCH);
$qb->resetQueryPart('from');
$qb->from($fromPart[0]['table'], $fromPart[0]['alias']);
return $qb;
}
}
|