From ca33e71f2d62ca6419787578efe71284366d867a Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Fri, 4 Sep 2015 13:02:57 +0200 Subject: [PATCH] Deduplicate and clean up service summary component Remove redundant component view script and streamline subFilter handling in all views that render it. Remove heading from component to allow embedding in different types of views. refs #10033 --- library/Icinga/Web/Url.php | 22 ++- .../views/scripts/host/services.phtml | 10 +- .../application/views/scripts/host/show.phtml | 10 +- .../views/scripts/hosts/show.phtml | 4 +- .../list/components/hostssummary.phtml | 29 ++-- .../list/components/servicesummary.phtml | 26 ++- .../views/scripts/list/hosts.phtml | 4 +- .../views/scripts/list/services.phtml | 4 +- .../command/object-command-form.phtml | 10 +- .../command/objects-command-form.phtml | 10 +- .../partials/host/servicesummary.phtml | 150 ------------------ .../views/scripts/services/show.phtml | 5 +- 12 files changed, 91 insertions(+), 193 deletions(-) delete mode 100644 modules/monitoring/application/views/scripts/partials/host/servicesummary.phtml diff --git a/library/Icinga/Web/Url.php b/library/Icinga/Web/Url.php index 7c3829537..237c8988a 100644 --- a/library/Icinga/Web/Url.php +++ b/library/Icinga/Web/Url.php @@ -6,7 +6,7 @@ namespace Icinga\Web; use Icinga\Application\Icinga; use Icinga\Cli\FakeRequest; use Icinga\Exception\ProgrammingError; -use Icinga\Web\UrlParams; +use Icinga\Data\Filter\Filter; /** * Url class that provides convenient access to parameters, allows to modify query parameters and @@ -156,6 +156,26 @@ class Url return $urlObject; } + /** + * Create a new filter that needs to fullfill the base filter and the optional filter (if it exists) + * + * @param string $url The url to apply the new filter to + * @param Filter $filter The base filter + * @param Filter $optional The optional filter + * + * @return Url The altered URL containing the new filter + * @throws ProgrammingError + */ + public static function urlAddFilterOptional($url, $filter, $optional) + { + $url = Url::fromPath($url); + $f = $filter; + if (isset($optional)) { + $f = Filter::matchAll($filter, $optional); + } + return $url->setQueryString($f->toQueryString()); + } + /** * Overwrite the baseUrl * diff --git a/modules/monitoring/application/views/scripts/host/services.phtml b/modules/monitoring/application/views/scripts/host/services.phtml index 11166f8fa..fd69d88ad 100644 --- a/modules/monitoring/application/views/scripts/host/services.phtml +++ b/modules/monitoring/application/views/scripts/host/services.phtml @@ -1,9 +1,17 @@ + +
compact): ?> tabs; ?> render('partials/host/object-header.phtml') ?> - render('partials/host/servicesummary.phtml') ?> +

compact ? ' data-base-target="col1"' : ''; ?> + baseFilter = Filter::where('host', $object->host_name); + $this->stats = $object->stats; + echo $this->render('list/components/servicesummary.phtml'); + ?> +

partial( 'list/services.phtml', diff --git a/modules/monitoring/application/views/scripts/host/show.phtml b/modules/monitoring/application/views/scripts/host/show.phtml index a612ae1af..12de39a78 100644 --- a/modules/monitoring/application/views/scripts/host/show.phtml +++ b/modules/monitoring/application/views/scripts/host/show.phtml @@ -1,9 +1,17 @@ + +
compact): ?> tabs; ?> render('partials/host/object-header.phtml') ?> - render('partials/host/servicesummary.phtml') ?> +

compact ? ' data-base-target="col1"' : ''; ?>> + stats = $object->stats; + $this->baseFilter = Filter::where('host', $object->host_name); + echo $this->render('list/components/servicesummary.phtml'); + ?> +

render('show/components/output.phtml') ?> diff --git a/modules/monitoring/application/views/scripts/hosts/show.phtml b/modules/monitoring/application/views/scripts/hosts/show.phtml index 3dbb0673f..053c44520 100644 --- a/modules/monitoring/application/views/scripts/hosts/show.phtml +++ b/modules/monitoring/application/views/scripts/hosts/show.phtml @@ -3,7 +3,9 @@ - render('list/components/hostssummary.phtml') ?> +

compact ? ' data-base-target="col1"' : ''; ?> + render('list/components/hostssummary.phtml') ?> +

