Skip to content

Commit 5ebb069

Browse files
committed
Merge branch 'feature/configure-rule-modifiers'
2 parents 6f871d4 + 7488d08 commit 5ebb069

37 files changed

+1325
-68
lines changed

application/controllers/ConfigurationController.php

Lines changed: 166 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@
22

33
namespace Icinga\Module\Eventtracker\Controllers;
44

5+
use ECSPrefix202509\React\Dns\Model\Message;
56
use Exception;
67
use gipfl\IcingaWeb2\Link;
78
use gipfl\IcingaWeb2\Url;
89
use gipfl\Json\JsonDecodeException;
910
use gipfl\Json\JsonString;
11+
use gipfl\Web\InlineForm;
1012
use gipfl\Web\Widget\Hint;
1113
use gipfl\ZfDbStore\DbStorableInterface;
1214
use gipfl\ZfDbStore\NotFoundError;
1315
use gipfl\ZfDbStore\ZfDbStore;
16+
use Icinga\Module\Eventtracker\Data\PlainObjectRenderer;
1417
use Icinga\Module\Eventtracker\Db\ConfigStore;
1518
use Icinga\Module\Eventtracker\Engine\Downtime\DowntimeRule;
1619
use Icinga\Module\Eventtracker\Engine\Input\KafkaInput;
20+
use Icinga\Module\Eventtracker\Modifier\Modifier;
1721
use Icinga\Module\Eventtracker\Modifier\ModifierChain;
22+
use Icinga\Module\Eventtracker\Modifier\ModifierRegistry;
23+
use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore;
1824
use Icinga\Module\Eventtracker\Modifier\Settings;
1925
use Icinga\Module\Eventtracker\Web\Dashboard\ConfigurationDashboard;
26+
use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm;
2027
use Icinga\Module\Eventtracker\Web\Form\DowntimeForm;
28+
use Icinga\Module\Eventtracker\Web\Form\SimulateRuleForm;
2129
use Icinga\Module\Eventtracker\Web\Form\UuidObjectForm;
2230
use Icinga\Module\Eventtracker\Web\Table\BaseTable;
2331
use Icinga\Module\Eventtracker\Web\Table\ChannelRulesTable;
@@ -27,6 +35,7 @@
2735
use Icinga\Module\Eventtracker\Web\WebAction;
2836
use Icinga\Module\Eventtracker\Web\WebActions;
2937
use Icinga\Web\Notification;
38+
use Icinga\Web\Session\SessionNamespace;
3039
use ipl\Html\Html;
3140
use ipl\Html\Table;
3241
use Ramsey\Uuid\Uuid;
@@ -148,23 +157,107 @@ public function channelAction()
148157
} else {
149158
$this->addObjectTab($action);
150159
}
151-
$this->content()->add($this->getForm($action));
160+
$this->content()->add($this->getForm($action, function () {
161+
if ($uuid = $this->getUuid()) {
162+
$ns = $this->Window()->getSessionNamespace('eventtracker');
163+
$sessionKey = 'channelrules/' . $uuid->toString();
164+
$ns->delete($sessionKey);
165+
}
166+
}));
152167
}
153168

