Skip to content

Commit

Permalink
Range filters should be improved to work mostly client side (#2827)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgbaybay authored Dec 19, 2024
1 parent 9d2fa41 commit 1bec7bb
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 337 deletions.
52 changes: 4 additions & 48 deletions lib/Report/ApiRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,57 +174,13 @@ public function getResults(SanitizerInterface $sanitizedParams)
throw new AccessDeniedException();
}

$currentDate = Carbon::now()->startOfDay();

//
// From and To Date Selection
// --------------------------
// Our report has a range filter which determines whether the user has to enter their own from / to dates
// check the range filter first and set from/to dates accordingly.
$reportFilter = $sanitizedParams->getString('reportFilter');

// Use the current date as a helper
$now = Carbon::now();

switch ($reportFilter) {
// the monthly data starts from yesterday
case 'yesterday':
$fromDt = $now->copy()->startOfDay()->subDay();
$toDt = $now->copy()->startOfDay();
break;

case 'lastweek':
$fromDt = $now->copy()->locale(Translate::GetLocale())->startOfWeek()->subWeek();
$toDt = $fromDt->copy()->addWeek();
break;

case 'lastmonth':
$fromDt = $now->copy()->startOfMonth()->subMonth();
$toDt = $fromDt->copy()->addMonth();
break;

case 'lastyear':
$fromDt = $now->copy()->startOfYear()->subYear();
$toDt = $fromDt->copy()->addYear();
break;

case '':
default:
// Expect dates to be provided.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

$fromDt = $fromDt->startOfDay();

// If toDt is current date then make it current datetime
if ($toDt->format('Y-m-d') == $currentDate->format('Y-m-d')) {
$toDt = Carbon::now();
} else {
$toDt = $toDt->addDay()->startOfDay();
}

break;
}
// The report uses a custom range filter that automatically calculates the from/to dates
// depending on the date range selected.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

$type = $sanitizedParams->getString('type');

Expand Down
52 changes: 4 additions & 48 deletions lib/Report/DisplayAlerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,57 +164,13 @@ public function getResults(SanitizerInterface $sanitizedParams)
$displayIds = $this->getDisplayIdFilter($sanitizedParams);
$onlyLoggedIn = $sanitizedParams->getCheckbox('onlyLoggedIn') == 1;

$currentDate = Carbon::now()->startOfDay();

//
// From and To Date Selection
// --------------------------
// Our report has a range filter which determines whether the user has to enter their own from / to dates
// check the range filter first and set from/to dates accordingly.
$reportFilter = $sanitizedParams->getString('reportFilter');

// Use the current date as a helper
$now = Carbon::now();

switch ($reportFilter) {
// the monthly data starts from yesterday
case 'yesterday':
$fromDt = $now->copy()->startOfDay()->subDay();
$toDt = $now->copy()->startOfDay();
break;

case 'lastweek':
$fromDt = $now->copy()->locale(Translate::GetLocale())->startOfWeek()->subWeek();
$toDt = $fromDt->copy()->addWeek();
break;

case 'lastmonth':
$fromDt = $now->copy()->startOfMonth()->subMonth();
$toDt = $fromDt->copy()->addMonth();
break;

case 'lastyear':
$fromDt = $now->copy()->startOfYear()->subYear();
$toDt = $fromDt->copy()->addYear();
break;

case '':
default:
// Expect dates to be provided.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

$fromDt = $fromDt->startOfDay();

// If toDt is current date then make it current datetime
if ($toDt->format('Y-m-d') == $currentDate->format('Y-m-d')) {
$toDt = Carbon::now();
} else {
$toDt = $toDt->addDay()->startOfDay();
}

break;
}
// The report uses a custom range filter that automatically calculates the from/to dates
// depending on the date range selected.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

