PropertyModifierSimpleGroupBy: new modifier

fixes #2317
This commit is contained in:
Thomas Gelf 2021-04-23 17:37:56 +02:00
parent c5e25cdcc7
commit e5595431df
3 changed files with 71 additions and 0 deletions

View File

@ -13,6 +13,7 @@ next (will be 1.9.0)
### Import and Sync
* FEATURE: introduce 'disable' as your purge action on Sync (#2285)
* FEATURE: there is now a simple "group by" Property Modifier (#2317)
### Configuration Baskets
* FEATURE: it's now possible to purge objects of specific types (#2201)

View File

@ -0,0 +1,68 @@
<?php
namespace Icinga\Module\Director\PropertyModifier;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;
class PropertyModifierSimpleGroupBy extends PropertyModifierHook
{
private $keptRows = [];
public function getName()
{
return mt('director', 'Group by a column, aggregate others');
}
public function requiresRow()
{
return true;
}
public function transform($value)
{
$row = $this->getRow();
$aggregationColumns = preg_split(
'/\s*,\s*/',
$this->getSetting('aggregation_columns'),
-1,
PREG_SPLIT_NO_EMPTY
);
if (isset($this->keptRows[$value])) {
foreach ($aggregationColumns as $column) {
if (isset($row->$column)) {
$this->keptRows[$value]->{$column} = array_unique(array_merge(
$this->keptRows[$value]->{$column},
[$row->$column]
));
sort($this->keptRows[$value]->{$column});
}
}
$this->rejectRow();
} else {
foreach ($aggregationColumns as $column) {
if (isset($row->$column)) {
$row->$column = [$row->$column];
} else {
$row->$column = [];
}
}
$this->keptRows[$value] = $row;
}
return $value;
}
public static function addSettingsFormFields(QuickForm $form)
{
$form->addElement('text', 'aggregation_columns', [
'label' => $form->translate('Aggregation Columns'),
'description' => $form->translate(
'Comma-separated list of columns that should be aggregated (transformed into an Array).'
. ' For all other columns only the first value will be kept.'
),
'required' => true,
]);
}
}

View File

@ -45,6 +45,7 @@ use Icinga\Module\Director\PropertyModifier\PropertyModifierRegexSplit;
use Icinga\Module\Director\PropertyModifier\PropertyModifierRejectOrSelect;
use Icinga\Module\Director\PropertyModifier\PropertyModifierRenameColumn;
use Icinga\Module\Director\PropertyModifier\PropertyModifierReplace;
use Icinga\Module\Director\PropertyModifier\PropertyModifierSimpleGroupBy;
use Icinga\Module\Director\PropertyModifier\PropertyModifierSkipDuplicates;
use Icinga\Module\Director\PropertyModifier\PropertyModifierSplit;
use Icinga\Module\Director\PropertyModifier\PropertyModifierStripDomain;
@ -115,6 +116,7 @@ $directorHooks = [
PropertyModifierRejectOrSelect::class,
PropertyModifierRenameColumn::class,
PropertyModifierReplace::class,
PropertyModifierSimpleGroupBy::class,
PropertyModifierSkipDuplicates::class,
PropertyModifierSplit::class,
PropertyModifierStripDomain::class,