Skip to content

Commit

Permalink
[statistics] fix: add missing last period to custom date ranges (#3661)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
davwheat authored Nov 6, 2022
1 parent 5733a8e commit 6e1a944
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions js/src/admin/components/StatisticsWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/Api/Controller/ShowStatisticsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,15 @@ private function getTimedStatistics(string $model)

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)) {
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/api/CanRequestCustomTimedStatisticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ public function can_request_timed_stats()
'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,
]
];

Expand Down

0 comments on commit 6e1a944

Please sign in to comment.