From e99bb0d26c74b5844201dbd770e03622f1029632 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Thu, 19 Mar 2026 17:34:47 +0100 Subject: [PATCH] `BpConfig`: Add method `applyDbStates()` to clean-up redundant code --- application/clicommands/CleanupCommand.php | 13 +--------- application/clicommands/ProcessCommand.php | 13 +--------- application/controllers/NodeController.php | 13 +--------- application/controllers/ProcessController.php | 13 +--------- library/Businessprocess/BpConfig.php | 26 +++++++++++++------ .../Web/Component/Dashboard.php | 13 +--------- .../Web/Component/DashboardFullscreen.php | 14 +--------- .../Validator/HostServiceTermValidator.php | 9 +------ .../Renderer/ProcessesProblemsBadge.php | 13 +--------- 9 files changed, 26 insertions(+), 101 deletions(-) diff --git a/application/clicommands/CleanupCommand.php b/application/clicommands/CleanupCommand.php index 13f1475..03c6b8d 100644 --- a/application/clicommands/CleanupCommand.php +++ b/application/clicommands/CleanupCommand.php @@ -7,12 +7,8 @@ namespace Icinga\Module\Businessprocess\Clicommands; use Exception; use Icinga\Application\Logger; -use Icinga\Application\Modules\Module; use Icinga\Cli\Command; use Icinga\Module\Businessprocess\Modification\NodeRemoveAction; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; class CleanupCommand extends Command @@ -62,14 +58,7 @@ class CleanupCommand extends Command continue; } - if ( - Module::exists('icingadb') - && (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + $bp->applyDbStates(); $removedNodes = []; foreach (array_keys($bp->getMissingChildren()) as $missingNode) { diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php index 1c53a69..4cdb3ca 100644 --- a/application/clicommands/ProcessCommand.php +++ b/application/clicommands/ProcessCommand.php @@ -7,16 +7,12 @@ namespace Icinga\Module\Businessprocess\Clicommands; use Exception; use Icinga\Application\Logger; -use Icinga\Application\Modules\Module; use Icinga\Cli\Command; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\HostNode; use Icinga\Module\Businessprocess\Node; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\ServiceNode; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; class ProcessCommand extends Command @@ -139,14 +135,7 @@ class ProcessCommand extends Command try { /** @var BpNode $node */ $node = $bp->getNode($nodeName); - if ( - Module::exists('icingadb') - && (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + $bp->applyDbStates(); if ($bp->hasErrors()) { Logger::error("Checking Business Process '%s' failed: %s\n", $name, $bp->getErrors()); diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index f3bdb25..f3c6e1c 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -6,13 +6,9 @@ namespace Icinga\Module\Businessprocess\Controllers; use Exception; -use Icinga\Application\Modules\Module; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Renderer\Breadcrumb; use Icinga\Module\Businessprocess\Renderer\TileRenderer; use Icinga\Module\Businessprocess\Simulation; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Web\Controller; use Icinga\Module\Businessprocess\Web\Url; use ipl\Html\Html; @@ -98,14 +94,7 @@ class NodeController extends Controller continue; } - if ( - Module::exists('icingadb') && - (! $config->getBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($config); - } else { - MonitoringState::apply($config); - } + $config->applyDbStates(); $config->applySimulation($simulation); foreach ($parents as $parentAndPath) { diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 8479908..245a50e 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -5,21 +5,17 @@ namespace Icinga\Module\Businessprocess\Controllers; -use Icinga\Application\Modules\Module; use Icinga\Date\DateFormatter; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\Forms\AddNodeForm; use Icinga\Module\Businessprocess\Forms\EditNodeForm; use Icinga\Module\Businessprocess\Node; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Renderer\Breadcrumb; use Icinga\Module\Businessprocess\Renderer\Renderer; use Icinga\Module\Businessprocess\Renderer\TileRenderer; use Icinga\Module\Businessprocess\Renderer\TreeRenderer; use Icinga\Module\Businessprocess\Simulation; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\ConfigDiff; use Icinga\Module\Businessprocess\Storage\LegacyConfigRenderer; use Icinga\Module\Businessprocess\Web\Component\ActionBar; @@ -97,14 +93,7 @@ class ProcessController extends Controller $bp = $this->loadModifiedBpConfig(); $node = $this->getNode($bp); - if ( - Module::exists('icingadb') && - (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + $bp->applyDbStates(); $this->handleSimulations($bp); diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index 548d09d..57d1da3 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -157,6 +157,23 @@ class BpConfig return $this->metadata; } + /** + * Apply database states + * + * @return void + */ + public function applyDbStates(): void + { + if ( + Module::exists('icingadb') + && (! $this->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) + ) { + IcingaDbState::apply($this); + } else { + MonitoringState::apply($this); + } + } + /** * Set metadata * @@ -179,14 +196,7 @@ class BpConfig */ public function applyChanges(ProcessChanges $changes) { - if ( - Module::exists('icingadb') - && (! $this->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($this); - } else { - MonitoringState::apply($this); - } + $this->applyDbStates(); $cnt = 0; foreach ($changes->getChanges() as $change) { diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index 400b40c..fa8fc31 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -6,12 +6,8 @@ namespace Icinga\Module\Businessprocess\Web\Component; use Exception; -use Icinga\Application\Modules\Module; use Icinga\Authentication\Auth; use Icinga\Module\Businessprocess\BpConfig; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\Storage; use ipl\Html\BaseHtmlElement; use ipl\Html\Html; @@ -112,14 +108,7 @@ class Dashboard extends BaseHtmlElement continue; } - if ( - Module::exists('icingadb') && - (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + $bp->applyDbStates(); $this->add(new BpDashboardTile( $bp, diff --git a/library/Businessprocess/Web/Component/DashboardFullscreen.php b/library/Businessprocess/Web/Component/DashboardFullscreen.php index f4d68d6..19f5db7 100644 --- a/library/Businessprocess/Web/Component/DashboardFullscreen.php +++ b/library/Businessprocess/Web/Component/DashboardFullscreen.php @@ -6,12 +6,7 @@ namespace Icinga\Module\Businessprocess\Web\Component; use Exception; -use Icinga\Application\Modules\Module; -use Icinga\Authentication\Auth; use Icinga\Module\Businessprocess\BpConfig; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\Storage; use ipl\Html\BaseHtmlElement; use ipl\Html\Html; @@ -69,14 +64,7 @@ class DashboardFullscreen extends BaseHtmlElement continue; } - if ( - Module::exists('icingadb') && - (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + $bp->applyDbStates(); $this->add(new BpDashboardFullscreenTile( $bp, diff --git a/library/Businessprocess/Web/Form/Validator/HostServiceTermValidator.php b/library/Businessprocess/Web/Form/Validator/HostServiceTermValidator.php index 7af4ad7..bca617f 100644 --- a/library/Businessprocess/Web/Form/Validator/HostServiceTermValidator.php +++ b/library/Businessprocess/Web/Form/Validator/HostServiceTermValidator.php @@ -8,9 +8,6 @@ namespace Icinga\Module\Businessprocess\Web\Form\Validator; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\ServiceNode; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; -use Icinga\Module\Monitoring\Backend\MonitoringBackend; use ipl\I18n\Translation; use ipl\Validator\BaseValidator; use ipl\Web\FormElement\TermInput\Term; @@ -72,11 +69,7 @@ class HostServiceTermValidator extends BaseValidator } } - if ($testConfig->getBackend() instanceof MonitoringBackend) { - MonitoringState::apply($testConfig); - } else { - IcingaDbState::apply($testConfig); - } + $testConfig->applyDbStates(); foreach ($terms as $term) { /** @var Term $term */ diff --git a/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php b/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php index 2212a1e..b19b8b7 100644 --- a/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php +++ b/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php @@ -6,11 +6,7 @@ namespace Icinga\Module\Businessprocess\Web\Navigation\Renderer; use Icinga\Application\Logger; -use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\Node; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; -use Icinga\Module\Businessprocess\State\IcingaDbState; -use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer; use Throwable; @@ -35,14 +31,7 @@ class ProcessesProblemsBadge extends BadgeNavigationItemRenderer foreach ($storage->listProcessNames() as $processName) { $bp = $storage->loadProcess($processName); - if ( - Module::exists('icingadb') && - (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + $bp->applyDbStates(); foreach ($bp->getRootNodes() as $rootNode) { $nodeState = $rootNode->getState();