Skip to content

Commit

Permalink
Reports: Add MySql data grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
mgbaybay committed Aug 28, 2024
1 parent a9cb342 commit 32d1107
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 21 deletions.
88 changes: 84 additions & 4 deletions lib/Report/ProofOfPlay.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ public function getResults(SanitizerInterface $sanitizedParams)
$exactTags = $sanitizedParams->getCheckbox('exactTags');
$operator = $sanitizedParams->getString('logicalOperator', ['default' => 'OR']);
$parentCampaignId = $sanitizedParams->getInt('parentCampaignId');
$groupBy = $sanitizedParams->getString('groupBy');
$displayGroupIds = $sanitizedParams->getIntArray('displayGroupId', ['default' => []]);

// Display filter.
try {
Expand Down Expand Up @@ -472,7 +474,9 @@ public function getResults(SanitizerInterface $sanitizedParams)
$tags,
$tagsType,
$exactTags,
$operator
$operator,
$groupBy,
$displayGroupIds
);
}

Expand Down Expand Up @@ -505,6 +509,8 @@ public function getResults(SanitizerInterface $sanitizedParams)
$entry['minStart'] = Carbon::createFromTimestamp($row['minStart'])->format(DateFormatHelper::getSystemFormat());
$entry['maxEnd'] = Carbon::createFromTimestamp($row['maxEnd'])->format(DateFormatHelper::getSystemFormat());
$entry['mediaId'] = $sanitizedRow->getInt('mediaId');
$entry['displayGroup'] = $sanitizedRow->getString('displayGroup');
$entry['displayGroupId'] = $sanitizedRow->getInt('displayGroupId');

