Skip to content

Commit

Permalink
support ranking query and order by
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Aug 10, 2023
1 parent 490dd81 commit 7a26519
Showing 1 changed file with 66 additions and 5 deletions.
71 changes: 66 additions & 5 deletions core/ArchiveProcessor/LogAggregationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
use Piwik\Metrics;
use Piwik\Plugin\ArchivedMetric;
use Piwik\Plugin\LogTablesProvider;
use Piwik\RankingQuery;
use Piwik\Tracker\GoalManager;

Check failure on line 26 in core/ArchiveProcessor/LogAggregationQuery.php

View workflow job for this annotation

GitHub Actions / PHPCS

Type Piwik\Tracker\GoalManager is not used in this file.

/**
* TODO
*
* TODO: rankingquery support
*
* TODO: member docs
*/
class LogAggregationQuery
Expand Down Expand Up @@ -77,13 +76,33 @@ class LogAggregationQuery
*/
private $groupBy = [];

/**
* @var string|null
*/
private $orderBySelect = null;

/**
* @var string|null
*/
private $orderByDirection = null;

/**
* TODO
*
* @var callable
*/
private $rowProcessor;

/**
* @var bool|number
*/
private $rankingQueryLimit = false;

/**
* @var array
*/
private $rankingQueryAggregations = [];

public function __construct(string $table, LogAggregator $logAggregator)
{
$this->table = $table;
Expand Down Expand Up @@ -370,6 +389,17 @@ public function getMetricFields(): array
return $this->metrics;
}

/**
* TODO
* @return $this
*/
public function useRankingQuery($rankingQueryLimit, array $metricAggregations = [])
{
$this->rankingQueryLimit = $rankingQueryLimit;
$this->rankingQueryAggregations = $metricAggregations;
return $this;
}

/**
* TODO
* @return string[]
Expand Down Expand Up @@ -442,8 +472,6 @@ public function buildQuery(): array
$bind = array_merge($bind, $selectInfo['bind']);
}

// TODO: support order by as well

$from = $this->from;

$where = [];
Expand All @@ -457,20 +485,53 @@ public function buildQuery(): array

$groupBy = implode(', ', $this->groupBy);

$orderBy = !empty($this->orderBySelect) ? ($this->orderBySelect . ' ' . $this->orderByDirection) : null;

$query = $this->logAggregator->generateQuery(
implode(",\n ", $selects),
$from,
implode(' AND ', $where),
$groupBy,
$orderBy = false
$orderBy
);

// TODO: timeLimitInMs support

if ($this->rankingQueryLimit) {
$rankingQuery = new RankingQuery($this->rankingQueryLimit);
foreach ($this->dimensions as $dimensionSelectAs) {
if (!array_key_exists($dimensionSelectAs, $this->rankingQueryAggregations)) {
$rankingQuery->addLabelColumn($dimensionSelectAs);
}
}

foreach ($this->rankingQueryAggregations as $metric => $aggregation) {
$rankingQuery->addColumn($metric, $aggregation);
}

$query['sql'] = $rankingQuery->generateRankingQuery($query['sql']);
}

// TODO: bind only works up til where statement. adding placeholders after bind won't work.
$query['bind'] = array_merge($bind, $query['bind']);

return $query;
}

/**
* TODO
*
* @param string $select
* @param string $direction
* @return $this
*/
public function orderBy(string $select, string $direction = 'desc')
{
$this->orderBySelect = $select;
$this->orderByDirection = $direction;
return $this;
}

private function getDimensionSelectSql(Dimension $dimension): string
{
$sql = $dimension->getSqlFilter();
Expand Down

0 comments on commit 7a26519

Please sign in to comment.