|
2 | 2 |
|
3 | 3 | namespace Icinga\Module\Eventtracker\Controllers; |
4 | 4 |
|
| 5 | +use ECSPrefix202509\React\Dns\Model\Message; |
5 | 6 | use Exception; |
6 | 7 | use gipfl\IcingaWeb2\Link; |
7 | 8 | use gipfl\IcingaWeb2\Url; |
8 | 9 | use gipfl\Json\JsonDecodeException; |
9 | 10 | use gipfl\Json\JsonString; |
| 11 | +use gipfl\Web\InlineForm; |
10 | 12 | use gipfl\Web\Widget\Hint; |
11 | 13 | use gipfl\ZfDbStore\DbStorableInterface; |
12 | 14 | use gipfl\ZfDbStore\NotFoundError; |
13 | 15 | use gipfl\ZfDbStore\ZfDbStore; |
| 16 | +use Icinga\Module\Eventtracker\Data\PlainObjectRenderer; |
14 | 17 | use Icinga\Module\Eventtracker\Db\ConfigStore; |
15 | 18 | use Icinga\Module\Eventtracker\Engine\Downtime\DowntimeRule; |
16 | 19 | use Icinga\Module\Eventtracker\Engine\Input\KafkaInput; |
| 20 | +use Icinga\Module\Eventtracker\Modifier\Modifier; |
17 | 21 | use Icinga\Module\Eventtracker\Modifier\ModifierChain; |
| 22 | +use Icinga\Module\Eventtracker\Modifier\ModifierRegistry; |
| 23 | +use Icinga\Module\Eventtracker\Modifier\ModifierRuleStore; |
18 | 24 | use Icinga\Module\Eventtracker\Modifier\Settings; |
19 | 25 | use Icinga\Module\Eventtracker\Web\Dashboard\ConfigurationDashboard; |
| 26 | +use Icinga\Module\Eventtracker\Web\Form\ChannelRuleForm; |
20 | 27 | use Icinga\Module\Eventtracker\Web\Form\DowntimeForm; |
| 28 | +use Icinga\Module\Eventtracker\Web\Form\SimulateRuleForm; |
21 | 29 | use Icinga\Module\Eventtracker\Web\Form\UuidObjectForm; |
22 | 30 | use Icinga\Module\Eventtracker\Web\Table\BaseTable; |
23 | 31 | use Icinga\Module\Eventtracker\Web\Table\ChannelRulesTable; |
|
27 | 35 | use Icinga\Module\Eventtracker\Web\WebAction; |
28 | 36 | use Icinga\Module\Eventtracker\Web\WebActions; |
29 | 37 | use Icinga\Web\Notification; |
| 38 | +use Icinga\Web\Session\SessionNamespace; |
30 | 39 | use ipl\Html\Html; |
31 | 40 | use ipl\Html\Table; |
32 | 41 | use Ramsey\Uuid\Uuid; |
@@ -148,23 +157,107 @@ public function channelAction() |
148 | 157 | } else { |
149 | 158 | $this->addObjectTab($action); |
150 | 159 | } |
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 | + })); |
152 | 167 | } |
153 | 168 |
|
154 | 169 | public function channelrulesAction() |
155 | 170 | { |
156 | 171 | $this->notForApi(); |
157 | 172 | $action = $this->actions->get('channels'); |
158 | 173 | $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 | + |
164 | 190 | $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')); |
167 | 259 | } |
| 260 | + $this->showRules($modifierRuleStore, $ns, $uuid, $form); |
168 | 261 | } |
169 | 262 |
|
170 | 263 | public function channelruleAction() |
@@ -195,11 +288,11 @@ protected function channelTabs() |
195 | 288 | 'label' => $this->translate('Channel Configuration'), |
196 | 289 | 'url' => 'eventtracker/configuration/channel', |
197 | 290 | 'urlParams' => $params, |
198 | | - ])/*->add('rules', [ |
| 291 | + ])->add('rules', [ |
199 | 292 | 'label' => $this->translate('Rules'), |
200 | 293 | 'url' => 'eventtracker/configuration/channelrules', |
201 | 294 | 'urlParams' => $params, |
202 | | - ])*/; |
| 295 | + ]); |
203 | 296 | } |
204 | 297 |
|
205 | 298 |
|
@@ -593,17 +686,73 @@ protected function putHostListMembers() |
593 | 686 | $this->sendJsonSuccess(['message' => sprintf('updated %s hosts', $cnt)], 201); |
594 | 687 | } |
595 | 688 |
|
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) { |
601 | 722 | return; |
602 | 723 | } |
| 724 | + $modifiers = $modifierRuleStore->getRules(); |
603 | 725 | $url = Url::fromPath('eventtracker/configuration/channelrule', [ |
604 | 726 | 'uuid' => $uuid->toString() |
605 | 727 | ]); |
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 | + |
607 | 756 | $this->content()->add([ |
608 | 757 | Html::tag('h3', $this->translate('Configured Rules')), |
609 | 758 | $info |
@@ -684,6 +833,7 @@ protected function createForm(WebAction $action, ?callable $onSuccess): UuidObje |
684 | 833 | } else { |
685 | 834 | $form = new $formClass($store); |
686 | 835 | } |
| 836 | + /** @var UuidObjectForm */ |
687 | 837 | if ($onSuccess) { |
688 | 838 | $form->on($form::ON_SUCCESS, $onSuccess); |
689 | 839 | } |
|
0 commit comments