Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reports: Add Proof of Play MongoDB grouping #2748

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 139 additions & 2 deletions lib/Report/ProofOfPlay.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Xibo\Factory\MediaFactory;
use Xibo\Factory\ReportScheduleFactory;
use Xibo\Factory\DisplayGroupFactory;
use Xibo\Factory\TagFactory;
use Xibo\Helper\ApplicationState;
use Xibo\Helper\DateFormatHelper;
use Xibo\Helper\SanitizerService;
Expand Down Expand Up @@ -75,6 +76,11 @@ class ProofOfPlay implements ReportInterface
*/
private $displayGroupFactory;

/**
* @var TagFactory
*/
private $tagFactory;

/**
* @var SanitizerService
*/
Expand All @@ -101,6 +107,7 @@ public function setFactories(ContainerInterface $container)
$this->layoutFactory = $container->get('layoutFactory');
$this->reportScheduleFactory = $container->get('reportScheduleFactory');
$this->displayGroupFactory = $container->get('displayGroupFactory');
$this->tagFactory = $container->get('tagFactory');
$this->sanitizer = $container->get('sanitizerService');

return $this;
Expand Down Expand Up @@ -470,7 +477,9 @@ public function getResults(SanitizerInterface $sanitizedParams)
$tags,
$tagsType,
$exactTags,
$operator
$operator,
$groupBy,
$displayGroupIds
);
} else {
$result = $this->getProofOfPlayReportMySql(
Expand Down Expand Up @@ -952,6 +961,8 @@ private function getBodyForTagsType($tagsType, $exclude) :string
* @param $tags string
* @param $tagsType string
* @param $exactTags mixed
* @param $groupBy string
* @param $displayGroupIds array
* @return array[array result, date periodStart, date periodEnd, int count, int totalStats]
* @throws InvalidArgumentException
* @throws \Xibo\Support\Exception\GeneralException
Expand All @@ -968,7 +979,9 @@ private function getProofOfPlayReportMongoDb(
$tags,
$tagsType,
$exactTags,
$operator
$operator,
$groupBy,
$displayGroupIds
) {
$fromDt = new UTCDateTime($filterFromDt->format('U')*1000);
$toDt = new UTCDateTime($filterToDt->format('U')*1000);
Expand Down Expand Up @@ -1196,11 +1209,21 @@ private function getProofOfPlayReportMongoDb(
$entry['widgetId'] = $row['widgetId'];
$entry['mediaId'] = $row['mediaId'];
$entry['tag'] = $row['eventName'];
$entry['displayGroupId'] = '';
$entry['displayGroup'] = '';
$entry['tagId'] = '';
$entry['tagName'] = '';

$rows[] = $entry;
}
}

if ($groupBy === 'tag') {
$rows = $this->groupByTagMongoDb($rows, $tagsType);
} else if ($groupBy === 'displayGroup') {
$rows = $this->groupByDisplayGroupMongoDb($rows, $displayGroupIds);
}

return [
'periodStart' => $filterFromDt->format(DateFormatHelper::getSystemFormat()),
'periodEnd' => $filterToDt->format(DateFormatHelper::getSystemFormat()),
Expand Down Expand Up @@ -1228,4 +1251,118 @@ private function groupByTagType(string $tagType) : string
`lktagdisplaygroup` AS taglink ON taglink.displaygroupId = displaydg.displaygroupId',
};
}

/**
* Group by display group in MongoDB
* @param array $rows
* @param array $filteredDisplayGroupIds
* @return array
* @throws NotFoundException
*/
private function groupByDisplayGroupMongoDb(array $rows, array $filteredDisplayGroupIds) : array
{
$data = [];
$displayInfoArr = $this->displayGroupFactory->query();

// Get the display groups
foreach ($rows as $row) {
foreach ($displayInfoArr as $dg) {
// Do we have a filter?
if (!$filteredDisplayGroupIds || in_array($dg->displayGroupId, $filteredDisplayGroupIds)) {
// Create a temporary key to group by multiple columns at once
// and save memory instead of checking each column recursively
$key = $dg->displayGroupId . '_' . $row['layoutId'] . '_' . $row['mediaId'] . '_' .
$row['tag'] . '_' . $row['widgetId'] . '_' . $row['parentCampaignId'] . '_' . $row['type'];

if (!isset($data[$key])) {
// Since we already have the display group as the grouping option, we can remove the display info
$row['display'] = null;
$row['displayId'] = null;
$row['displayGroupId'] = $dg->displayGroupId;
$row['displayGroup'] = $dg->displayGroup;

$data[$key] = $row;
} else {
$data[$key]['duration'] += $row['duration'];
$data[$key]['numberPlays'] += $row['numberPlays'];
}
}
}
}

return $data;
}

/**
* Group by tag in MongoDB
* @param array $rows
* @param string $tagsType
* @return array
*/
private function groupByTagMongoDb(array $rows, string $tagsType) : array
{
$data = [];
$tags = $this->filterByTagType($tagsType);
$type = match ($tagsType) {
'media' => 'mediaId',
'layout' => 'layoutId',
'dg' => 'displayId',
};;

foreach ($rows as $row) {
foreach ($tags as $tag) {
if ($row[$type] == $tag['entityId']) {
// Create a temporary key to group by multiple columns at once
// and save memory instead of checking each column recursively
$key = $tag['tagId'] . '_' . $row['layoutId'] . '_' . $row['mediaId'] . '_' .
$row['tag'] . '_' . $row['widgetId'] . '_' . $row['parentCampaignId'] . '_' . $row['type'];

if (!isset($data[$key])) {
// Since we already have the tags as the grouping option, we can remove the display info
$row['display'] = null;
$row['displayId'] = null;
$row['tagName'] = $tag['tag'];
$row['tagId'] = $tag['tagId'];

$data[$key] = $row;
} else {
$data[$key]['duration'] += $row['duration'];
$data[$key]['numberPlays'] += $row['numberPlays'];
}
}
}
}

return $data;
}

/**
* @param string $tagsType
* @return array
*/
private function filterByTagType(string $tagsType): array
{
$tags = [];
$filter = match ($tagsType) {
'media' => 'Media',
'layout' => 'Layout',
'dg' => 'Display',
};

// Get the list of tags to get the tag type (ie media tag, layout tag, or display tag)
$tagInfoArr = $this->tagFactory->query();

foreach ($tagInfoArr as $tag) {
// What type of tags are we looking for?
foreach ($this->tagFactory->getAllLinks(null, ['tagId' => $tag->tagId]) as $filteredTag) {
if ($filteredTag['type'] == $filter) {
$filteredTag['tagId'] = $tag->tagId;
$filteredTag['tag'] = $tag->tag;
$tags[] = $filteredTag;
}
}
}

return $tags;
}
}
2 changes: 1 addition & 1 deletion reports/proofofplay-report-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
let totalNumberPlaysPage = api.column(13, { page: 'current'}).data().reduce(function (a, b) {
return a + b;
}, 0);
let totalDurationPage = api.column(13, { page: 'current'}).data().reduce(function (a, b) {
let totalDurationPage = api.column(15, { page: 'current'}).data().reduce(function (a, b) {
return a + b;
}, 0);

Expand Down
Loading