diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Resolver/CommandUsage.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | library/Director/Resolver/CommandUsage.php | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/library/Director/Resolver/CommandUsage.php b/library/Director/Resolver/CommandUsage.php new file mode 100644 index 0000000..7e3e0c5 --- /dev/null +++ b/library/Director/Resolver/CommandUsage.php @@ -0,0 +1,104 @@ +<?php + +namespace Icinga\Module\Director\Resolver; + +use gipfl\IcingaWeb2\Link; +use gipfl\Translation\TranslationHelper; +use Icinga\Module\Director\Objects\IcingaCommand; +use InvalidArgumentException; + +class CommandUsage +{ + use TranslationHelper; + + /** @var IcingaCommand */ + protected $command; + + /** @var \Zend_Db_Adapter_Abstract */ + protected $db; + + /** + * CommandUsageTable constructor. + * @param IcingaCommand $command + */ + public function __construct(IcingaCommand $command) + { + if ($command->isTemplate()) { + throw new InvalidArgumentException( + 'CommandUsageTable expects object or external_object, got a template' + ); + } + + $this->command = $command; + $this->db = $command->getDb(); + } + + /** + * @return array + */ + public function getLinks() + { + $name = $this->command->getObjectName(); + $links = []; + $map = [ + 'host' => ['check_command', 'event_command'], + 'service' => ['check_command', 'event_command'], + 'notification' => ['command'], + ]; + $types = [ + 'host' => [ + 'object' => $this->translate('%d Host(s)'), + 'template' => $this->translate('%d Host Template(s)'), + ], + 'service' => [ + 'object' => $this->translate('%d Service(s)'), + 'template' => $this->translate('%d Service Template(s)'), + 'apply' => $this->translate('%d Service Apply Rule(s)'), + ], + 'notification' => [ + 'object' => $this->translate('%d Notification(s)'), + 'template' => $this->translate('%d Notification Template(s)'), + 'apply' => $this->translate('%d Notification Apply Rule(s)'), + ], + ]; + + $urlSuffix = [ + 'object' => '', + 'template' => '/templates', + 'apply' => '/applyrules', + ]; + + foreach ($map as $type => $relations) { + $res = $this->fetchFor($type, $relations, array_keys($types[$type])); + foreach ($types[$type] as $objectType => $caption) { + if ($res->$objectType > 0) { + $suffix = $urlSuffix[$objectType]; + $links[] = Link::create( + sprintf($caption, $res->$objectType), + "director/${type}s$suffix", + ['command' => $name] + ); + } + } + } + + return $links; + } + + protected function fetchFor($table, $rels, $objectTypes) + { + $id = $this->command->getAutoincId(); + + $columns = []; + foreach ($objectTypes as $type) { + $columns[$type] = "COALESCE(SUM(CASE WHEN object_type = '$type' THEN 1 ELSE 0 END), 0)"; + } + $query = $this->db->select()->from("icinga_$table", $columns); + + foreach ($rels as $rel) { + $query->orWhere("${rel}_id = ?", $id); + } + + return $this->db->fetchRow($query); + } +} |