$rows[] = $entry;
}
Expand Down Expand Up @@ -541,6 +547,8 @@ public function getResults(SanitizerInterface $sanitizedParams)
* @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]
*/
private function getProofOfPlayReportMySql(
Expand All @@ -555,7 +563,9 @@ private function getProofOfPlayReportMySql(
$tags,
$tagsType,
$exactTags,
$logicalOperator
$logicalOperator,
$groupBy,
$displayGroupIds
) {
$fromDt = $fromDt->format('U');
$toDt = $toDt->format('U');
Expand Down Expand Up @@ -585,6 +595,11 @@ private function getProofOfPlayReportMySql(
stat.displayId
';


if ($groupBy === 'displayGroup') {
$select .= ', displaydg.displayGroup, displaydg.displayGroupId ';
}

$body = '
FROM stat
LEFT OUTER JOIN display
Expand Down Expand Up @@ -615,6 +630,15 @@ private function getProofOfPlayReportMySql(
}
}

// Grouping Option
if ($groupBy === 'displayGroup') {
$body .= 'INNER JOIN `lkdisplaydg` AS linkdg
ON linkdg.DisplayID = display.displayid
INNER JOIN `displaygroup` AS displaydg
ON displaydg.displaygroupId = linkdg.displaygroupId
AND `displaydg`.isDisplaySpecific = 0 ';
}

$body .= ' WHERE stat.type <> \'displaydown\'
AND stat.end > :fromDt
AND stat.start < :toDt
Expand Down Expand Up @@ -816,6 +840,11 @@ private function getProofOfPlayReportMySql(
stat.displayId
';

// Group By displayGroup Filter
if ($groupBy === 'displayGroup') {
$body .= ', displaydg.displayGroupId';
}

$order = '';
if ($columns != null) {
$order = 'ORDER BY ' . implode(',', $columns);
Expand Down Expand Up @@ -843,10 +872,17 @@ private function getProofOfPlayReportMySql(
$entry['widgetId'] = $row['widgetId'];
$entry['mediaId'] = $row['mediaId'];
$entry['tag'] = $row['tag'];

$entry['displayGroupId'] = $row['displayGroupId'] ?? '';
$entry['displayGroup'] = $row['displayGroup'] ?? '';
$rows[] = $entry;
}

if ($groupBy != '') {
$filterKey = $groupBy === 'tag' ? 'tag' : 'displayGroupId';

$rows = $this->getByDisplayGroup($rows, $filterKey, $displayGroupIds);
}

return [
'periodStart' => Carbon::createFromTimestamp($fromDt)->format(DateFormatHelper::getSystemFormat()),
'periodEnd' => Carbon::createFromTimestamp($toDt)->format(DateFormatHelper::getSystemFormat()),
Expand Down Expand Up @@ -901,6 +937,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 @@ -917,7 +955,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 @@ -1157,4 +1197,44 @@ private function getProofOfPlayReportMongoDb(
'count' => count($rows)
];
}

/**
* Get the accumulated value by display groups
* @param array $rows
* @param string $type
* @param array $displayIds
* @return array
*/
private function getByDisplayGroup(array $rows, string $type, array $displayIds = []) : array
{
$data = [];
$groups = [];

// Get the accumulated values by displayGroupId
foreach ($rows as $row) {
$groupId = $row[$type];

if (isset($groups[$groupId])) {
$groups[$groupId]['duration'] += $row['duration'];
$groups[$groupId]['numberPlays'] += $row['numberPlays'];
$groups[$groupId]['count'] += 1;
} else if ($row[$type] != '') {
$row['count'] = 1;
$groups[$groupId] = $row;
}
}

if ($type === 'tag') {
return $groups;
}

// Get all display groups or selected display groups only
foreach ($groups as $group) {
if (!$displayIds || in_array($group['displayGroupId'], $displayIds)) {
$data[] = $group;
}
}

return $data;
}
}
48 changes: 31 additions & 17 deletions reports/proofofplay-report-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@
<th>{% trans "Display" %}</th>
<th>{% trans "Display Group ID" %}</th>
<th>{% trans "Display Group" %}</th>
<th>{% trans "Tag ID" %}</th>
<th>{% trans "Campaign" %}</th>
<th>{% trans "Layout ID" %}</th>
<th>{% trans "Layout" %}</th>
Expand Down Expand Up @@ -264,7 +263,6 @@
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
Expand Down Expand Up @@ -308,16 +306,16 @@
// Hide and show appropriate columns
switch ($('select[name="groupBy"]').val()) {
case 'displayGroup':
$(this.api().columns([1, 2, 5, 6, 7, 8, 9, 10, 11]).visible(false));
$(this.api().columns([1, 2, 5, 6, 7, 8, 9, 10, 14, 15]).visible(false));
$(this.api().columns([3, 4]).visible(true));
break;
case 'tag':
$(this.api().columns([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).visible(false));
$(this.api().columns([5]).visible(true));
$(this.api().columns([1, 2, 3, 4, 5, 6, 7, 8, 9, 14, 15]).visible(false));
$(this.api().columns([10]).visible(true));
break;
default:
$(this.api().columns([3, 4, 5]).visible(false));
$(this.api().columns([1, 2, 5, 6, 7, 8, 9, 10, 11]).visible(true));
$(this.api().columns([3, 4]).visible(false));
$(this.api().columns([1, 2, 5, 6, 7, 8, 9, 10, 14, 15]).visible(true));
}
},
filter: false,
Expand All @@ -329,7 +327,6 @@
{"data": "display"},
{"data": "displayGroupId"},
{"data": "displayGroup"},
{"data": "tagId"},
{"data": "parentCampaign"},
{"data": "layoutId"},
{"data": "layout"},
Expand Down Expand Up @@ -373,28 +370,27 @@
{"data": "duration"},
{"data": "minStart"},
{"data": "maxEnd"}
],
footerCallback: function (row, data, start, end, display) {
let api = this.api();
// Total over all pages
let totalNumberPlays = api.column(9).data().reduce(function (a, b) {
let totalNumberPlays = api.column(11).data().reduce(function (a, b) {
return a + b;
}, 0);
let totalDuration = api.column(11).data().reduce(function (a, b) {
let totalDuration = api.column(13).data().reduce(function (a, b) {
return a + b;
}, 0);
let totalNumberPlaysPage = api.column(9, { page: 'current'}).data().reduce(function (a, b) {
let totalNumberPlaysPage = api.column(11, { page: 'current'}).data().reduce(function (a, b) {
return a + b;
}, 0);
let totalDurationPage = api.column(11, { page: 'current'}).data().reduce(function (a, b) {
let totalDurationPage = api.column(13, { page: 'current'}).data().reduce(function (a, b) {
return a + b;
}, 0);
// Update footer
$(api.column(9).footer()).html(totalNumberPlaysPage + ' (' + totalNumberPlays + ' total)');
$(api.column(11).footer()).html(Math.floor(totalDurationPage) + ' (' + Math.floor(totalDuration) + ' total)');
$(api.column(11).footer()).html(totalNumberPlaysPage + ' (' + totalNumberPlays + ' total)');
$(api.column(13).footer()).html(Math.floor(totalDurationPage) + ' (' + Math.floor(totalDuration) + ' total)');
},
});
Expand Down Expand Up @@ -474,10 +470,10 @@
getData($dataTable.data().url);
});
// If we select a displayId we hide the display group filter
// If we select a displayId, we hide the display group filter
$('#displayId').off('change').change( function() {
let displayId = $('#displayId').val();
if (displayId) {
$('select[name="displayGroupId[]"] option').remove();
$('select[name="displayGroupId[]"]').next(".select2-container").parent().hide();
Expand All @@ -490,6 +486,24 @@
}
});
// If we select a group by filter, we hide the display filter
$("select[name='groupBy']").on('change', function() {
let optionSelected = $(this).find("option:selected").val();
if (optionSelected === 'tag') {
$('select[name="displayGroupId[]"] option').remove();
$('select[name="displayGroupId[]"]').next(".select2-container").parent().hide();
} else if (optionSelected === 'displayGroup') {
$('select[name="groupBy"]').parent().show();
}
if (optionSelected !== '') {
$("select[name='displayId']").parent().hide();
} else {
$("select[name='displayId']").parent().show();
}
});
// Hide / Show FromDt and ToDt
function checkReportFilter(reportFilter) {
if (reportFilter.val() === '' || reportFilter.val() === undefined) {
Expand Down

0 comments on commit 32d1107

Please sign in to comment.