$metadata = [
'periodStart' => Carbon::createFromTimestamp($fromDt->toDateTime()->format('U'))
Expand Down
50 changes: 4 additions & 46 deletions lib/Report/SessionHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,52 +173,10 @@ public function getResults(SanitizerInterface $sanitizedParams)
//
// From and To Date Selection
// --------------------------
// Our report has a range filter which determines whether the user has to enter their own from / to dates
// check the range filter first and set from/to dates accordingly.
$reportFilter = $sanitizedParams->getString('reportFilter');

// Use the current date as a helper
$now = Carbon::now();

switch ($reportFilter) {
// the monthly data starts from yesterday
case 'yesterday':
$fromDt = $now->copy()->startOfDay()->subDay();
$toDt = $now->copy()->startOfDay();
break;

case 'lastweek':
$fromDt = $now->copy()->locale(Translate::GetLocale())->startOfWeek()->subWeek();
$toDt = $fromDt->copy()->addWeek();
break;

case 'lastmonth':
$fromDt = $now->copy()->startOfMonth()->subMonth();
$toDt = $fromDt->copy()->addMonth();
break;

case 'lastyear':
$fromDt = $now->copy()->startOfYear()->subYear();
$toDt = $fromDt->copy()->addYear();
break;

case '':
default:
// Expect dates to be provided.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

$fromDt = $fromDt->startOfDay();

// If toDt is current date then make it current datetime
if ($toDt->format('Y-m-d') == $currentDate->format('Y-m-d')) {
$toDt = Carbon::now();
} else {
$toDt = $toDt->addDay()->startOfDay();
}

break;
}
// The report uses a custom range filter that automatically calculates the from/to dates
// depending on the date range selected.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

$metadata = [
'periodStart' => Carbon::createFromTimestamp($fromDt->toDateTime()->format('U'))
Expand Down
73 changes: 4 additions & 69 deletions lib/Report/TimeConnected.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,75 +216,10 @@ public function getResults(SanitizerInterface $sanitizedParams)

// From and To Date Selection
// --------------------------
// Our report has a range filter which determines whether the user has to enter their own from / to dates
// check the range filter first and set from/to dates accordingly.
$reportFilter = $sanitizedParams->getString('reportFilter');
// Use the current date as a helper
$now = Carbon::now();

switch ($reportFilter) {
case 'today':
$fromDt = $now->copy()->startOfDay();
$toDt = $fromDt->copy()->addDay();
break;

case 'yesterday':
$fromDt = $now->copy()->startOfDay()->subDay();
$toDt = $now->copy()->startOfDay();
break;

case 'thisweek':
$fromDt = $now->copy()->locale(Translate::GetLocale())->startOfWeek();
$toDt = $fromDt->copy()->addWeek();
break;

case 'thismonth':
$fromDt = $now->copy()->startOfMonth();
$toDt = $fromDt->copy()->addMonth();
break;

case 'thisyear':
$fromDt = $now->copy()->startOfYear();
$toDt = $fromDt->copy()->addYear();
break;

case 'lastweek':
$fromDt = $now->copy()->locale(Translate::GetLocale())->startOfWeek()->subWeek();
$toDt = $fromDt->copy()->addWeek();
break;

case 'lastmonth':
$fromDt = $now->copy()->startOfMonth()->subMonth();
$toDt = $fromDt->copy()->addMonth();
break;

case 'lastyear':
$fromDt = $now->copy()->startOfYear()->subYear();
$toDt = $fromDt->copy()->addYear();
break;

case '':
default:
// Expect dates to be provided.
$fromDt = $sanitizedParams->getDate('statsFromDt', ['default' => Carbon::now()->subDay()]);
$fromDt->startOfDay();

$toDt = $sanitizedParams->getDate('statsToDt', ['default' => Carbon::now()]);
$toDt->addDay()->startOfDay();

// What if the fromdt and todt are exactly the same?
// in this case assume an entire day from midnight on the fromdt to midnight on the todt (i.e. add a day to the todt)
if ($fromDt == $toDt) {
$toDt->addDay();
}

// No need to execute the query if fromdt/todt range is not correct
if ($fromDt > $toDt) {
return [];
}

break;
}
// The report uses a custom range filter that automatically calculates the from/to dates
// depending on the date range selected.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

// Use the group by filter provided
// NB: this differs from the Summary Report where we set the group by according to the range selected
Expand Down
50 changes: 4 additions & 46 deletions lib/Report/TimeDisconnectedSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,52 +205,10 @@ public function getResults(SanitizerInterface $sanitizedParams)
//
// From and To Date Selection
// --------------------------
// Our report has a range filter which determins whether or not the user has to enter their own from / to dates
// check the range filter first and set from/to dates accordingly.
$reportFilter = $sanitizedParams->getString('reportFilter');

// Use the current date as a helper
$now = Carbon::now();

switch ($reportFilter) {
// the monthly data starts from yesterday
case 'yesterday':
$fromDt = $now->copy()->startOfDay()->subDay();
$toDt = $now->copy()->startOfDay();
break;

case 'lastweek':
$fromDt = $now->copy()->locale(Translate::GetLocale())->startOfWeek()->subWeek();
$toDt = $fromDt->copy()->addWeek();
break;

case 'lastmonth':
$fromDt = $now->copy()->startOfMonth()->subMonth();
$toDt = $fromDt->copy()->addMonth();
break;

case 'lastyear':
$fromDt = $now->copy()->startOfYear()->subYear();
$toDt = $fromDt->copy()->addYear();
break;

case '':
default:
// Expect dates to be provided.
$fromDt = $sanitizedParams->getDate('fromDt', ['default' => $sanitizedParams->getDate('availabilityFromDt')]);
$toDt = $sanitizedParams->getDate('toDt', ['default' => $sanitizedParams->getDate('availabilityToDt')]);

$fromDt = $fromDt->startOfDay();

// If toDt is current date then make it current datetime
if ($toDt->format('Y-m-d') == $currentDate->format('Y-m-d')) {
$toDt = Carbon::now();
} else {
$toDt = $toDt->addDay()->startOfDay();
}

break;
}
// The report uses a custom range filter that automatically calculates the from/to dates
// depending on the date range selected.
$fromDt = $sanitizedParams->getDate('fromDt');
$toDt = $sanitizedParams->getDate('toDt');

// Get an array of display groups this user has access to
$displayGroupIds = [];
Expand Down
7 changes: 2 additions & 5 deletions reports/apirequests-report-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
<div class="XiboFilterCustom">
<div class="FilterDiv card-body" id="apiRequestsHistoryFilter">
<form class="form-inline">
{% set title %}{% trans "From Date" %}{% endset %}
{{ inline.date("fromDt", title, defaults.fromDate, "", "", "", "") }}

{% set title %}{% trans "To Date" %}{% endset %}
{{ inline.date("toDt", title, defaults.toDate, "", "", "", "") }}
{% set title %}{% trans "Range" %}{% endset %}
{{ inline.dateRangeFilter("reportFilter", title, "", "", "", "", "") }}

{% set title %}{% trans "User" %}{% endset %}
{% set attributes = [
Expand Down
9 changes: 3 additions & 6 deletions reports/displayalerts-report-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
<div class="XiboFilterCustom">
<div class="FilterDiv card-body" id="displayAlertsFilter">
<form class="form-inline">
{% set title %}{% trans "From Date" %}{% endset %}
{{ inline.date("fromDt", title, defaults.fromDate, "", "", "", "") }}

{% set title %}{% trans "To Date" %}{% endset %}
{{ inline.date("toDt", title, defaults.toDate, "", "", "", "") }}
{% set title %}{% trans "Range" %}{% endset %}
{{ inline.dateRangeFilter("reportFilter", title, "", "", "", "", "") }}

{% set title %}{% trans "Display" %}{% endset %}
{% set attributes = [
Expand Down Expand Up @@ -166,6 +163,7 @@
let $report = $("#displayAlertsFilter");
let $dataTable = $('#displayAlertsGrid'); // Datatable
let result; // XHR get data result
let reportData = '';
let imageLoader = $("#imageLoader");
let $warning = $("#applyWarning");
Expand Down Expand Up @@ -299,7 +297,6 @@
});
function displayAlertsReportScheduleFormOpen(dialog) {
// If we select a displayId we hide the display group filter
$('#reportScheduleAddForm #displayId').off('change').change( function() {
Expand Down
7 changes: 2 additions & 5 deletions reports/sessionhistory-report-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
<div class="XiboFilterCustom">
<div class="FilterDiv card-body" id="sessionHistoryFilter">
<form class="form-inline">
{% set title %}{% trans "From Date" %}{% endset %}
{{ inline.date("fromDt", title, defaults.fromDate, "", "", "", "") }}

{% set title %}{% trans "To Date" %}{% endset %}
{{ inline.date("toDt", title, defaults.toDate, "", "", "", "") }}
{% set title %}{% trans "Range" %}{% endset %}
{{ inline.dateRangeFilter("reportFilter", title, "", "", "", "", "") }}

{% set title %}{% trans "User" %}{% endset %}
{% set attributes = [
Expand Down
Loading

0 comments on commit 1bec7bb

Please sign in to comment.