From 9964ddd731a7f44ebd58446c8107182cab17e2d7 Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Sun, 6 Nov 2022 18:21:28 +0000 Subject: [PATCH] [statistics] fix: add missing last period to custom date ranges (#3661) * fix: last node in previous data matches first node of current data * fix: add previous period support for custom periods * test: update to show previous period for custom range --- .../js/src/admin/components/StatisticsWidget.tsx | 4 ++-- .../statistics/src/Api/Controller/ShowStatisticsData.php | 9 ++++++++- .../api/CanRequestCustomTimedStatisticsTest.php | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/extensions/statistics/js/src/admin/components/StatisticsWidget.tsx b/extensions/statistics/js/src/admin/components/StatisticsWidget.tsx index 6fecf522c..e2b74ec6b 100644 --- a/extensions/statistics/js/src/admin/components/StatisticsWidget.tsx +++ b/extensions/statistics/js/src/admin/components/StatisticsWidget.tsx @@ -348,7 +348,7 @@ export default class StatisticsWidget extends DashboardWidget { } : this.periods![this.selectedPeriod!]; const periodLength = period.end - period.start; - const labels = []; + const labels: string[] = []; const thisPeriod = []; const lastPeriod = []; @@ -373,7 +373,7 @@ export default class StatisticsWidget extends DashboardWidget { labels.push(label); thisPeriod.push(this.getPeriodCount(this.selectedEntity, { start: i, end: i + period.step })); - lastPeriod.push(this.getPeriodCount(this.selectedEntity, { start: i - periodLength, end: i - periodLength + period.step })); + lastPeriod.push(this.getPeriodCount(this.selectedEntity, { start: i - periodLength, end: i - periodLength })); } if (thisPeriod.length === 0) { diff --git a/extensions/statistics/src/Api/Controller/ShowStatisticsData.php b/extensions/statistics/src/Api/Controller/ShowStatisticsData.php index 0040c80c5..d6125074e 100644 --- a/extensions/statistics/src/Api/Controller/ShowStatisticsData.php +++ b/extensions/statistics/src/Api/Controller/ShowStatisticsData.php @@ -126,8 +126,15 @@ class ShowStatisticsData implements RequestHandlerInterface private function getTimedCounts(Builder $query, string $column, ?DateTime $startDate = null, ?DateTime $endDate = null) { + $diff = $startDate && $endDate ? $startDate->diff($endDate) : null; + if (! isset($startDate)) { - $startDate = new DateTime('-365 days'); + // need -12 months and period before that + $startDate = new DateTime('-2 years'); + } else { + // If the start date is custom, we need to include an equal amount beforehand + // to show the data for the previous period. + $startDate = (new Carbon($startDate))->subtract($diff)->toDateTime(); } if (! isset($endDate)) { diff --git a/extensions/statistics/tests/integration/api/CanRequestCustomTimedStatisticsTest.php b/extensions/statistics/tests/integration/api/CanRequestCustomTimedStatisticsTest.php index e1fbd40d0..35c25e858 100644 --- a/extensions/statistics/tests/integration/api/CanRequestCustomTimedStatisticsTest.php +++ b/extensions/statistics/tests/integration/api/CanRequestCustomTimedStatisticsTest.php @@ -73,12 +73,15 @@ class CanRequestCustomTimedStatisticsTest extends TestCase 'users' => [ $timeStart->copy()->getTimestamp() => 1, $timeStart->copy()->subDays(1)->getTimestamp() => 1, + $timeStart->copy()->subDays(2)->getTimestamp() => 1, ], 'discussions' => [ $timeStart->copy()->getTimestamp() => 1, $timeStart->copy()->subDays(1)->getTimestamp() => 2, + $timeStart->copy()->subDays(2)->getTimestamp() => 1, ], 'posts' => [ $timeStart->copy()->getTimestamp() => 2, $timeStart->copy()->subDays(1)->getTimestamp() => 2, + $timeStart->copy()->subDays(2)->getTimestamp() => 1, ] ];