Skip to content

Commit

Permalink
Allow ProcessedMetrics to declare some metrics as temporary so they w…
Browse files Browse the repository at this point in the history
…ill be removed if not explicitly requested.
  • Loading branch information
diosmosis committed Nov 26, 2014
1 parent 8fafa66 commit 757d47c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
22 changes: 22 additions & 0 deletions core/API/DataTablePostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,33 @@ public function applyRequestedColumnDeletion($dataTable)
|| !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
Expand Down
13 changes: 13 additions & 0 deletions core/Plugin/ProcessedMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ abstract public function compute(Row $row);
*/
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.
Expand Down
5 changes: 5 additions & 0 deletions plugins/Actions/Columns/Metrics/AveragePageGenerationTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function getDependentMetrics()
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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ public function getApiForTesting()
$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',
Expand Down

0 comments on commit 757d47c

Please sign in to comment.