Skip to content

Commit

Permalink
refs #1816 code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Nov 20, 2013
1 parent 0ea27f7 commit 53825ab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,26 @@
use Piwik\Plugins\API\API;

/**
* This class is responsible for adding ratio columns.
* This class is responsible for setting the metadata property 'totals' on each dataTable if the report
* has a dimension. 'Totals' means it tries to calculate the total report value for each metric. For each
* the total number of visits, actions, ... for a given report / dataTable.
*
* @package Piwik
* @subpackage Piwik_API
*/
class Totals extends DataTableManipulator
class ReportTotalsCalculator extends DataTableManipulator
{
protected $roundPrecision = 1;

/**
* Array [readableMetric] => [summed value]
* Cached report metadata array.
* @var array
*/
private $totalValues = array();
private static $reportMetadata = array();

/**
* @param DataTable $table
* @return \Piwik\DataTable|\Piwik\DataTable\Map
*/
public function generate($table)
public function calculate($table)
{
return $this->manipulate($table);
}
Expand All @@ -54,33 +53,35 @@ public function generate($table)
*/
protected function manipulateDataTable($dataTable)
{
$report = $this->getCurrentReport();
$report = $this->findCurrentReport();

if (!empty($report) && empty($report['dimension'])) {
// we currently do not calculate the total value for reports having no dimension
return $dataTable;
}

$this->totalValues = array();
// Array [readableMetric] => [summed value]
$totalValues = array();

$firstLevelTable = $this->makeSureToWorkOnFirstLevelDataTable($dataTable);
$metricsToCalculate = Metrics::getMetricIdsToProcessReportTotal();
$parentTable = $this->getFirstLevelDataTable($dataTable);

foreach ($metricsToCalculate as $metricId) {
if (!$this->hasDataTableMetric($parentTable, $metricId)) {
if (!$this->hasDataTableMetric($firstLevelTable, $metricId)) {
continue;
}

foreach ($parentTable->getRows() as $row) {
$this->addColumnValueToTotal($row, $metricId);
foreach ($firstLevelTable->getRows() as $row) {
$totalValues = $this->sumColumnValueToTotal($row, $metricId, $totalValues);
}
}

$dataTable->setMetadata('totals', $this->totalValues);
$dataTable->setMetadata('totals', $totalValues);

return $dataTable;
}

protected function hasDataTableMetric(DataTable $dataTable, $metricId)
private function hasDataTableMetric(DataTable $dataTable, $metricId)
{
$firstRow = $dataTable->getFirstRow();

Expand All @@ -95,7 +96,7 @@ protected function hasDataTableMetric(DataTable $dataTable, $metricId)
return true;
}

protected function getColumn($row, $columnIdRaw)
private function getColumn($row, $columnIdRaw)
{
$columnIdReadable = Metrics::getReadableColumnName($columnIdRaw);

Expand All @@ -111,35 +112,13 @@ protected function getColumn($row, $columnIdRaw)
return false;
}

protected function getCurrentReport()
{
foreach ($this->getReportMetadata() as $report) {
if (!empty($report['actionToLoadSubTables'])
&& $this->apiMethod == $report['action']
&& $this->apiModule == $report['module']) {

return $report;
}
}

}

protected function getFirstLevelDataTable($table)
private function makeSureToWorkOnFirstLevelDataTable($table)
{
if (!array_key_exists('idSubtable', $this->request)) {
return $table;
}

$firstLevelReport = array();
foreach ($this->getReportMetadata() as $report) {
if (!empty($report['actionToLoadSubTables'])
&& $this->apiMethod == $report['actionToLoadSubTables']
&& $this->apiModule == $report['module']) {

$firstLevelReport = $report;
break;
}
}
$firstLevelReport = $this->findFirstLevelReport();

if (empty($firstLevelReport)) {
// it is not a subtable report
Expand Down Expand Up @@ -168,22 +147,24 @@ protected function getFirstLevelDataTable($table)
return $this->callApiAndReturnDataTable($module, $action, $request);
}

private function addColumnValueToTotal(Row $row, $metricId)
private function sumColumnValueToTotal(Row $row, $metricId, $totalValues)
{
$value = $this->getColumn($row, $metricId);

if (false === $value) {

return;
return $totalValues;
}

$metricName = Metrics::getReadableColumnName($metricId);

if (array_key_exists($metricName, $this->totalValues)) {
$this->totalValues[$metricName] += $value;
if (array_key_exists($metricName, $totalValues)) {
$totalValues[$metricName] += $value;
} else {
$this->totalValues[$metricName] = $value;
$totalValues[$metricName] = $value;
}

return $totalValues;
}

/**
Expand Down Expand Up @@ -217,4 +198,32 @@ private function getReportMetadata()

return static::$reportMetadata;
}

private function findCurrentReport()
{
foreach ($this->getReportMetadata() as $report) {
if ($this->apiMethod == $report['action']
&& $this->apiModule == $report['module']) {

return $report;
}
}

}

private function findFirstLevelReport()
{
$firstLevelReport = array();
foreach ($this->getReportMetadata() as $report) {
if (!empty($report['actionToLoadSubTables'])
&& $this->apiMethod == $report['actionToLoadSubTables']
&& $this->apiModule == $report['module']
) {

$firstLevelReport = $report;
break;
}
}
return $firstLevelReport;
}
}
6 changes: 3 additions & 3 deletions core/API/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Exception;
use Piwik\API\DataTableManipulator\Flattener;
use Piwik\API\DataTableManipulator\LabelFilter;
use Piwik\API\DataTableManipulator\Totals;
use Piwik\API\DataTableManipulator\ReportTotalsCalculator;
use Piwik\Common;
use Piwik\DataTable\Renderer\Json;
use Piwik\DataTable\Renderer;
Expand Down Expand Up @@ -301,8 +301,8 @@ protected function handleDataTable($datatable)
}

if (1 == Common::getRequestVar('totals', '1', 'integer', $this->request)) {
$genericFilter = new Totals($this->apiModule, $this->apiMethod, $this->request);
$datatable = $genericFilter->generate($datatable);
$genericFilter = new ReportTotalsCalculator($this->apiModule, $this->apiMethod, $this->request);
$datatable = $genericFilter->calculate($datatable);
}

// if the flag disable_generic_filters is defined we skip the generic filters
Expand Down

0 comments on commit 53825ab

Please sign in to comment.