154169
public function channelrulesAction()
155170
{
156171
$this->notForApi();
157172
$action = $this->actions->get('channels');
158173
$this->channelTabs()->activate('rules');
159-
$this->actions()->add(Link::create($this->translate('Edit'), 'TODO', [
160-
'what' => 'ever'
161-
], [
162-
'class' => 'icon-edit'
163-
]));
174+
$this->actions()->add(Link::create(
175+
$this->translate('Add Modifier'),
176+
'eventtracker/configuration/channelRules/',
177+
[
178+
'uuid' => $this->requireUuid(),
179+
'action' => 'add'
180+
],
181+
[
182+
'class' => 'icon-plus'
183+
]
184+
));
185+
186+
$uuid = $this->requireUuid();
187+
$ns = $this->Window()->getSessionNamespace('eventtracker');
188+
$sessionKey = 'simulationObject/'. $uuid->toString();
189+
164190
$form = $this->getForm($action); // TODO: w/o form
165-
if ($rules = $form->getElementValue('rules')) {
166-
$this->showRules($this->requireUuid(), $rules);
191+
$modifierRuleStore = new ModifierRuleStore($ns, $uuid, $form);
192+
if ($this->params->get('action') === 'add') {
193+
$ruleForm = new ChannelRuleForm($modifierRuleStore);
194+
$ruleForm->on($ruleForm::ON_SUCCESS, function () use ($ruleForm, $modifierRuleStore) {
195+
$rules = $modifierRuleStore->getRules();
196+
$rules->addModifier($ruleForm->getModifier(), $ruleForm->getPropertyName());
197+
$modifierRuleStore->setModifierRules($rules);
198+
$this->redirectNow($this->url()->getUrlWithout('action'));
199+
});
200+
$ruleForm->handleRequest($this->getServerRequest());
201+
if ($ruleForm->hasBeenCancelled()) {
202+
$this->redirectNow($this->url()->getUrlWithout('action'));
203+
}
204+
$this->content()->add($ruleForm);
205+
}
206+
if ($ns->get($sessionKey) !== null) {
207+
$this->actions()->add(Link::create(
208+
$this->translate('Stop Simulation'),
209+
'eventtracker/configuration/channelRules',
210+
[
211+
'uuid' => $this->requireUuid(),
212+
'action' => 'stop_simulation'
213+
]
214+
));
215+
} elseif ($this->params->get('action') === 'load_simulation') {
216+
$simulationForm = new SimulateRuleForm($ns, $sessionKey);
217+
$simulationForm->on($simulationForm::ON_SUCCESS, function () {
218+
$this->redirectNow($this->url()->getUrlWithout('action'));
219+
});
220+
$simulationForm->handleRequest($this->getServerRequest());
221+
if ($simulationForm->hasBeenCancelled()) {
222+
$this->redirectNow($this->url()->getUrlWithout('action'));
223+
}
224+
225+
$this->content()->add($simulationForm);
226+
}
227+
if ($this->params->get('action') === 'edit') {
228+
var_dump("foobar");
229+
$ruleForm = new ChannelRuleForm($modifierRuleStore);
230+
$ruleForm->editRow(
231+
$this->url()->getParam('row'),
232+
$this->url()->getParam('checksum')
233+
);
234+
$ruleForm->on($ruleForm::ON_SUCCESS, function () {
235+
$this->redirectNow($this->url()->getUrlWithout('action'));
236+
});
237+
$ruleForm->handleRequest($this->getServerRequest());
238+
if ($ruleForm->hasBeenCancelled()) {
239+
$this->redirectNow($this->url()->getUrlWithout('action'));
240+
}
241+
242+
$this->content()->add($ruleForm);
243+
} else {
244+
$this->actions()->add(Link::create(
245+
$this->translate('Load Simulation'),
246+
'eventtracker/configuration/channelRules/',
247+
[
248+
'uuid' => $this->requireUuid(),
249+
'action' => 'load_simulation',
250+
],
251+
[
252+
'class' => 'icon-magic'
253+
]
254+
));
255+
}
256+
if ($this->params->get('action') === 'stop_simulation') {
257+
$ns->delete($sessionKey);
258+
$this->redirectNow($this->url()->getUrlWithout('action'));
167259
}
260+
$this->showRules($modifierRuleStore, $ns, $uuid, $form);
168261
}
169262

170263
public function channelruleAction()
@@ -195,11 +288,11 @@ protected function channelTabs()
195288
'label' => $this->translate('Channel Configuration'),
196289
'url' => 'eventtracker/configuration/channel',
197290
'urlParams' => $params,
198-
])/*->add('rules', [
291+
])->add('rules', [
199292
'label' => $this->translate('Rules'),
200293
'url' => 'eventtracker/configuration/channelrules',
201294
'urlParams' => $params,
202-
])*/;
295+
]);
203296
}
204297

205298

@@ -593,17 +686,73 @@ protected function putHostListMembers()
593686
$this->sendJsonSuccess(['message' => sprintf('updated %s hosts', $cnt)], 201);
594687
}
595688

