diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2013-11-20 07:40:08 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2013-11-20 07:40:08 +0400 |
commit | 4793bc774656c59df6a48eed239f72718570ac35 (patch) | |
tree | 5d4e08a01b3b6fa8a3adc1e48fb51a6320a76f88 | |
parent | 4cec24f6b8db0a8d65952cb837ea8632ec5cc3a4 (diff) |
refs #1816 do not create metrics for ratio, instead add total values to tableMetadata and generate ratio in view
-rw-r--r-- | core/API/DataTableManipulator/Totals.php (renamed from core/API/DataTableManipulator/AddRatioColumn.php) | 44 | ||||
-rw-r--r-- | core/API/ResponseBuilder.php | 9 | ||||
-rw-r--r-- | core/DataTable/Row.php | 2 | ||||
-rw-r--r-- | core/Metrics.php | 45 | ||||
-rw-r--r-- | core/Twig.php | 9 | ||||
-rw-r--r-- | plugins/API/ProcessedReport.php | 35 | ||||
-rw-r--r-- | plugins/CoreHome/templates/_dataTableCell.twig | 5 |
7 files changed, 35 insertions, 114 deletions
diff --git a/core/API/DataTableManipulator/AddRatioColumn.php b/core/API/DataTableManipulator/Totals.php index ea22f3c12b..2e5006f250 100644 --- a/core/API/DataTableManipulator/AddRatioColumn.php +++ b/core/API/DataTableManipulator/Totals.php @@ -25,7 +25,7 @@ use Piwik\Plugins\API\API; * @package Piwik * @subpackage Piwik_API */ -class AddRatioColumn extends DataTableManipulator +class Totals extends DataTableManipulator { protected $roundPrecision = 1; @@ -40,7 +40,7 @@ class AddRatioColumn extends DataTableManipulator * @param DataTable $table * @return \Piwik\DataTable|\Piwik\DataTable\Map */ - public function addColumns($table) + public function generate($table) { return $this->manipulate($table); } @@ -72,15 +72,7 @@ class AddRatioColumn extends DataTableManipulator } } - foreach ($this->totalValues as $metricId => $totalValue) { - if (!$this->hasDataTableMetric($dataTable, $metricId)) { - continue; - } - - foreach ($dataTable->getRows() as $row) { - $this->addRatioColumnIfPossible($row, $metricId, $totalValue); - } - } + $dataTable->setMetadata('totals', $this->totalValues); return $dataTable; } @@ -169,33 +161,13 @@ class AddRatioColumn extends DataTableManipulator return; } - if (array_key_exists($metricId, $this->totalValues)) { - $this->totalValues[$metricId] += $value; - } else { - $this->totalValues[$metricId] = $value; - } - } - - private function addRatioColumnIfPossible(Row $row, $metricId, $totalValue) - { - $value = $this->getColumn($row, $metricId); + $metricName = Metrics::getReadableColumnName($metricId); - if (false === $value) { - return; + if (array_key_exists($metricName, $this->totalValues)) { + $this->totalValues[$metricName] += $value; + } else { + $this->totalValues[$metricName] = $value; } - - $relativeValue = $this->getPercentage($value, $totalValue); - $metricName = Metrics::getReadableColumnName($metricId); - $ratioMetric = Metrics::makeReportRatioMetricName($metricName); - - $row->addColumn($ratioMetric, $relativeValue); - } - - private function getPercentage($value, $totalValue) - { - $percentage = Piwik::getPercentageSafe($value, $totalValue, $this->roundPrecision); - - return $percentage . '%'; } /** diff --git a/core/API/ResponseBuilder.php b/core/API/ResponseBuilder.php index d325d2bcb6..43616be345 100644 --- a/core/API/ResponseBuilder.php +++ b/core/API/ResponseBuilder.php @@ -11,9 +11,9 @@ namespace Piwik\API; use Exception; -use Piwik\API\DataTableManipulator\AddRatioColumn; use Piwik\API\DataTableManipulator\Flattener; use Piwik\API\DataTableManipulator\LabelFilter; +use Piwik\API\DataTableManipulator\Totals; use Piwik\Common; use Piwik\DataTable\Renderer\Json; use Piwik\DataTable\Renderer; @@ -306,10 +306,9 @@ class ResponseBuilder $genericFilter->filter($datatable); } - // if the flag disable_generic_filters is defined we skip the generic filters - if (1 == Common::getRequestVar('ratio', '1', 'integer', $this->request)) { - $genericFilter = new AddRatioColumn($this->apiModule, $this->apiMethod, $this->request); - $datatable = $genericFilter->addColumns($datatable); + if (1 == Common::getRequestVar('totals', '1', 'integer', $this->request)) { + $genericFilter = new Totals($this->apiModule, $this->apiMethod, $this->request); + $datatable = $genericFilter->generate($datatable); } // we automatically safe decode all datatable labels (against xss) diff --git a/core/DataTable/Row.php b/core/DataTable/Row.php index bc6a448b82..a8b3b24707 100644 --- a/core/DataTable/Row.php +++ b/core/DataTable/Row.php @@ -457,7 +457,7 @@ class Row public function sumRow(Row $rowToSum, $enableCopyMetadata = true, $aggregationOperations = false) { foreach ($rowToSum->getColumns() as $columnToSumName => $columnToSumValue) { - if (!isset(self::$unsummableColumns[$columnToSumName]) && false === strpos($columnToSumName, '_ratio')) // make sure we can add this column + if (!isset(self::$unsummableColumns[$columnToSumName])) // make sure we can add this column { $thisColumnValue = $this->getColumn($columnToSumName); diff --git a/core/Metrics.php b/core/Metrics.php index 10407f950a..9abb957a7a 100644 --- a/core/Metrics.php +++ b/core/Metrics.php @@ -322,51 +322,6 @@ class Metrics ); } - static public function getDefaultRatioMetrics() - { - $metrics = self::getMetricIdsToProcessRatio(); - $metricTranslations = self::getDefaultMetricTranslations(); - - $translations = array(); - - foreach ($metrics as $metricId) { - $readableMetric = self::getReadableColumnName($metricId); - $ratioMetric = self::makeReportRatioMetricName($readableMetric); - - if (array_key_exists($readableMetric, $metricTranslations)) { - $metricTranslated = $metricTranslations[$readableMetric]; - $translations[$ratioMetric] = Piwik::translate('General_ColumnRatioMetric', $metricTranslated); - } - } - - return $translations; - } - - static public function makeReportRatioMetricName($metric) - { - return $metric . '_ratio_report'; - } - - static public function getDefaultRatioMetricsDocumentation() - { - $metrics = self::getMetricIdsToProcessRatio(); - $metricTranslations = self::getDefaultMetricTranslations(); - - $translations = array(); - - foreach ($metrics as $metricId) { - $readableMetric = self::getReadableColumnName($metricId); - $ratioMetric = self::makeReportRatioMetricName($readableMetric); - - if (array_key_exists($readableMetric, $metricTranslations)) { - $metricTranslated = $metricTranslations[$readableMetric]; - $translations[$ratioMetric] = Piwik::translate('General_ColumnRatioMetricDocumentation', $metricTranslated); - } - } - - return $translations; - } - static public function getDefaultMetricsDocumentation() { $documentation = array( diff --git a/core/Twig.php b/core/Twig.php index 01282ef35d..69a6501d90 100644 --- a/core/Twig.php +++ b/core/Twig.php @@ -65,6 +65,7 @@ class Twig $this->addFilter_money(); $this->addFilter_truncate(); $this->addFilter_notificiation(); + $this->addFilter_percentage(); $this->twig->addFilter(new Twig_SimpleFilter('implode', 'implode')); $this->twig->addFilter(new Twig_SimpleFilter('ucwords', 'ucwords')); @@ -187,6 +188,14 @@ class Twig $this->twig->addFilter($notificationFunction); } + protected function addFilter_percentage() + { + $percentage = new Twig_SimpleFilter('percentage', function ($string, $totalValue, $precision = 1) { + return Piwik::getPercentageSafe($string, $totalValue, $precision) . '%'; + }); + $this->twig->addFilter($percentage); + } + protected function addFilter_truncate() { $truncateFilter = new Twig_SimpleFilter('truncate', function ($string, $size) { diff --git a/plugins/API/ProcessedReport.php b/plugins/API/ProcessedReport.php index 019dd1fbf4..78739e87bf 100644 --- a/plugins/API/ProcessedReport.php +++ b/plugins/API/ProcessedReport.php @@ -118,10 +118,6 @@ class ProcessedReport $availableReport['processedMetrics'] = Metrics::getDefaultProcessedMetrics(); } - if (!isset($availableReport['ratioMetrics'])) { - $availableReport['ratioMetrics'] = Metrics::getDefaultRatioMetrics(); - } - if ($hideMetricsDoc) // remove metric documentation if it's not wanted { unset($availableReport['metricsDocumentation']); @@ -158,7 +154,7 @@ class ProcessedReport // Add the magic API.get report metadata aggregating all plugins API.get API calls automatically $this->addApiGetMetdata($availableReports); - $knownMetrics = array_merge(Metrics::getDefaultMetrics(), Metrics::getDefaultProcessedMetrics(), Metrics::getDefaultRatioMetrics()); + $knownMetrics = array_merge(Metrics::getDefaultMetrics(), Metrics::getDefaultProcessedMetrics()); foreach ($availableReports as &$availableReport) { // Ensure all metrics have a translation $metrics = $availableReport['metrics']; @@ -181,9 +177,6 @@ class ProcessedReport if (isset($availableReport['processedMetrics'])) { $availableReport['processedMetrics'] = $this->hideShowMetrics($availableReport['processedMetrics']); } - if (isset($availableReport['ratioMetrics'])) { - $availableReport['ratioMetrics'] = $this->hideShowMetrics($availableReport['ratioMetrics']); - } if (isset($availableReport['metricsDocumentation'])) { $availableReport['metricsDocumentation'] = $this->hideShowMetrics($availableReport['metricsDocumentation']); @@ -334,7 +327,7 @@ class ProcessedReport throw new Exception("API returned an error: " . $e->getMessage() . " at " . basename($e->getFile()) . ":" . $e->getLine() . "\n"); } - list($newReport, $columns, $rowsMetadata) = $this->handleTableReport($idSite, $dataTable, $reportMetadata, $showRawMetrics); + list($newReport, $columns, $rowsMetadata, $totals) = $this->handleTableReport($idSite, $dataTable, $reportMetadata, $showRawMetrics); foreach ($columns as $columnId => &$name) { $name = ucfirst($name); } @@ -350,6 +343,7 @@ class ProcessedReport 'columns' => $columns, 'reportData' => $newReport, 'reportMetadata' => $rowsMetadata, + 'total' => $totals ); if ($showTimer) { $return['timerMillis'] = $timer->getTimeMs(0); @@ -383,21 +377,6 @@ class ProcessedReport $columns ); - if (isset($reportMetadata['ratioMetrics'])) { - $ratioMetricDocs = Metrics::getDefaultRatioMetricsDocumentation(); - - // we automatically detect which ratio metrics should be added - foreach ($reportMetadata['ratioMetrics'] as $ratioMetricId => $ratioMetricTranslation) { - if (array_filter($dataTable->getColumn($ratioMetricId))) { - $columns[$ratioMetricId] = $ratioMetricTranslation; - - if (array_key_exists('metricsDocumentation', $reportMetadata)) { - $reportMetadata['metricsDocumentation'][$ratioMetricId] = $ratioMetricDocs[$ratioMetricId]; - } - } - } - } - if (isset($reportMetadata['processedMetrics'])) { $processedMetricsAdded = Metrics::getDefaultProcessedMetrics(); foreach ($processedMetricsAdded as $processedMetricId => $processedMetricTranslation) { @@ -453,10 +432,16 @@ class ProcessedReport list($newReport, $rowsMetadata) = $this->handleSimpleDataTable($idSite, $dataTable, $columns, $hasDimension, $showRawMetrics); } + $totals = array(); + if ($dataTable->getMetadata('totals')) { + $totals = $this->hideShowMetrics($dataTable->getMetadata('totals')); + } + return array( $newReport, $columns, - $rowsMetadata + $rowsMetadata, + $totals ); } diff --git a/plugins/CoreHome/templates/_dataTableCell.twig b/plugins/CoreHome/templates/_dataTableCell.twig index 79bcf466a4..e72aaa6e72 100644 --- a/plugins/CoreHome/templates/_dataTableCell.twig +++ b/plugins/CoreHome/templates/_dataTableCell.twig @@ -20,7 +20,8 @@ </a> {% endif %} {% if row.getMetadata(tooltipIndex) %}</span>{% endif %} -{% if row.getColumn([column, '_ratio_report']|join) -%} - <span class="ratio"> {{ row.getColumn([column, '_ratio_report']|join) }}</span> + {% set totals = dataTable.getMetadata('totals') %} +{% if column in totals|keys and totals[column] -%} + <span class="ratio"> {{ row.getColumn(column)|percentage(totals[column], 1) }}</span> {%- endif %} {% endspaceless %} |