From 757d47c08a0b5f51d42748c2b8c6bdd787e6ac7e Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Wed, 26 Nov 2014 14:42:54 -0800
Subject: [PATCH] Allow ProcessedMetrics to declare some metrics as temporary
 so they will be removed if not explicitly requested.

---
 core/API/DataTablePostProcessor.php           | 22 +++++++++++++++++++
 core/Plugin/ProcessedMetric.php               | 13 +++++++++++
 .../Metrics/AveragePageGenerationTime.php     |  5 +++++
 ...woVisitorsTwoWebsitesDifferentDaysTest.php |  8 +------
 4 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/core/API/DataTablePostProcessor.php b/core/API/DataTablePostProcessor.php
index 955c51acd7..11f4126402 100644
--- a/core/API/DataTablePostProcessor.php
+++ b/core/API/DataTablePostProcessor.php
@@ -248,11 +248,33 @@ class DataTablePostProcessor
             || !empty($showColumns)
         ) {
             $dataTable->filter('ColumnDelete', array($hideColumns, $showColumns));
+        } else {
+            $this->removeTemporaryMetrics($dataTable);
         }
 
         return $dataTable;
     }
 
+    /**
+     * @param DataTableInterface $dataTable
+     */
+    public function removeTemporaryMetrics(DataTableInterface $dataTable)
+    {
+        $allColumns = !empty($this->report) ? $this->report->getAllMetrics() : array();
+
+        $report = $this->report;
+        $dataTable->filter(function (DataTable $table) use ($report, $allColumns) {
+            $processedMetrics = Report::getProcessedMetricsForTable($table, $this->report);
+
+            $allTemporaryMetrics = array();
+            foreach ($processedMetrics as $metric) {
+                $allTemporaryMetrics = array_merge($allTemporaryMetrics, $metric->getTemporaryMetrics());
+            }
+
+            $table->filter('ColumnDelete', array($allTemporaryMetrics));
+        });
+    }
+
     /**
      * @param DataTableInterface $dataTable
      * @return DataTableInterface
diff --git a/core/Plugin/ProcessedMetric.php b/core/Plugin/ProcessedMetric.php
index 2280ea531e..20201d0887 100644
--- a/core/Plugin/ProcessedMetric.php
+++ b/core/Plugin/ProcessedMetric.php
@@ -41,6 +41,19 @@ abstract class ProcessedMetric extends Metric
      */
     abstract public function getDependentMetrics();
 
+    /**
+     * Returns the array of metrics that are necessary for computing this metric, but should not
+     * be displayed to the user unless explicitly requested. These metrics are intermediate
+     * metrics that are not really valuable to the user. On a request, if showColumns or hideColumns
+     * is not used, they will be removed automatically.
+     *
+     * @return string[]
+     */
+    public function getTemporaryMetrics()
+    {
+        return array();
+    }
+
     /**
      * Executed before computing all processed metrics for a report. Implementers can return `false`
      * to skip computing this metric.
diff --git a/plugins/Actions/Columns/Metrics/AveragePageGenerationTime.php b/plugins/Actions/Columns/Metrics/AveragePageGenerationTime.php
index f7df68aaaa..6847a7322d 100644
--- a/plugins/Actions/Columns/Metrics/AveragePageGenerationTime.php
+++ b/plugins/Actions/Columns/Metrics/AveragePageGenerationTime.php
@@ -40,6 +40,11 @@ class AveragePageGenerationTime extends ProcessedMetric
         return array('sum_time_generation', 'nb_hits_with_time_generation');
     }
 
+    public function getTemporaryMetrics()
+    {
+        return array('nb_hits_with_time_generation');
+    }
+
     public function compute(Row $row)
     {
         $sumGenerationTime = $this->getMetric($row, 'sum_time_generation');
diff --git a/tests/PHPUnit/System/TwoVisitorsTwoWebsitesDifferentDaysTest.php b/tests/PHPUnit/System/TwoVisitorsTwoWebsitesDifferentDaysTest.php
index 7816253781..c9fd4f04c5 100755
--- a/tests/PHPUnit/System/TwoVisitorsTwoWebsitesDifferentDaysTest.php
+++ b/tests/PHPUnit/System/TwoVisitorsTwoWebsitesDifferentDaysTest.php
@@ -62,13 +62,7 @@ class TwoVisitorsTwoWebsitesDifferentDaysTest extends SystemTestCase
         $singlePeriodApi = array('VisitsSummary.get', 'Goals.get');
 
         $periods = array('day', 'week', 'month', 'year');
-return array(
-    array('Goals.get', array('idSite'       => 'all',
-        'date'         => $dateTime,
-        'periods'      => array('day'),
-        'setDateLastN' => false,
-        'testSuffix'   => '_NotLastNPeriods'))
-);
+
         $result = array(
             // Request data for the last 6 periods and idSite=all
             array($apiToCall, array('idSite'       => 'all',
-- 
GitLab