Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\LeadBundle\Segment\Stat;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Mautic\CampaignBundle\Model\CampaignModel;
|
||||
use Mautic\CoreBundle\Helper\CacheStorageHelper;
|
||||
|
||||
class SegmentCampaignShare
|
||||
{
|
||||
public function __construct(
|
||||
private CampaignModel $campaignModel,
|
||||
private CacheStorageHelper $cacheStorageHelper,
|
||||
private EntityManager $entityManager,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $segmentId
|
||||
* @param array $campaignIds
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCampaignsSegmentShare($segmentId, $campaignIds = [])
|
||||
{
|
||||
$campaigns = $this->campaignModel->getRepository()->getCampaignsSegmentShare($segmentId, $campaignIds);
|
||||
foreach ($campaigns as $campaign) {
|
||||
$this->cacheStorageHelper->set($this->getCachedKey($segmentId, $campaign['id']), $campaign['segmentCampaignShare']);
|
||||
}
|
||||
|
||||
return $campaigns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $segmentId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCampaignList($segmentId)
|
||||
{
|
||||
$q = $this->entityManager->getConnection()->createQueryBuilder();
|
||||
$q->select('c.id, c.name, null as share')
|
||||
->from(MAUTIC_TABLE_PREFIX.'campaigns', 'c')
|
||||
->where($this->campaignModel->getRepository()->getPublishedByDateExpression($q))
|
||||
->orderBy('c.id', 'DESC');
|
||||
|
||||
$campaigns = $q->executeQuery()->fetchAllAssociative();
|
||||
|
||||
foreach ($campaigns as &$campaign) {
|
||||
// just load from cache If exists
|
||||
if ($share = $this->cacheStorageHelper->get($this->getCachedKey($segmentId, $campaign['id']))) {
|
||||
$campaign['share'] = $share;
|
||||
}
|
||||
}
|
||||
|
||||
return $campaigns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $segmentId
|
||||
* @param int $campaignId
|
||||
*/
|
||||
private function getCachedKey($segmentId, $campaignId): string
|
||||
{
|
||||
return sprintf('%s|%s|%s|%s|%s', 'campaign', $campaignId, 'segment', $segmentId, 'share');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\LeadBundle\Segment\Stat;
|
||||
|
||||
use Mautic\LeadBundle\Model\ListModel;
|
||||
use Mautic\LeadBundle\Segment\Stat\ChartQuery\SegmentContactsLineChartQuery;
|
||||
|
||||
class SegmentChartQueryFactory
|
||||
{
|
||||
public function getContactsTotal(SegmentContactsLineChartQuery $query, ListModel $listModel): array
|
||||
{
|
||||
$total = $listModel->getRepository()->getLeadCount($query->getSegmentId());
|
||||
|
||||
return $query->getTotalStats($total);
|
||||
}
|
||||
|
||||
public function getContactsAdded(SegmentContactsLineChartQuery $query): array
|
||||
{
|
||||
return $query->getAddedEventLogStats();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getContactsRemoved(SegmentContactsLineChartQuery $query)
|
||||
{
|
||||
return $query->getRemovedEventLogStats();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\LeadBundle\Segment\Stat;
|
||||
|
||||
use Mautic\CampaignBundle\Model\CampaignModel;
|
||||
use Mautic\EmailBundle\Model\EmailModel;
|
||||
use Mautic\FormBundle\Model\ActionModel;
|
||||
use Mautic\LeadBundle\Model\ListModel;
|
||||
use Mautic\PointBundle\Model\TriggerEventModel;
|
||||
use Mautic\ReportBundle\Model\ReportModel;
|
||||
|
||||
class SegmentDependencies
|
||||
{
|
||||
public function __construct(
|
||||
private EmailModel $emailModel,
|
||||
private CampaignModel $campaignModel,
|
||||
private ActionModel $actionModel,
|
||||
private ListModel $listModel,
|
||||
private TriggerEventModel $triggerEventModel,
|
||||
private ReportModel $reportModel,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getChannelsIds($segmentId): array
|
||||
{
|
||||
$usage = [];
|
||||
$usage[] = [
|
||||
'label' => 'mautic.email.emails',
|
||||
'route' => 'mautic_email_index',
|
||||
'ids' => $this->emailModel->getEmailsIdsWithDependenciesOnSegment($segmentId),
|
||||
];
|
||||
|
||||
$usage[] = [
|
||||
'label' => 'mautic.campaign.campaigns',
|
||||
'route' => 'mautic_campaign_index',
|
||||
'ids' => $this->campaignModel->getCampaignIdsWithDependenciesOnSegment($segmentId),
|
||||
];
|
||||
|
||||
$usage[] = [
|
||||
'label' => 'mautic.lead.lead.lists',
|
||||
'route' => 'mautic_segment_index',
|
||||
'ids' => $this->listModel->getSegmentsWithDependenciesOnSegment($segmentId, 'id'),
|
||||
];
|
||||
|
||||
$usage[] = [
|
||||
'label' => 'mautic.report.reports',
|
||||
'route' => 'mautic_report_index',
|
||||
'ids' => $this->reportModel->getReportsIdsWithDependenciesOnSegment($segmentId),
|
||||
];
|
||||
|
||||
$usage[] = [
|
||||
'label' => 'mautic.form.forms',
|
||||
'route' => 'mautic_form_index',
|
||||
'ids' => $this->actionModel->getFormsIdsWithDependenciesOnSegment($segmentId),
|
||||
];
|
||||
|
||||
$usage[] = [
|
||||
'label' => 'mautic.point.trigger.header.index',
|
||||
'route' => 'mautic_pointtrigger_index',
|
||||
'ids' => $this->triggerEventModel->getReportIdsWithDependenciesOnSegment($segmentId),
|
||||
];
|
||||
|
||||
return $usage;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user