Skip to content
Extraits de code Groupes Projets
Valider 551413f3 rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Move MetricsFormatter::getMetricsToFormat logic to Report class.

parent c8daaabc
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -167,9 +167,8 @@ class DataTablePostProcessor
$self = $this;
$report = $this->report;
$formatter = $this->formatter;
$dataTable->filter(function (DataTable $table) use ($genericFilter, $report, $self, $formatter) {
$processedMetrics = $formatter->getMetricsToFormat($table, $report, 'Piwik\\Plugin\\ProcessedMetric');
$dataTable->filter(function (DataTable $table) use ($genericFilter, $report, $self) {
$processedMetrics = Report::getProcessedMetricsForTable($table, $report);
if ($genericFilter->areProcessedMetricsNeededFor($processedMetrics)) {
$self->computeProcessedMetrics($table);
}
......@@ -333,7 +332,7 @@ class DataTablePostProcessor
}
/** @var ProcessedMetric[] $processedMetrics */
$processedMetrics = $this->formatter->getMetricsToFormat($dataTable, $this->report, 'Piwik\\Plugin\\ProcessedMetric');
$processedMetrics = Report::getProcessedMetricsForTable($dataTable, $this->report);
if (empty($processedMetrics)) {
return;
}
......
......@@ -298,25 +298,8 @@ class Formatter
* @param Report $report
* @return Metric[]
*/
public function getMetricsToFormat(DataTable $dataTable, Report $report = null, $baseType = 'Piwik\\Plugin\\Metric')
private function getMetricsToFormat(DataTable $dataTable, Report $report = null)
{
$metrics = $dataTable->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME) ?: array();
if (!empty($report)) {
$metrics = array_merge($metrics, $report->getProcessedMetricsById());
}
$result = array();
/** @var Metric $metric */
foreach ($metrics as $metric) {
if (!($metric instanceof $baseType)) {
continue;
}
$result[$metric->getName()] = $metric;
}
return $result;
return Report::getMetricsForTable($dataTable, $report, $baseType = 'Piwik\\Plugin\\Metric');
}
}
\ No newline at end of file
......@@ -843,4 +843,52 @@ class Report
}
return $result;
}
/**
* Returns the Metrics that are displayed by a DataTable of a certain Report type.
*
* Includes ProcessedMetrics and Metrics.
*
* @param DataTable $dataTable
* @param Report|null $report
* @param string $baseType The base type each metric class needs to be of.
* @return Metric[]
* @api
*/
public static function getMetricsForTable(DataTable $dataTable, Report $report = null, $baseType = 'Piwik\\Plugin\\Metric')
{
$metrics = $dataTable->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME) ?: array();
if (!empty($report)) {
$metrics = array_merge($metrics, $report->getProcessedMetricsById());
}
$result = array();
/** @var Metric $metric */
foreach ($metrics as $metric) {
if (!($metric instanceof $baseType)) {
continue;
}
$result[$metric->getName()] = $metric;
}
return $result;
}
/**
* Returns the ProcessedMetrics that should be computed and formatted for a DataTable of a
* certain report. The ProcessedMetrics returned are those specified by the Report metadata
* as well as the DataTable metadata.
*
* @param DataTable $dataTable
* @param Report|null $report
* @return ProcessedMetric[]
* @api
*/
public static function getProcessedMetricsForTable(DataTable $dataTable, Report $report = null)
{
return self::getMetricsForTable($dataTable, $report, 'Piwik\\Plugin\\ProcessedMetric');
}
}
\ No newline at end of file
......@@ -320,11 +320,11 @@ class Visualization extends ViewDataTable
{
$dataTable = $this->dataTable instanceof DataTable\Map ? $this->dataTable->getFirstRow() : $this->dataTable;
$processedMetrics = $this->metricsFormatter->getMetricsToFormat($dataTable, $this->report);
$metrics = Report::getMetricsForTable($dataTable, $this->report);
// TODO: instead of iterating & calling translate everywhere, maybe we can get all translated names in one place.
// may be difficult, though, since translated metrics are specific to the report.
foreach ($processedMetrics as $metric) {
foreach ($metrics as $metric) {
$name = $metric->getName();
if (empty($this->config->translations[$name])) {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter