mirror of
https://github.com/DanielSiepmann/tracking.git
synced 2024-11-21 13:36:09 +01:00
Fix performance issue of PageviewsPerDay widget
Issue a single query with native group by instead of x queries. Resolves: #63
This commit is contained in:
parent
9624847b9a
commit
67c2d22f15
2 changed files with 31 additions and 15 deletions
|
@ -101,21 +101,26 @@ class PageviewsPerDay implements ChartDataProviderInterface
|
|||
$data = [];
|
||||
|
||||
for ($daysBefore = $this->days; $daysBefore >= 0; $daysBefore--) {
|
||||
$timeForLabel = (int) strtotime('-' . $daysBefore . ' day');
|
||||
$startPeriod = (int) strtotime('-' . $daysBefore . ' day 0:00:00');
|
||||
$endPeriod = (int) strtotime('-' . $daysBefore . ' day 23:59:59');
|
||||
$label = date($this->dateFormat, (int) strtotime('-' . $daysBefore . ' day'));
|
||||
$labels[$label] = $label;
|
||||
$data[$label] = 0;
|
||||
}
|
||||
|
||||
$labels[] = date($this->dateFormat, $timeForLabel);
|
||||
$data[] = $this->getPageviewsInPeriod($startPeriod, $endPeriod);
|
||||
$start = (int) strtotime('-' . $this->days . ' day 0:00:00');
|
||||
$end = (int) strtotime('tomorrow midnight');
|
||||
|
||||
|
||||
foreach ($this->getPageviewsInPeriod($start, $end) as $day) {
|
||||
$data[$day['label']] = (int) $day['count'];
|
||||
}
|
||||
|
||||
return [
|
||||
$labels,
|
||||
$data,
|
||||
array_values($labels),
|
||||
array_values($data),
|
||||
];
|
||||
}
|
||||
|
||||
private function getPageviewsInPeriod(int $start, int $end): int
|
||||
private function getPageviewsInPeriod(int $start, int $end): array
|
||||
{
|
||||
$constraints = [
|
||||
$this->queryBuilder->expr()->gte('crdate', $start),
|
||||
|
@ -142,11 +147,20 @@ class PageviewsPerDay implements ChartDataProviderInterface
|
|||
);
|
||||
}
|
||||
|
||||
return (int)$this->queryBuilder
|
||||
->count('*')
|
||||
$this->queryBuilder
|
||||
->addSelectLiteral('COUNT(*) as "count"')
|
||||
->from('tx_tracking_pageview')
|
||||
->where(...$constraints)
|
||||
->execute()
|
||||
->fetchColumn();
|
||||
->groupBy('label')
|
||||
->orderBy('label', 'ASC')
|
||||
;
|
||||
|
||||
if ($this->queryBuilder->getConnection()->getDatabasePlatform()->getName() === 'sqlite') {
|
||||
$this->queryBuilder->addSelectLiteral('date(crdate, "unixepoch") as "label"');
|
||||
} else {
|
||||
$this->queryBuilder->addSelectLiteral('FROM_UNIXTIME(crdate, "%Y-%m-%d") as "label"');
|
||||
}
|
||||
|
||||
return $this->queryBuilder->execute()->fetchAll();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,10 +25,12 @@ Fixes
|
|||
|
||||
This mainly improves the PageviewsPerPage widget.
|
||||
|
||||
PageviewsPerDay is still way to slow, but I couldn't find a working approach to
|
||||
improve performance.
|
||||
PageviewsPerDay is fixed by altering the whole query to fetch all data in a
|
||||
performant way.
|
||||
A single query with native group by date is issued, instead of a single query per
|
||||
day.
|
||||
|
||||
Relates: :issue:`63`.
|
||||
Resolves: :issue:`63`.
|
||||
|
||||
Tasks
|
||||
-----
|
||||
|
|
Loading…
Reference in a new issue