Skip to content
Extraits de code Groupes Projets
Valider ea9d961a rédigé par Thomas Steur's avatar Thomas Steur Validation de GitHub
Parcourir les fichiers

Add possibility to specify a custom row aggregation operation (#10288)

* Add possibility to specify a custom row aggregation operation

* added test for row metadata aggregations
parent fcea4514
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -461,7 +461,11 @@ class Row implements \ArrayAccess, \IteratorAggregate ...@@ -461,7 +461,11 @@ class Row implements \ArrayAccess, \IteratorAggregate
$operation = 'sum'; $operation = 'sum';
if (is_array($aggregationOperations) && isset($aggregationOperations[$columnToSumName])) { if (is_array($aggregationOperations) && isset($aggregationOperations[$columnToSumName])) {
$operation = strtolower($aggregationOperations[$columnToSumName]); if (is_string($aggregationOperations[$columnToSumName])) {
$operation = strtolower($aggregationOperations[$columnToSumName]);
} elseif (is_callable($aggregationOperations[$columnToSumName])) {
$operation = $aggregationOperations[$columnToSumName];
}
} }
// max_actions is a core metric that is generated in ArchiveProcess_Day. Therefore, it can be // max_actions is a core metric that is generated in ArchiveProcess_Day. Therefore, it can be
...@@ -520,6 +524,10 @@ class Row implements \ArrayAccess, \IteratorAggregate ...@@ -520,6 +524,10 @@ class Row implements \ArrayAccess, \IteratorAggregate
$newValue = $thisColumnValue; $newValue = $thisColumnValue;
break; break;
default: default:
if (is_callable($operation)) {
return call_user_func($operation, $thisColumnValue, $columnToSumValue);
}
throw new Exception("Unknown operation '$operation'."); throw new Exception("Unknown operation '$operation'.");
} }
return $newValue; return $newValue;
......
...@@ -308,6 +308,58 @@ class DataTableTest extends \PHPUnit_Framework_TestCase ...@@ -308,6 +308,58 @@ class DataTableTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($subtable, $row1->getIdSubDataTable()); $this->assertEquals($subtable, $row1->getIdSubDataTable());
} }
public function testSumRowMetadata_CustomAggregationOperation()
{
$metadata1 = array('mytest' => 'value1');
$metadata2 = array('mytest' => 'value2');
$row1 = new Row(array(Row::COLUMNS => array('test_int' => 145), Row::METADATA => $metadata1));
$finalRow = new Row(array(Row::COLUMNS => array('test_int' => 5), Row::METADATA => $metadata2));
$finalRow->sumRowMetadata($row1, array('mytest' => function ($thisValue, $otherValue) {
if (!is_array($thisValue)) {
$thisValue = array($thisValue);
}
$thisValue[] = $otherValue;
return $thisValue;
}));
$this->assertEquals(array('value2', 'value1'), $finalRow->getMetadata('mytest'));
}
public function testSumRow_CustomAggregationOperation()
{
$columns = array('test_int' => 145, 'test_float' => 145.5);
$row1 = new Row(array(Row::COLUMNS => $columns));
$columns2 = array('test_int' => 5);
$finalRow = new Row(array(Row::COLUMNS => $columns2));
$finalRow->sumRow($row1, $copyMetadata = true, $operation = array('test_int' => function ($thisValue, $otherValue) {
if (!is_array($thisValue)) {
$thisValue = array($thisValue);
}
$thisValue[] = $otherValue;
return $thisValue;
}));
$this->assertEquals(array(5, 145), $finalRow->getColumn('test_int'));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Unknown operation 'foobarinvalid'
*/
public function testSumRow_ShouldThrowExceptionIfInvalidOperationIsGiven()
{
$row1 = new Row(array(Row::COLUMNS => array('test_int' => 145)));
$finalRow = new Row(array(Row::COLUMNS => array('test_int' => 5)));
$finalRow->sumRow($row1, $copyMetadata = true, $operation = array('test_int' => 'fooBarInvalid'));
$this->assertEquals(array(5, 145), $finalRow->getColumn('test_int'));
}
public function unserializeTestsDataProvider() public function unserializeTestsDataProvider()
{ {
return array( return array(
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter