Skip to content

Commit

Permalink
Ensure column aggregation is able to handle columns by id and name
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Jan 13, 2025
1 parent 19e6c7b commit 38aafc0
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions core/DataTable/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,24 +483,7 @@ public function sumRow(Row $rowToSum, $enableCopyMetadata = true, $aggregationOp

$thisColumnValue = $this->getColumn($columnToSumName);

$operation = 'sum';
if ($operationsIsArray && isset($aggregationOperations[$columnToSumName])) {
$operationName = $aggregationOperations[$columnToSumName];
if (is_string($operationName)) {
$operation = strtolower($operationName);
} elseif (is_callable($operationName)) {
$operation = $operationName;
}
}

// max_actions is a core metric that is generated in ArchiveProcess_Day. Therefore, it can be
// present in any data table and is not part of the $aggregationOperations mechanism.
if ($columnToSumName == Metrics::INDEX_MAX_ACTIONS) {
$operation = 'max';
}
if (empty($operation)) {
throw new Exception("Unknown aggregation operation for column $columnToSumName.");
}
$operation = $this->getColumnAggregationOperation($columnToSumName, $aggregationOperations);

$newValue = $this->getColumnValuesMerged($operation, $thisColumnValue, $columnToSumValue, $this, $rowToSum, $columnToSumName);

Expand All @@ -512,6 +495,51 @@ public function sumRow(Row $rowToSum, $enableCopyMetadata = true, $aggregationOp
}
}

protected function getColumnAggregationOperation($columnNameOrId, $columnOperations): string
{
// max_actions is a core metric that is generated in ArchiveProcess_Day. Therefore, it can be
// present in any data table and is not part of the $aggregationOperations mechanism.
if ($columnNameOrId == Metrics::INDEX_MAX_ACTIONS) {
return 'max';
}

if (empty($columnOperations) || !is_array($columnOperations)) {
return 'sum';
}

$operationName = 'sum';

if (isset($aggregationOperations[$columnNameOrId])) {
$operationName = $aggregationOperations[$columnNameOrId];
} elseif (is_numeric($columnNameOrId)) {
// if the column was provided as index, check if a aggration is set for the real name
$metricsIdToName = Metrics::getMappingFromIdToName();
if (isset($metricsIdToName[$columnNameOrId]) && isset($aggregationOperations[$metricsIdToName[$columnNameOrId]])) {
$operationName = $aggregationOperations[$metricsIdToName[$columnNameOrId]];
}
} else {
// if the column was provided as string, check if a aggration is set for the index
$metricsNameToId = Metrics::getMappingFromNameToId();
if (isset($metricsNameToId[$columnNameOrId]) && isset($aggregationOperations[$metricsNameToId[$columnNameOrId]])) {
$operationName = $aggregationOperations[$metricsNameToId[$columnNameOrId]];
}
}

if (is_string($operationName)) {
return strtolower($operationName);
} elseif (is_callable($operationName)) {
return $operationName;
} elseif (!empty($operationName)) {
// log a debug message if set aggregation is invalid, but fall back to `sum`
StaticContainer::get(LoggerInterface::class)->debug(
'Invalid column aggregation set for {column}: {aggregation}',
['column' => $columnNameOrId, 'aggregation' => $operationName]
);
}

return 'sum';
}

/**
*/
private function getColumnValuesMerged($operation, $thisColumnValue, $columnToSumValue, $thisRow, $rowToSum, $columnName = null)
Expand Down

0 comments on commit 38aafc0

Please sign in to comment.