Skip to content

Commit

Permalink
revert most changes to EvolutionMetric and rely on new API.getProcess…
Browse files Browse the repository at this point in the history
…edReport behavior to get this metric's metadata
  • Loading branch information
diosmosis committed Sep 20, 2024
1 parent 8f10b42 commit 685b419
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 92 deletions.
83 changes: 32 additions & 51 deletions plugins/CoreHome/Columns/Metrics/EvolutionMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
*/
class EvolutionMetric extends ProcessedMetric
{
const DATATABLE_METADATA_PAST_DATA_NAME = 'EvolutionMetric_past_data';

/**
* @var Metric|string
*/
Expand Down Expand Up @@ -99,7 +97,6 @@ class EvolutionMetric extends ProcessedMetric
* @param int $quotientPrecision The percent's quotient precision.
* @param DataTable|null $currentData The current datatable, optional but required to calculate the proportionate
* evolution values
* TODO: add extra params
*/
public function __construct(
$wrapped,
Expand All @@ -115,9 +112,6 @@ public function __construct(
$this->isLowerBetter = Metrics::isLowerValueBetter($this->wrapped);
$this->pastData = $pastData;
$this->currentData = $currentData;
$this->wrappedMetricTranslatedName = $wrappedMetricTranslatedName;
$this->wrappedSemanticType = $wrappedSemanticType;
$this->wrappedMetricAggregationType = $wrappedMetricAggregationType;

if (empty($evolutionMetricName)) {
$wrappedName = $this->getWrappedName();
Expand Down Expand Up @@ -152,19 +146,6 @@ public function getTranslatedName()
return Piwik::translate('API_EvolutionMetricName', [$metricName]);
}

public function beforeCompute($report, DataTable $table)
{
if (empty($this->pastData)) {
$this->pastData = $table->getMetadata(self::DATATABLE_METADATA_PAST_DATA_NAME);
}

if (empty($this->currentData)) {
$this->currentData = $table;
}

return true; // always compute
}

public function getTrendValue($computedValue = 0)
{
if ($this->isLowerBetter) {
Expand Down Expand Up @@ -194,38 +175,6 @@ public function computeExtraTemporaryMetrics(Row $row): array
return [ "past_{$columnName}" => $pastValue ];
}

public function getExtraMetricAggregationTypes(): array
{
$columnName = $this->getWrappedName();

if (!empty($this->wrappedMetricAggregationType)) {
$aggregationType = $this->wrappedMetricAggregationType;
} else if ($this->wrapped instanceof Metric) {
$aggregationType = $this->wrapped->getAggregationType();
} else {
$allAggregationTypes = Metrics::getDefaultMetricAggregationTypes();
$aggregationType = $allAggregationTypes[$columnName] ?? null;
}

return [ "past_{$columnName}" => $aggregationType ];
}

public function getExtraMetricSemanticTypes(): array
{
$columnName = $this->getWrappedName();

if (!empty($this->wrappedSemanticType)) {
$semanticType = $this->wrappedSemanticType;
} else if ($this->wrapped instanceof Metric) {
$semanticType = $this->wrapped->getSemanticType();
} else {
$allSemanticTypes = Metrics::getDefaultMetricSemanticTypes();
$semanticType = $allSemanticTypes[$columnName] ?? null;
}

return [ "past_{$columnName}" => $semanticType ];
}

public function compute(Row $row)
{
$columnName = $this->getWrappedName();
Expand Down Expand Up @@ -365,4 +314,36 @@ public function getFormula(): ?string

return sprintf("%s == 0 ? 1 : ((%s - %s) / %s)", $pastColumnName, '$' . $columnName, $pastColumnName, $pastColumnName);
}

public function getExtraMetricAggregationTypes(): array
{
$columnName = $this->getWrappedName();

if (!empty($this->wrappedMetricAggregationType)) {
$aggregationType = $this->wrappedMetricAggregationType;
} else if ($this->wrapped instanceof Metric) {

Check warning on line 324 in plugins/CoreHome/Columns/Metrics/EvolutionMetric.php

View workflow job for this annotation

GitHub Actions / PHPCS

Usage of ELSE IF is discouraged; use ELSEIF instead
$aggregationType = $this->wrapped->getAggregationType();
} else {
$allAggregationTypes = Metrics::getDefaultMetricAggregationTypes();
$aggregationType = $allAggregationTypes[$columnName] ?? null;
}

return [ "past_{$columnName}" => $aggregationType ];
}

public function getExtraMetricSemanticTypes(): array
{
$columnName = $this->getWrappedName();

if (!empty($this->wrappedSemanticType)) {
$semanticType = $this->wrappedSemanticType;
} else if ($this->wrapped instanceof Metric) {

Check warning on line 340 in plugins/CoreHome/Columns/Metrics/EvolutionMetric.php

View workflow job for this annotation

GitHub Actions / PHPCS

Usage of ELSE IF is discouraged; use ELSEIF instead
$semanticType = $this->wrapped->getSemanticType();
} else {
$allSemanticTypes = Metrics::getDefaultMetricSemanticTypes();
$semanticType = $allSemanticTypes[$columnName] ?? null;
}

return [ "past_{$columnName}" => $semanticType ];
}
}
31 changes: 28 additions & 3 deletions plugins/MultiSites/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
use Piwik\Period\Range;
use Piwik\Piwik;
use Piwik\Plugin\Metric;
use Piwik\Plugins\CoreHome\Columns\Metrics\EvolutionMetric;
use Piwik\Plugins\Goals\Archiver;
use Piwik\Plugins\FeatureFlags\FeatureFlagManager;
use Piwik\Plugins\Goals\Archiver;
use Piwik\Plugins\MultiSites\FeatureFlags\ImprovedAllWebsitesDashboard;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Scheduler\Scheduler;
Expand Down Expand Up @@ -507,7 +506,24 @@ private function calculateEvolutionPercentages($currentData, $pastData, $apiMetr
next($pastArray);
}
} else {
$currentData->setMetadata(EvolutionMetric::DATATABLE_METADATA_PAST_DATA_NAME, $pastData);
$extraProcessedMetrics = $currentData->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME);
foreach ($apiMetrics as $metricSettings) {
$evolutionMetricClass = $this->isEcommerceEvolutionMetric($metricSettings)
? "Piwik\\Plugins\\MultiSites\\Columns\\Metrics\\EcommerceOnlyEvolutionMetric"
: "Piwik\\Plugins\\CoreHome\\Columns\\Metrics\\EvolutionMetric";

$extraProcessedMetrics = is_array($extraProcessedMetrics) ? $extraProcessedMetrics : [];
$extraProcessedMetrics[] = new $evolutionMetricClass(
$metricSettings[self::METRIC_RECORD_NAME_KEY],
$pastData,
$metricSettings[self::METRIC_EVOLUTION_COL_NAME_KEY],
$quotientPrecision = 1,
$currentData,
$metricSettings[API::METRIC_TRANSLATION_KEY],
$metricSettings[API::METRIC_WRAPPED_SEMANTIC_TYPE_KEY],
$metricSettings[API::METRIC_WRAPPED_AGGREGATION_TYPE_KEY] );

Check failure on line 524 in plugins/MultiSites/API.php

View workflow job for this annotation

GitHub Actions / PHPCS

Multi-line function call not indented correctly; expected 16 spaces but found 20

Check failure on line 524 in plugins/MultiSites/API.php

View workflow job for this annotation

GitHub Actions / PHPCS

Closing parenthesis of a multi-line function call must be on a line by itself
}
$currentData->setMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME, $extraProcessedMetrics);
}
}