render('partials/host/objects-header.phtml'); ?>
diff --git a/modules/monitoring/application/views/scripts/list/components/hostssummary.phtml b/modules/monitoring/application/views/scripts/list/components/hostssummary.phtml index 22f84b583..3fe11bcca 100644 --- a/modules/monitoring/application/views/scripts/list/components/hostssummary.phtml +++ b/modules/monitoring/application/views/scripts/list/components/hostssummary.phtml @@ -3,20 +3,16 @@ use Icinga\Web\Url; use Icinga\Data\Filter\Filter; -function urlAddFilterOptional($url, $filter, $optional) { - $url = Url::fromPath($url); - $f = $filter; - if (isset($optional)) { - $f = Filter::matchAll($filter, $optional); - } - return $url->setQueryString($f->toQueryString()); -} $this->baseFilter = isset($this->baseFilter) ? $this->baseFilter : null; -$stats = $stats->fetchRow(); +// don't fetch rows until they are actually needed to improve dashlet performance +if (!$stats instanceof stdClass) { + $stats = $stats->fetchRow(); +} + $selfUrl = 'monitoring/list/hosts'; $currentUrl = Url::fromRequest()->getRelativeUrl(); -?>

compact ? ' data-base-target="col1"' : ''; ?>> +?> qlink( sprintf($this->translatePlural('%u Host', '%u Hosts', $stats->hosts_total), $stats->hosts_total), $selfUrl, @@ -31,7 +27,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->hosts_up, - urlAddFilterOptional( + Url::urlAddFilterOptional( $selfUrl, Filter::where('host_state', 0), $this->baseFilter @@ -54,7 +50,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->hosts_down_unhandled, - urlAddFilterOptional( + Url::urlAddFilterOptional( $selfUrl, Filter::matchAll(Filter::where('host_state', 1), Filter::where('host_unhandled', 1)), $this->baseFilter @@ -75,7 +71,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->hosts_down_handled, - urlAddFilterOptional( + Url::urlAddFilterOptional( $selfUrl, Filter::matchAll(Filter::where('host_state', 1), Filter::where('host_unhandled', 0)), $this->baseFilter @@ -101,7 +97,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->hosts_unreachable_unhandled, - urlAddFilterOptional( + Url::urlAddFilterOptional( $selfUrl, Filter::matchAll(Filter::where('host_state', 2), Filter::where('host_unhandled', 1)), $this->baseFilter @@ -122,7 +118,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->hosts_unreachable_handled, - urlAddFilterOptional( + Url::urlAddFilterOptional( $selfUrl, Filter::matchAll(Filter::where('host_state', 2), Filter::where('host_unhandled', 0)), $this->baseFilter @@ -148,7 +144,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->hosts_pending, - urlAddFilterOptional( + Url::urlAddFilterOptional( $selfUrl, Filter::where('host_state', 99), $this->baseFilter @@ -166,4 +162,3 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); -

diff --git a/modules/monitoring/application/views/scripts/list/components/servicesummary.phtml b/modules/monitoring/application/views/scripts/list/components/servicesummary.phtml index 9fd798a0d..1f401004d 100644 --- a/modules/monitoring/application/views/scripts/list/components/servicesummary.phtml +++ b/modules/monitoring/application/views/scripts/list/components/servicesummary.phtml @@ -4,21 +4,16 @@ use Icinga\Data\Filter\Filter; use Icinga\Web\Url; use Icinga\Module\Monitoring\Object\Service; -function urlAddFilterOptional($url, $filter, $optional) { - $url = Url::fromPath($url); - $f = $filter; - if (isset($optional)) { - $f = Filter::matchAll($filter, $optional); - } - return $url->setQueryString($f->toQueryString()); -} $this->baseFilter = isset($this->baseFilter) ? $this->baseFilter : null; -$stats = $stats->fetchRow(); +// don't fetch rows until they are actually needed, to improve dashlet performance +if (!$stats instanceof stdClass) { + $stats = $stats->fetchRow(); +}; $selfUrl = 'monitoring/list/services'; $currentUrl = Url::fromRequest()->getRelativeUrl(); -?>

compact ? ' data-base-target="col1"' : ''; ?>> -qlink( + +echo $this->qlink( sprintf($this->translatePlural( '%u Service', '%u Services', $stats->services_total), $stats->services_total @@ -36,7 +31,7 @@ $currentUrl = Url::fromRequest()->getRelativeUrl(); qlink( $stats->services_ok, - urlAddFilterOptional($selfUrl, Filter::where('service_state', 0), $this->baseFilter), + Url::urlAddFilterOptional($selfUrl, Filter::where('service_state', 0), $this->baseFilter), null, array('title' => sprintf( $this->translatePlural( @@ -81,7 +76,7 @@ foreach (array(2 => 'critical', 3 => 'unknown', 1 => 'warning') as $stateId => $ echo $this->qlink( $stats->$unhandled, - urlAddFilterOptional($selfUrl, $paramsUnhandled, $this->baseFilter), + Url::urlAddFilterOptional($selfUrl, $paramsUnhandled, $this->baseFilter), null, array('title' => sprintf( $this->translatePlural( @@ -106,7 +101,7 @@ foreach (array(2 => 'critical', 3 => 'unknown', 1 => 'warning') as $stateId => $ } echo $this->qlink( $stats->$handled, - urlAddFilterOptional($selfUrl, $paramsHandled, $this->baseFilter), + Url::urlAddFilterOptional($selfUrl, $paramsHandled, $this->baseFilter), null, array('title' => sprintf( $this->translatePlural( @@ -130,7 +125,7 @@ foreach (array(2 => 'critical', 3 => 'unknown', 1 => 'warning') as $stateId => $ qlink( $stats->services_pending, - urlAddFilterOptional($selfUrl, Filter::where('service_state', 99), $this->baseFilter), + Url::urlAddFilterOptional($selfUrl, Filter::where('service_state', 99), $this->baseFilter), null, array('title' => sprintf( $this->translatePlural( @@ -144,4 +139,3 @@ foreach (array(2 => 'critical', 3 => 'unknown', 1 => 'warning') as $stateId => $ -

diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 62ca65121..859f01aec 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -8,7 +8,9 @@ if (! $this->compact): ?> tabs; ?>
render('list/components/selectioninfo.phtml'); ?> - render('list/components/hostssummary.phtml'); ?> +

compact ? ' data-base-target="col1"' : ''; ?> + render('list/components/hostssummary.phtml'); ?> +

sortBox; ?> limiter; ?> diff --git a/modules/monitoring/application/views/scripts/list/services.phtml b/modules/monitoring/application/views/scripts/list/services.phtml index d2dfb250a..9b5dd1ec1 100644 --- a/modules/monitoring/application/views/scripts/list/services.phtml +++ b/modules/monitoring/application/views/scripts/list/services.phtml @@ -10,7 +10,9 @@ if (! $this->compact): ?> tabs ?>
render('list/components/selectioninfo.phtml') ?> - render('list/components/servicesummary.phtml') ?> +

compact ? ' data-base-target="col1"' : ''; ?>> + render('list/components/servicesummary.phtml') ?> +

sortBox ?> limiter ?> diff --git a/modules/monitoring/application/views/scripts/partials/command/object-command-form.phtml b/modules/monitoring/application/views/scripts/partials/command/object-command-form.phtml index ab14b1b2a..095d9a4c4 100644 --- a/modules/monitoring/application/views/scripts/partials/command/object-command-form.phtml +++ b/modules/monitoring/application/views/scripts/partials/command/object-command-form.phtml @@ -1,10 +1,18 @@ + +
compact): ?> tabs; ?> getType() === $object::TYPE_HOST): ?> render('partials/host/object-header.phtml'); ?> - render('partials/host/servicesummary.phtml'); ?> +

compact ? ' data-base-target="col1"' : ''; ?>"> + baseFilter = Filter::where('host', $object->host_name); + $this->stats = $object->stats; + echo $this->render('list/components/servicesummary.phtml'); + ?> +

render('partials/service/object-header.phtml'); ?>
diff --git a/modules/monitoring/application/views/scripts/partials/command/objects-command-form.phtml b/modules/monitoring/application/views/scripts/partials/command/objects-command-form.phtml index 42c6e73cd..6a209e159 100644 --- a/modules/monitoring/application/views/scripts/partials/command/objects-command-form.phtml +++ b/modules/monitoring/application/views/scripts/partials/command/objects-command-form.phtml @@ -1,3 +1,5 @@ + +
compact): ?> @@ -5,10 +7,14 @@ - render('list/components/servicesummary.phtml') ?> +

compact ? ' data-base-target="col1"' : ''; ?> + render('list/components/servicesummary.phtml'); ?> +

render('partials/service/objects-header.phtml'); ?> - render('list/components/hostssummary.phtml') ?> +

compact ? ' data-base-target="col1"' : ''; ?> + render('list/components/hostssummary.phtml') ?> +

render('partials/host/objects-header.phtml'); ?>
diff --git a/modules/monitoring/application/views/scripts/partials/host/servicesummary.phtml b/modules/monitoring/application/views/scripts/partials/host/servicesummary.phtml deleted file mode 100644 index be146acbe..000000000 --- a/modules/monitoring/application/views/scripts/partials/host/servicesummary.phtml +++ /dev/null @@ -1,150 +0,0 @@ -setQueryString($f->toQueryString()); -} - -$selfUrl = Url::fromPath('monitoring/list/services', array('host' => $object->host_name)); -?>

compact ? ' data-base-target="col1"' : ''; ?>> -stats->services_total): ?> -qlink( - sprintf( - $this->translatePlural( - '%u configured service:', - '%u configured services:', - $object->stats->services_total - ), - $object->stats->services_total - ), - $selfUrl, - null, - array( - 'title' => sprintf( - $this->translatePlural( - 'List all %u service on host %s', - 'List all %u services on host %s', - $object->stats->services_total - ), - $object->stats->services_total, - $object->host_name - ), - 'data-base-target' => '_next' - ) -); ?> - -translate('No services configured on this host'); ?> - - - -stats->services_ok): ?> - - qlink( - $object->stats->services_ok, - $selfUrl, - array('service_state' => 0), - array( - 'title' => sprintf( - $this->translatePlural( - 'List %u service that is currently in state OK on host %s', - 'List %u services which are currently in state OK on host %s', - $object->stats->services_ok - ), - $object->stats->services_ok, - $object->host_name - ), - 'data-base-target' => '_next' - ) - ); ?> - - - 'critical', 3 => 'unknown', 1 => 'warning') as $stateId => $state) { - $pre = 'services_' . $state; - if ($object->stats->$pre) { - $handled = $pre . '_handled'; - $unhandled = $pre . '_unhandled'; - $paramsHandled = array('service_state' => $stateId, 'service_handled' => 1); - $paramsUnhandled = array('service_state' => $stateId, 'service_handled' => 0); - echo ''; - if ($object->stats->$unhandled) { - - echo $this->qlink( - $object->stats->$unhandled, - $selfUrl, - $paramsUnhandled, - array( - 'title' => sprintf( - $this->translatePlural( - 'List %u service that is currently in state %s on host %s', - 'List %u services which are currently in state %s on host %s', - $object->stats->$unhandled - ), - $object->stats->$unhandled, - Service::getStateText($stateId, true), - $object->host_name - ), - 'data-base-target' => '_next' - ) - ); - } - if ($object->stats->$handled) { - if ($object->stats->$unhandled) { - echo ''; - } - echo $this->qlink( - $object->stats->$handled, - $selfUrl, - $paramsHandled, - array( - 'title' => sprintf( - $this->translatePlural( - 'List %u service that is currently in state %s (Acknowledged) on host %s', - 'List %u services which are currently in state %s (Acknowledged) on host %s', - $object->stats->$handled - ), - $object->stats->$handled, - Service::getStateText($stateId, true), - $object->host_name - ), - 'data-base-target' => '_next' - ) - ); - if ($object->stats->$unhandled) { - echo "\n"; - } - } - echo "\n"; - } -} -?> -stats->services_pending): ?> - - qlink( - $object->stats->services_pending, - $selfUrl, - array('service_state' => 99), - array( - 'title' => sprintf( - $this->translatePlural( - 'List %u service that is currently in state PENDING on host %s', - 'List %u services which are currently in state PENDING on host %s', - $object->stats->services_pending - ), - $object->stats->services_pending, - $object->host_name - ), - 'data-base-target' => '_next' - ) - ) ?> - - - -

diff --git a/modules/monitoring/application/views/scripts/services/show.phtml b/modules/monitoring/application/views/scripts/services/show.phtml index d2ad21bca..be8cf5a8b 100644 --- a/modules/monitoring/application/views/scripts/services/show.phtml +++ b/modules/monitoring/application/views/scripts/services/show.phtml @@ -4,7 +4,10 @@ - render('list/components/servicesummary.phtml') ?> + +

compact ? ' data-base-target="col1"' : ''; ?> + render('list/components/servicesummary.phtml') ?> +

render('partials/service/objects-header.phtml'); ?>