Skip to content

Commit

Permalink
refs #1816 do not create metrics for ratio, instead add total values …
Browse files Browse the repository at this point in the history
…to tableMetadata and generate ratio in view
  • Loading branch information
tsteur committed Nov 20, 2013
1 parent 4cec24f commit 4793bc7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 114 deletions.
Expand Up @@ -25,7 +25,7 @@
* @package Piwik
* @subpackage Piwik_API
*/
class AddRatioColumn extends DataTableManipulator
class Totals extends DataTableManipulator
{
protected $roundPrecision = 1;

Expand All @@ -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);
}
Expand Down Expand Up @@ -72,15 +72,7 @@ protected function manipulateDataTable($dataTable)
}
}

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;
}
Expand Down Expand Up @@ -169,33 +161,13 @@ private function addColumnValueToTotal(Row $row, $metricId)
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 . '%';
}

/**
Expand Down
9 changes: 4 additions & 5 deletions core/API/ResponseBuilder.php
Expand Up @@ -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;
Expand Down Expand Up @@ -306,10 +306,9 @@ protected function handleDataTable($datatable)
$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)
Expand Down
2 changes: 1 addition & 1 deletion core/DataTable/Row.php
Expand Up @@ -457,7 +457,7 @@ public function addMetadata($name, $value)
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);

Expand Down
45 changes: 0 additions & 45 deletions core/Metrics.php
Expand Up @@ -322,51 +322,6 @@ static public function getMetricIdsToProcessRatio()
);
}

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(
Expand Down
9 changes: 9 additions & 0 deletions core/Twig.php
Expand Up @@ -65,6 +65,7 @@ public function __construct()
$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'));

Expand Down Expand Up @@ -187,6 +188,14 @@ protected function addFilter_notificiation()
$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) {
Expand Down
35 changes: 10 additions & 25 deletions plugins/API/ProcessedReport.php
Expand Up @@ -118,10 +118,6 @@ public function getReportMetadata($idSites, $period = false, $date = false, $hid
$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']);
Expand Down Expand Up @@ -158,7 +154,7 @@ public function getReportMetadata($idSites, $period = false, $date = false, $hid
// 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'];
Expand All @@ -181,9 +177,6 @@ public function getReportMetadata($idSites, $period = false, $date = false, $hid
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']);
Expand Down Expand Up @@ -334,7 +327,7 @@ public function getProcessedReport($idSite, $period, $date, $apiModule, $apiActi
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);
}
Expand All @@ -350,6 +343,7 @@ public function getProcessedReport($idSite, $period, $date, $apiModule, $apiActi
'columns' => $columns,
'reportData' => $newReport,
'reportMetadata' => $rowsMetadata,
'total' => $totals
);
if ($showTimer) {
$return['timerMillis'] = $timer->getTimeMs(0);
Expand Down Expand Up @@ -383,21 +377,6 @@ private function handleTableReport($idSite, $dataTable, &$reportMetadata, $showR
$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) {
Expand Down Expand Up @@ -453,10 +432,16 @@ private function handleTableReport($idSite, $dataTable, &$reportMetadata, $showR
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
);
}

Expand Down
5 changes: 3 additions & 2 deletions plugins/CoreHome/templates/_dataTableCell.twig
Expand Up @@ -20,7 +20,8 @@
</a>
{% endif %}
{% if row.getMetadata(tooltipIndex) %}</span>{% endif %}
{% if row.getColumn([column, '_ratio_report']|join) -%}
<span class="ratio">&nbsp;&nbsp;&nbsp;{{ row.getColumn([column, '_ratio_report']|join) }}</span>
{% set totals = dataTable.getMetadata('totals') %}
{% if column in totals|keys and totals[column] -%}
<span class="ratio">&nbsp;{{ row.getColumn(column)|percentage(totals[column], 1) }}</span>
{%- endif %}
{% endspaceless %}

0 comments on commit 4793bc7

Please sign in to comment.