Expand Down Expand Up @@ -701,4 +717,13 @@ private function populateLabel($dataTable)
}
});
}

private function isEcommerceEvolutionMetric($metricSettings)
{
return in_array($metricSettings[self::METRIC_EVOLUTION_COL_NAME_KEY], array(
self::GOAL_REVENUE_METRIC . '_evolution',
self::ECOMMERCE_ORDERS_METRIC . '_evolution',
self::ECOMMERCE_REVENUE_METRIC . '_evolution'
));
}
}
28 changes: 2 additions & 26 deletions plugins/MultiSites/Reports/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
namespace Piwik\Plugins\MultiSites\Reports;

use Piwik\Piwik;
use Piwik\Plugins\CoreHome\Columns\Metrics\EvolutionMetric;
use Piwik\Plugins\MultiSites\API;
use Piwik\Plugins\MultiSites\Columns\Metrics\EcommerceOnlyEvolutionMetric;

abstract class Base extends \Piwik\Plugin\Report
{
Expand All @@ -26,36 +24,14 @@ protected function init()
$processedMetricsMetadata = array();

foreach ($allMetricsInfo as $metricName => $metricSettings) {
$evolutionMetricClass = $this->isEcommerceEvolutionMetric($metricSettings)
? EcommerceOnlyEvolutionMetric::class
: EvolutionMetric::class;

$metadataMetrics[$metricName] =
Piwik::translate($metricSettings[API::METRIC_TRANSLATION_KEY]);

$processedMetricsMetadata[$metricSettings[API::METRIC_EVOLUTION_COL_NAME_KEY]] =
new $evolutionMetricClass(
$metricSettings[API::METRIC_RECORD_NAME_KEY],
null,
$metricSettings[API::METRIC_EVOLUTION_COL_NAME_KEY],
$quotientPrecision = 1,
null,
$metricSettings[API::METRIC_TRANSLATION_KEY],
$metricSettings[API::METRIC_WRAPPED_SEMANTIC_TYPE_KEY],
$metricSettings[API::METRIC_WRAPPED_AGGREGATION_TYPE_KEY]
);
Piwik::translate($metricSettings[API::METRIC_TRANSLATION_KEY]) . " " . Piwik::translate('MultiSites_Evolution');
}

$this->metrics = array_keys($metadataMetrics);
$this->processedMetrics = $processedMetricsMetadata;
}

private function isEcommerceEvolutionMetric($metricSettings): bool
{
return in_array($metricSettings[API::METRIC_EVOLUTION_COL_NAME_KEY], array(
API::GOAL_REVENUE_METRIC . '_evolution',
API::ECOMMERCE_ORDERS_METRIC . '_evolution',
API::ECOMMERCE_REVENUE_METRIC . '_evolution'
));
$this->processedMetrics = array_keys($processedMetricsMetadata);
}
}
Loading

0 comments on commit 685b419

Please sign in to comment.