596-
protected function showRules(UuidInterface $uuid, $rules)
597-
{
598-
try {
599-
$modifiers = ModifierChain::fromSerialization(JsonString::decode($rules));
600-
} catch (JsonDecodeException $e) {
689+
protected function showRules(
690+
ModifierRuleStore $modifierRuleStore,
691+
SessionNamespace $ns,
692+
UuidInterface $uuid,
693+
UuidObjectForm $form
694+
) {
695+
$form->addElement('submit', 'add_modifier', []);
696+
697+
if ($modifierRuleStore->getSessionRules() !== null) {
698+
if ($modifierRuleStore->hasBeenModified()) {
699+
$newForm = new InlineForm();
700+
$newForm->on($newForm::ON_SUCCESS, function () use ($form, $modifierRuleStore) {
701+
$form->populate(['rules' => JsonString::encode($modifierRuleStore->getRules())]);
702+
$form->storeObject();
703+
$modifierRuleStore->deleteSessionRules();
704+
$this->redirectNow($this->url());
705+
});
706+
$newForm->handleRequest($this->getServerRequest());
707+
$newForm->addElement('submit', 'submit', ['label' => $this->translate('Save')]);
708+
$message = Hint::warning(
709+
'The order has been modified please safe if you want to preserver the new order'
710+
);
711+
712+
//if ($newForm->hasBeenSubmitted()) {
713+
// $form->populate(['rules' => JsonString::encode($modifierRuleStore->getRules())]);
714+
// $form->storeObject();
715+
// $modifierRuleStore->deleteSessionRules();
716+
// $this->redirectNow($this->url());
717+
//}
718+
$this->content()->add([$message, $newForm]);
719+
}
720+
}
721+
if ($modifierRuleStore->getRules() === null) {
601722
return;
602723
}
724+
$modifiers = $modifierRuleStore->getRules();
603725
$url = Url::fromPath('eventtracker/configuration/channelrule', [
604726
'uuid' => $uuid->toString()
605727
]);
606-
$info = new ChannelRulesTable($modifiers, $url, $this->getServerRequest());
728+
try {
729+
if ($sampleObject = $ns->get('simulationObject/'. $uuid->toString())) {
730+
$this->content()->add([
731+
Html::tag('h3', "Original Event"),
732+
Html::tag('pre', [
733+
'class' => 'plain-object'
734+
], PlainObjectRenderer::render($sampleObject)),
735+
]);
736+
}
737+
$info = new ChannelRulesTable(
738+
$modifiers,
739+
$url,
740+
$this->getServerRequest(),
741+
$modifierRuleStore,
742+
$sampleObject,
743+
);
744+
if ($info->hasModifications()) {
745+
$this->redirectNow($this->url());
746+
}
747+
} catch (\Throwable $e) {
748+
$info = [
749+
Hint::error($e),
750+
Html::tag('pre', $modifierRuleStore->getRules()),
751+
Html::tag('pre', $modifierRuleStore->getRules(), JSON_PRETTY_PRINT),
752+
];
753+
}
754+
755+
607756
$this->content()->add([
608757
Html::tag('h3', $this->translate('Configured Rules')),
609758
$info
@@ -684,6 +833,7 @@ protected function createForm(WebAction $action, ?callable $onSuccess): UuidObje
684833
} else {
685834
$form = new $formClass($store);
686835
}
836+
/** @var UuidObjectForm */
687837
if ($onSuccess) {
688838
$form->on($form::ON_SUCCESS, $onSuccess);
689839
}

library/Eventtracker/Modifier/BaseModifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Icinga\Module\Eventtracker\Modifier;
44

5-
use Icinga\Module\Eventtracker\Web\Form;
5+
use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm;
66
use ipl\Html\ValidHtml;
77

88
use function array_pop;
@@ -60,7 +60,7 @@ public function transform($object, string $propertyName)
6060
return $this->simpleTransform($value);
6161
}
6262

63-
public static function extendSettingsForm(Form $form): void
63+
public static function extendSettingsForm(ChannelRuleForm $form): void
6464
{
6565
}
6666

library/Eventtracker/Modifier/ClassInventoryLookup.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ class ClassInventoryLookup extends BaseModifier
1111

1212
protected $XXinstanceDescriptionPattern = 'Transforms...';
1313

14-
/** @var ObjectClassInventory */
15-
protected $classes;
14+
protected ?ObjectClassInventory $classes = null;
1615

17-
/**
18-
* @return ObjectClassInventory
19-
*/
20-
protected function classes()
16+
protected function classes(): ObjectClassInventory
2117
{
2218
if ($this->classes === null) {
2319
// TODO: inject DB. requiredResources?

library/Eventtracker/Modifier/CloneProperty.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Icinga\Module\Eventtracker\Modifier;
44

5+
use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm;
6+
use ipl\Html\Html;
7+
58
class CloneProperty extends BaseModifier
69
{
710
protected static ?string $name = 'Clone Property';
@@ -18,4 +21,16 @@ public function transform($object, string $propertyName)
1821

1922
return $currentValue;
2023
}
24+
25+
public static function extendSettingsForm(ChannelRuleForm $form): void
26+
{
27+
$form->addElement('text', 'target_property', [
28+
'label' => $form->translate('Target Property'),
29+
'required' => false,
30+
'description' => Html::sprintf($form->translate(
31+
'The given property will be cloned into this target property.'
32+
. ' You can use %s to access nested properties.'
33+
), Html::tag('strong', 'attributes.some.value'))
34+
]);
35+
}
2136
}

library/Eventtracker/Modifier/CombineProperties.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Icinga\Module\Eventtracker\Modifier;
44

5-
use Icinga\Module\Eventtracker\Web\Form;
5+
use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm;
6+
use ipl\Html\Html;
67

78
class CombineProperties extends BaseModifier
89
{
@@ -13,18 +14,18 @@ public function transform($object, string $propertyName)
1314
return ObjectUtils::fillVariables($this->settings->getRequired('pattern'), $object);
1415
}
1516

16-
public static function extendSettingsForm(Form $form): void
17+
public static function extendSettingsForm(ChannelRuleForm $form): void
1718
{
1819
$form->addElement('text', 'pattern', [
1920
'label' => $form->translate('Pattern'),
2021
'required' => false,
21-
'description' => $form->translate(
22-
'This pattern will be evaluated, and variables like ${some_column}'
22+
'description' => Html::sprintf($form->translate(
23+
'This pattern will be evaluated, and variables like %s'
2324
. ' will be filled accordingly. A typical use-case is generating'
24-
. ' unique service identifiers via ${host}!${service} in case your'
25+
. ' unique service identifiers via %s in case your'
2526
. ' data source doesn\'t allow you to ship such. The chosen "property"'
2627
. ' has no effect here and will be ignored.'
27-
)
28+
), Html::tag('strong', '${some_column}'), Html::tag('strong', '${host}!${service}')),
2829
]);
2930
}
3031
}

library/Eventtracker/Modifier/ConfiguredMapLookup.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use gipfl\Json\JsonString;
66
use Icinga\Application\Config;
7+
use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm;
78
use ipl\Html\Html;
89
use ipl\Html\ValidHtml;
910
use RuntimeException;
@@ -13,7 +14,7 @@
1314
*/
1415
class ConfiguredMapLookup extends MapLookup
1516
{
16-
protected static ?string $name = 'Lookup (and map) values via a configured Map (deprecated)';
17+
protected static ?string $name = 'Lookup (and map) values via a configured Map (DEPRECATED)';
1718

1819
protected $map;
1920

@@ -43,9 +44,17 @@ protected function loadMap()
4344
public function describe(string $propertyName): ValidHtml
4445
{
4546
return Html::sprintf(
46-
'Lookup %s in the Map "%s"',
47+
'DEPRECATED Lookup %s in the Map "%s"',
4748
Html::tag('strong', $propertyName),
4849
Html::tag('strong', $this->settings->getRequired('map_name'))
4950
);
5051
}
52+
public static function extendSettingsForm(ChannelRuleForm $form): void
53+
{
54+
$form->addElement('text', 'map_name', [
55+
'label' => 'Map name',
56+
'required' => true,
57+
'description' => 'Name of the map file that is stored under /etc/icingaweb2/modules/eventtracker/maps/'
58+
]);
59+
}
5160
}

0 commit comments

Comments
 (0)