diff options
author | sgiehl <stefan@matomo.org> | 2022-11-11 13:37:46 +0300 |
---|---|---|
committer | sgiehl <stefan@matomo.org> | 2022-11-11 13:37:46 +0300 |
commit | f667381442697c5320230143da39355f83af49d8 (patch) | |
tree | fe70d3856fa0c068283653de289eb270e31e0ea1 | |
parent | c9800fcb9746b3adb3b201b85280015c2346e087 (diff) |
Avoid potential unneeded requests in Goals sparklinesgoalsget
-rw-r--r-- | plugins/Goals/Reports/Get.php | 118 |
1 files changed, 69 insertions, 49 deletions
diff --git a/plugins/Goals/Reports/Get.php b/plugins/Goals/Reports/Get.php index 5a57c1d3d8..cc8167afbf 100644 --- a/plugins/Goals/Reports/Get.php +++ b/plugins/Goals/Reports/Get.php @@ -184,60 +184,80 @@ class Get extends Base [$lastPeriodDate, $ignore] = Range::getLastDate(); if ($lastPeriodDate !== false) { - /** @var DataTable $previousData */ - $previousData = Request::processRequest('Goals.get', ['date' => $lastPeriodDate, 'format_metrics' => '0']); - $previousDataRow = $previousData->getFirstRow(); - - $currentPeriod = PeriodFactory::build(Piwik::getPeriod(), Common::getRequestVar('date')); - $currentPrettyDate = ($currentPeriod instanceof Month ? $currentPeriod->getLocalizedLongString() : $currentPeriod->getPrettyString()); - $lastPeriod = PeriodFactory::build(Piwik::getPeriod(), $lastPeriodDate); - $lastPrettyDate = ($currentPeriod instanceof Month ? $lastPeriod->getLocalizedLongString() : $lastPeriod->getPrettyString()); - - $view->config->compute_evolution = function ($columns, $metrics) use ($currentPrettyDate, $lastPrettyDate, $previousDataRow, $idSite) { - - $value = reset($columns); - $columnName = key($columns); - $pastValue = $previousDataRow ? $previousDataRow->getColumn($columnName) : 0; - - if (!is_numeric($value)) { - return; - } + // Using a filter here ensures the additional request is only performed when the view is rendered + $view->config->filters[] = function($datatable) use ($view, $lastPeriodDate, $idSite) { + /** @var DataTable $previousData */ + $previousData = Request::processRequest( + 'Goals.get', + ['date' => $lastPeriodDate, 'format_metrics' => '0'] + ); + $previousDataRow = $previousData->getFirstRow(); + + $currentPeriod = PeriodFactory::build(Piwik::getPeriod(), Common::getRequestVar('date')); + $currentPrettyDate = ($currentPeriod instanceof Month ? $currentPeriod->getLocalizedLongString( + ) : $currentPeriod->getPrettyString()); + $lastPeriod = PeriodFactory::build(Piwik::getPeriod(), $lastPeriodDate); + $lastPrettyDate = ($currentPeriod instanceof Month ? $lastPeriod->getLocalizedLongString( + ) : $lastPeriod->getPrettyString()); + + $view->config->compute_evolution = function ($columns, $metrics) use ( + $currentPrettyDate, + $lastPrettyDate, + $previousDataRow, + $idSite + ) { + $value = reset($columns); + $columnName = key($columns); + $pastValue = $previousDataRow ? $previousDataRow->getColumn($columnName) : 0; + + if (!is_numeric($value)) { + return; + } - // Format - $formatter = new MetricFormatter(); - $currentValueFormatted = $value; - $pastValueFormatted = $pastValue; - foreach ($metrics as $metric) { - if ($metric->getName() === $columnName) { - $pastValueFormatted = $metric->format($pastValue, $formatter); - $currentValueFormatted = $metric->format($value, $formatter); - break; + // Format + $formatter = new MetricFormatter(); + $currentValueFormatted = $value; + $pastValueFormatted = $pastValue; + foreach ($metrics as $metric) { + if ($metric->getName() === $columnName) { + $pastValueFormatted = $metric->format($pastValue, $formatter); + $currentValueFormatted = $metric->format($value, $formatter); + break; + } } - } - if (strpos($columnName, 'revenue') !== false) { - $currencySymbol = Site::getCurrencySymbolFor($idSite); - $pastValueFormatted = NumberFormatter::getInstance()->formatCurrency($pastValue, $currencySymbol, GoalManager::REVENUE_PRECISION); - $currentValueFormatted = NumberFormatter::getInstance()->formatCurrency($value, $currencySymbol, GoalManager::REVENUE_PRECISION); - } + if (strpos($columnName, 'revenue') !== false) { + $currencySymbol = Site::getCurrencySymbolFor($idSite); + $pastValueFormatted = NumberFormatter::getInstance()->formatCurrency( + $pastValue, + $currencySymbol, + GoalManager::REVENUE_PRECISION + ); + $currentValueFormatted = NumberFormatter::getInstance()->formatCurrency( + $value, + $currencySymbol, + GoalManager::REVENUE_PRECISION + ); + } - $columnTranslations = Metrics::getDefaultMetricTranslations(); - $columnTranslation = ''; - if (array_key_exists($columnName, $columnTranslations)) { - $columnTranslation = $columnTranslations[$columnName]; - } + $columnTranslations = Metrics::getDefaultMetricTranslations(); + $columnTranslation = ''; + if (array_key_exists($columnName, $columnTranslations)) { + $columnTranslation = $columnTranslations[$columnName]; + } - return [ - 'currentValue' => $value, - 'pastValue' => $pastValue, - 'tooltip' => Piwik::translate('General_EvolutionSummaryGeneric', [ - $currentValueFormatted . ' ' . $columnTranslation, - $currentPrettyDate, - $pastValueFormatted . ' ' . $columnTranslation, - $lastPrettyDate, - CalculateEvolutionFilter::calculate($value, $pastValue, $precision = 1) - ]), - ]; + return [ + 'currentValue' => $value, + 'pastValue' => $pastValue, + 'tooltip' => Piwik::translate('General_EvolutionSummaryGeneric', [ + $currentValueFormatted . ' ' . $columnTranslation, + $currentPrettyDate, + $pastValueFormatted . ' ' . $columnTranslation, + $lastPrettyDate, + CalculateEvolutionFilter::calculate($value, $pastValue, $precision = 1) + ]), + ]; + }; }; } } elseif ($view->isViewDataTableId(Evolution::ID)) { |