From 9d07548ba72f10fb53602befbf0308d5cf994baf Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 23 Jun 2021 07:54:10 +0200 Subject: [PATCH] Add new Host from Filter and Service from Filter in AddNodeForm.php Refs #300 --- application/forms/AddNodeForm.php | 116 ++++++++++++++++++ .../MonitoringRestrictions.php | 8 ++ 2 files changed, 124 insertions(+) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index df9fe662..a5e25ba9 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -65,6 +65,12 @@ public function setup() case 'new-process': $this->addNewProcess(); break; + case 'hosts_from_filter': + $this->selectHostsFromFilter(); + break; + case 'services_from_filter': + $this->selectServicesFromFilter(); + break; case null: $this->setSubmitLabel($this->translate('Next')); return; @@ -157,6 +163,8 @@ protected function selectNodeType() if ($this->hasParentNode()) { $types['host'] = $this->translate('Host'); $types['service'] = $this->translate('Service'); + $types['hosts_from_filter'] = $this->translate('Hosts from filter'); + $types['services_from_filter'] = $this->translate('Services from filter'); } elseif (! $this->hasProcesses()) { $this->addElement('hidden', 'node_type', array( 'ignore' => true, @@ -248,6 +256,53 @@ protected function addServicesElement($host) ]); } + protected function addFilteredHostsElement($filter) + { + $this->addElement('submit', 'refresh', [ + 'label' => $this->translate('Refresh'), + 'class' => 'refresh-filter' + ]); + $this->addElement('multiselect', 'children', [ + 'label' => $this->translate('Hosts'), + 'required' => true, + 'size' => 8, + 'style' => 'width: 25em', + 'multiOptions' => $this->enumHostListByFilter($filter), + 'description' => $this->translate( + 'Hosts that should be part of this business process node' + ), + 'validators' => [[new NoDuplicateChildrenValidator($this, $this->bp, $this->parent), true]] + ]); + } + + protected function addFilteredServicesElement($filter) + { + $this->addElement('submit', 'refresh', [ + 'label' => $this->translate('Refresh'), + 'class' => 'refresh-filter' + ]); + $this->addElement('multiselect', 'children', [ + 'label' => $this->translate('Services'), + 'required' => true, + 'size' => 8, + 'style' => 'width: 25em', + 'multiOptions' => $this->enumServiceListByFilter($filter), + 'description' => $this->translate( + 'Services that should be part of this business process node' + ), + 'validators' => [[new NoDuplicateChildrenValidator($this, $this->bp, $this->parent), true]] + ]); + } + + protected function addFilterElement() + { + $this->addElement('text', 'filter', array( + 'label' => $this->translate('Filter'), + 'required' => true, + 'ignore' => true + )); + } + protected function addFileElement() { $this->addElement('select', 'file', [ @@ -301,6 +356,26 @@ protected function addServiceOverrideElement() ]); } + protected function selectHostsFromFilter() + { + $this->addFilterElement(); + if ($filter = $this->getSentValue('filter')) { + $this->addFilteredHostsElement($filter); + } else { + $this->setSubmitLabel($this->translate('Next')); + } + } + + protected function selectServicesFromFilter() + { + $this->addFilterElement(); + if ($filter = $this->getSentValue('filter')) { + $this->addFilteredServicesElement($filter); + } else { + $this->setSubmitLabel($this->translate('Next')); + } + } + protected function selectProcess() { if ($this->hasParentNode()) { @@ -463,6 +538,45 @@ protected function enumServiceStateList() return $serviceStateList; } + protected function enumHostListByFilter($filter) + { + $names = $this->backend + ->select() + ->from('hostStatus', ['hostname' => 'host_name']) + ->applyFilter($this->getCustomFilterWithRestriction('monitoring/filter/objects', $filter)) + ->order('host_name') + ->getQuery() + ->fetchColumn(); + + // fetchPairs doesn't seem to work when using the same column with + // different aliases twice + $res = array(); + $suffix = ';Hoststatus'; + foreach ($names as $name) { + $res[$name . $suffix] = $name; + } + + return $res; + } + + protected function enumServiceListByFilter($filter) + { + $objects = $this->backend + ->select() + ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) + ->applyFilter($this->getCustomFilterWithRestriction('monitoring/filter/objects', $filter)) + ->order('service_description') + ->getQuery() + ->fetchAll(); + + $services = array(); + foreach ($objects as $object) { + $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; + } + + return $services; + } + protected function hasProcesses() { return count($this->enumProcesses()) > 0; @@ -552,6 +666,8 @@ public function onSuccess() // Fallthrough case 'process': + case 'hosts_from_filter': + case 'services_from_filter': if ($this->hasParentNode()) { $changes->addChildrenToNode($this->getValue('children'), $this->parent); } else { diff --git a/library/Businessprocess/MonitoringRestrictions.php b/library/Businessprocess/MonitoringRestrictions.php index 4dd4caa9..1efbe735 100644 --- a/library/Businessprocess/MonitoringRestrictions.php +++ b/library/Businessprocess/MonitoringRestrictions.php @@ -62,4 +62,12 @@ function ($c) { return $restriction; } + + public function getCustomFilterWithRestriction($restrictionName, $filterQueryString) { + $filter = Filter::matchAll(); + $filter->addFilter(Filter::fromQueryString($filterQueryString)); + $filter->addFilter($this->getRestriction($restrictionName)); + + return $filter; + } }