[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaObject: introduce getSingleResolvedProperty()

git at icinga.org git at icinga.org
Mon Nov 14 13:28:56 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: 913d934762f8b27d0be88d67018b42a9d5cacb26
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=913d934762f8b27d0be88d67018b42a9d5cacb26

Author: Thomas Gelf <thomas at gelf.net>
Date:   Mon Nov 14 13:25:01 2016 +0100

IcingaObject: introduce getSingleResolvedProperty()

Motivation: getResolvedProperty() has the effect that objects will fully
resolve all inherited properties, vars and fields. This is overkill in
situations where only a few resolves properties are needed and has quite
an influence on overall rendering performance.

Over time we might completely fade out that part of our resolving logic
as it predates current cache implementations. Meanwhile, we keep two
methods for two (internal) purposes.

---

 library/Director/Objects/IcingaCommand.php |    2 +-
 library/Director/Objects/IcingaHost.php    |   12 +++++------
 library/Director/Objects/IcingaObject.php  |   32 ++++++++++++++++++++++++----
 library/Director/Objects/IcingaService.php |    4 ++--
 4 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/library/Director/Objects/IcingaCommand.php b/library/Director/Objects/IcingaCommand.php
index e847c72..6c08247 100644
--- a/library/Director/Objects/IcingaCommand.php
+++ b/library/Director/Objects/IcingaCommand.php
@@ -83,7 +83,7 @@ class IcingaCommand extends IcingaObject
             $itlImport = '';
         }
 
-        $execute = $this->getResolvedProperty('methods_execute');
+        $execute = $this->getSingleResolvedProperty('methods_execute');
         if ($execute === 'PluginNotification') {
             return $this->renderObjectHeaderWithType('NotificationCommand') . $itlImport;
         } elseif ($execute === 'PluginEvent') {
diff --git a/library/Director/Objects/IcingaHost.php b/library/Director/Objects/IcingaHost.php
index 7afca7d..1ce459a 100644
--- a/library/Director/Objects/IcingaHost.php
+++ b/library/Director/Objects/IcingaHost.php
@@ -152,7 +152,7 @@ class IcingaHost extends IcingaObject
 
     public function getCheckCommand()
     {
-        $id = $this->getResolvedProperty('check_command_id');
+        $id = $this->getSingleResolvedProperty('check_command_id');
         return IcingaCommand::loadWithAutoIncId(
             $id,
             $this->getConnection()
@@ -161,7 +161,7 @@ class IcingaHost extends IcingaObject
 
     public function hasCheckCommand()
     {
-        return $this->getResolvedProperty('check_command_id') !== null;
+        return $this->getSingleResolvedProperty('check_command_id') !== null;
     }
 
     public function renderToConfig(IcingaConfig $config)
@@ -185,7 +185,7 @@ class IcingaHost extends IcingaObject
             return;
         }
 
-        if ($this->getResolvedProperty('has_agent') !== 'y') {
+        if ($this->getSingleResolvedProperty('has_agent') !== 'y') {
             return;
         }
 
@@ -200,11 +200,11 @@ class IcingaHost extends IcingaObject
             'log_duration' => 0
         );
 
-        if ($this->getResolvedProperty('master_should_connect') === 'y') {
-            $props['host'] = $this->getResolvedProperty('address');
+        if ($this->getSingleResolvedProperty('master_should_connect') === 'y') {
+            $props['host'] = $this->getSingleResolvedProperty('address');
         }
 
-        $props['zone_id'] = $this->getResolvedProperty('zone_id');
+        $props['zone_id'] = $this->getSingleResolvedProperty('zone_id');
 
         $endpoint = IcingaEndpoint::create($props);
 
diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index cd9e403..1bce917 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -309,7 +309,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
 
     public function getResolvedRelated($property)
     {
-        $id = $this->getResolvedProperty($property . '_id');
+        $id = $this->getSingleResolvedProperty($property . '_id');
 
         if ($id) {
             return $this->getRelatedObject($property, $id);
@@ -1059,6 +1059,30 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         $this->templateResolver()->listResolvedParentIds();
     }
 
+    public function getSingleResolvedProperty($key, $default = null)
+    {
+        if (array_key_exists($key, $this->unresolvedRelatedProperties)) {
+            $this->resolveUnresolvedRelatedProperty($key);
+            $this->invalidateResolveCache();
+        }
+
+        if ($my = $this->get($key)) {
+            if ($my !== null) {
+                return $my;
+            }
+        }
+
+        /** @var IcingaObject $object */
+        foreach (array_reverse($this->imports()->getObjects()) as $object) {
+            $v = $object->getSingleResolvedProperty($key);
+            if (null !== $v) {
+                return $v;
+            }
+        }
+
+        return $default;
+    }
+
     protected function resolve($what)
     {
         if ($this->hasResolveCached($what)) {
@@ -1488,7 +1512,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         $deploymentMode = $config->getDeploymentMode();
         if ($deploymentMode === 'active-passive') {
             if (
-                $this->getResolvedProperty('zone_id')
+                $this->getSingleResolvedProperty('zone_id')
                 && array_key_exists('enable_active_checks', $this->defaultProperties)
             ) {
                 $passive = clone($this);
@@ -1559,7 +1583,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
             }
 
             try {
-                if ($zoneId = $this->getResolvedProperty('zone_id')) {
+                if ($zoneId = $this->getSingleResolvedProperty('zone_id')) {
                     // Config has a lookup cache, is faster:
                     return $config->getZoneName($zoneId);
                 }
@@ -2592,7 +2616,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
                 $props[$k] = $v;
             }
         }
-        
+
         if ($this->supportsCustomVars()) {
             $props['vars'] = (object) array();
             foreach ($this->vars()->getOriginalVars() as $name => $var) {
diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php
index ac39bdd..2981620 100644
--- a/library/Director/Objects/IcingaService.php
+++ b/library/Director/Objects/IcingaService.php
@@ -103,7 +103,7 @@ class IcingaService extends IcingaObject
 
     public function getCheckCommand()
     {
-        $id = $this->getResolvedProperty('check_command_id');
+        $id = $this->getSingleResolvedProperty('check_command_id');
         return IcingaCommand::loadWithAutoIncId(
             $id,
             $this->getConnection()
@@ -388,7 +388,7 @@ class IcingaService extends IcingaObject
 
     public function hasCheckCommand()
     {
-        return $this->getResolvedProperty('check_command_id') !== null;
+        return $this->getSingleResolvedProperty('check_command_id') !== null;
     }
 
     public function getOnDeleteUrl()



More information about the icinga-checkins mailing list