Skip to content

Commit

Permalink
Prevent recomputing of metric metadata in EvolutionMetrics (#22818)
Browse files Browse the repository at this point in the history
* Prevent recomputing of ratio, currency symbol and previousRange to optimise performance of evolution

* Fix undefined variable

* Tidy up code, group/optimise period fetching

* Change order to make tests happy
  • Loading branch information
caddoo authored Dec 9, 2024
1 parent 08e2c5b commit ff14ebf
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions plugins/CoreHome/Columns/Metrics/EvolutionMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,28 @@ public function compute(Row $row)
$currentValue = $this->getMetric($row, $columnName);
$pastValue = $pastRow ? $this->getMetric($pastRow, $columnName) : 0;

// Reduce past value proportionally to match the percent of the current period which is complete, if applicable
$ratio = self::getRatio($this->currentData, $this->pastData, $row);
$period = $this->pastData->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX);
$row->setMetadata('ratio', $ratio);
$row->setMetadata('currencySymbol', $row['label'] !== DataTable::ID_SUMMARY_ROW && $row['label'] !== DataTable::LABEL_TOTALS_ROW ? Site::getCurrencySymbolFor($row['label']) : API::getInstance()->getDefaultCurrency());
if ($row->getMetadata('ratio') === false) {
// Reduce past value proportionally to match the percent of the current period which is complete, if applicable
$ratio = self::getRatio($this->currentData, $this->pastData, $row);
$row->setMetadata('ratio', $ratio);
}

if ($row->getMetadata('currencySymbol') === false) {
$row->setMetadata(
'currencySymbol',
$row['label'] !== DataTable::ID_SUMMARY_ROW && $row['label'] !== DataTable::LABEL_TOTALS_ROW ? Site::getCurrencySymbolFor($row['label']) : API::getInstance()->getDefaultCurrency()
);
}

$row->setMetadata('previous_' . $columnName, $pastValue);
$row->setMetadata('periodName', $period->getLabel());
$row->setMetadata('previousRange', $period->getLocalizedShortString());
$pastValue = ($pastValue * $ratio);

if ($row->getMetadata('previousRange') === false || $row->getMetadata('periodName') === false) {
$period = $this->pastData->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX);
$row->setMetadata('periodName', $period->getLabel());
$row->setMetadata('previousRange', $period->getLocalizedShortString());
}

$pastValue = ($pastValue * $row->getMetadata('ratio'));

$dividend = $currentValue - $pastValue;
$divisor = $pastValue;
Expand Down

0 comments on commit ff14ebf

Please sign in to comment.