[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaConfigHelper: provide helpers for intervals

git at icinga.org git at icinga.org
Sun Feb 28 17:12:39 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sun Feb 28 12:40:11 2016 +0100

IcingaConfigHelper: provide helpers for intervals

---

 .../Director/IcingaConfig/IcingaConfigHelper.php   |   79 ++++++++++++++++++++
 library/Director/Objects/IcingaObject.php          |   36 +--------
 2 files changed, 80 insertions(+), 35 deletions(-)

diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php
index bb86927..26e3c58 100644
--- a/library/Director/IcingaConfig/IcingaConfigHelper.php
+++ b/library/Director/IcingaConfig/IcingaConfigHelper.php
@@ -151,4 +151,83 @@ class IcingaConfigHelper
             return $string;
         }
     }
+
+    public static function isValidInterval($interval)
+    {
+        if (ctype_digit($interval)) {
+            return true;
+        }
+
+        $parts = preg_split('/\s+/', $interval, -1, PREG_SPLIT_NO_EMPTY);
+        $value = 0;
+        foreach ($parts as $part) {
+            if (! preg_match('/^(\d+)([dhms]?)$/', $part)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    public static function parseInterval($interval)
+    {
+        if (ctype_digit($interval)) {
+            return (int) $interval;
+        }
+
+        $parts = preg_split('/\s+/', $interval, -1, PREG_SPLIT_NO_EMPTY);
+        $value = 0;
+        foreach ($parts as $part) {
+            if (! preg_match('/^(\d+)([dhms]?)$/', $part, $m)) {
+                throw new ProgrammingError(
+                    '"%s" is not a valid time (duration) definition',
+                    $interval
+                );
+            }
+            switch ($m[2]) {
+                case 'd':
+                    $value += $m[1] * 86400;
+                    break;
+                case 'h':
+                    $value += $m[1] * 3600;
+                    break;
+                case 'm':
+                    $value += $m[1] * 60;
+                    break;
+                default:
+                    $value += (int) $m[1];
+            }
+        }
+
+        return $value;
+    }
+
+    public static function renderInterval($interval)
+    {
+        // TODO: compat only, do this at munge time. All db fields should be int
+        $seconds = self::parseInterval($interval);
+        if ($seconds === 0) {
+            return '0s';
+        }
+
+        $parts = array();
+        $steps = array(
+            'd' => 86400,
+            'h' => 3600,
+            'm' => 60,
+        );
+
+        foreach ($steps as $unit => $duration) {
+            if ($seconds > $duration) {
+                $parts[] = (int) floor($seconds / $duration) . $unit;
+                $seconds = $seconds % $duration;
+            }
+        }
+
+        if ($seconds > 0) {
+            $parts[] = $seconds . 's';
+        }
+
+        return implode(' ', $parts);
+    }
 }
diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index f82774b..4748b36 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -849,41 +849,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
 
     protected function renderPropertyAsSeconds($key)
     {
-        // TODO: db field should be int
-        if (ctype_digit($this->$key)) {
-            $value = (int) $this->$key;
-        } else {
-            // TODO: do this at munge time
-            $parts = preg_split('/\s+/', $this->$key, -1, PREG_SPLIT_NO_EMPTY);
-            $value = 0;
-            foreach ($parts as $part) {
-                if (! preg_match('/^(\d+)([dhms]?)$/', $part, $m)) {
-                    throw new ConfigurationError(
-                        '"%s" is not a valid time (duration) definition',
-                        $this->$key
-                    );
-                }
-                switch ($m[2]) {
-                    case 'd':
-                        $value += $m[1] * 86400;
-                        break;
-                    case 'h':
-                        $value += $m[1] * 3600;
-                        break;
-                    case 'm':
-                        $value += $m[1] * 60;
-                        break;
-                    default:
-                        $value += (int) $m[1];
-                }
-            }
-        }
-
-        if ($value > 0 && $value % 60 === 0) {
-            $value = (int) ($value / 60) . 'm';
-        }
-
-        return c::renderKeyValue($key, $value);
+        return c::renderKeyValue($key, c::renderInterval($this->$key));
     }
 
     protected function renderSuffix()



More information about the icinga-checkins mailing list