[icinga-checkins] icinga.org: icingaweb2-module-director/feature/icinga-legacy-config-13049: StartupLogRenderer: remove heavvy logic from view

git at icinga.org git at icinga.org
Fri Nov 11 10:44:00 CET 2016


Module: icingaweb2-module-director
Branch: feature/icinga-legacy-config-13049
Commit: 8ca17f4e7ba8ed677a4aa9120edf450356feafd6
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=8ca17f4e7ba8ed677a4aa9120edf450356feafd6

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Nov  8 16:48:45 2016 +0100

StartupLogRenderer: remove heavvy logic from view

---

 application/views/scripts/deployment/index.phtml |   94 +--------------------
 library/Director/StartupLogRenderer.php          |   98 ++++++++++++++++++++++
 2 files changed, 100 insertions(+), 92 deletions(-)

diff --git a/application/views/scripts/deployment/index.phtml b/application/views/scripts/deployment/index.phtml
index ecd5d9a..00aa77a 100644
--- a/application/views/scripts/deployment/index.phtml
+++ b/application/views/scripts/deployment/index.phtml
@@ -1,94 +1,7 @@
 <?php
 
 use Icinga\Util\Format;
-
-$view = $this;
-$logLink = function ($match, $severity) use ($view) {
-    $stageDir = $match[1];
-    $filename = $match[2];
-    $suffix = $match[3];
-    if (preg_match('/(\d+).*/', $suffix, $m)) {
-        $lineNumber = $m[1];
-    } else {
-        $lineNumber = null;
-    }
-
-    $params = array(
-        'config_checksum'   => $view->config_checksum,
-        'file_path'         => $filename,
-        'deployment_id'     => $view->deployment->id,
-        'fileOnly'          => true,
-    );
-    if ($lineNumber !== null) {
-        $params['highlight']         = $lineNumber;
-        $params['highlightSeverity'] = $severity;
-    }
-
-    return $view->qlink(
-        '[stage]/' . $filename,
-        'director/config/file',
-        $params,
-        array(
-            'data-base-target' => '_next',
-            'title'            => $stageDir . $filename
-        )
-    ) . $suffix;
-};
-
-function colorize($log, $logLink) {
-
-    $lines = array();
-    $severity = 'information';
-    $sevPattern = '/^(debug|notice|information|warning|critical)\/(\w+)/';
-    $filePatternHint = '~(/[\w/]+/api/packages/director/[^/]+/)([^:]+\.conf)(: (\d+))~';
-    $filePatternDetail = '~(/[\w/]+/api/packages/director/[^/]+/)([^:]+\.conf)(\((\d+)\))~';
-    $markPattern = null;
-    // len [stage] + 1
-    $markReplace = '        ^';
-    foreach (preg_split('/\n/', $log) as $line) {
-        if (preg_match($sevPattern, $line, $m)) {
-            $severity = $m[1];
-            $line = preg_replace(
-                $sevPattern,
-                '<span class="loglevel \1">\1</span>/<span class="application">\2</span>',
-                $line
-            );
-        }
-
-        if ($markPattern !== null) {
-            $line = preg_replace($markPattern, $markReplace, $line);
-        }
-        $line = preg_replace('/([\^]{2,})/', '<span class="error-hint">\1</span>', $line);
-        $markPattern = null;
-
-        if (preg_match($filePatternHint, $line, $m)) {
-            $line = preg_replace_callback(
-                $filePatternHint,
-                function ($matches) use ($logLink, $severity) {
-                    return $logLink($matches, $severity);
-                },
-                $line
-            );
-            $line = preg_replace('/\(in/', "\n  (in", $line);
-            $line = preg_replace('/\), new declaration/', "),\n  new declaration", $line);
-        } elseif (preg_match($filePatternDetail, $line, $m)) {
-            $markIndent = strlen($m[1]);
-            $markPattern = '/\s{' . $markIndent . '}\^/';
-
-            $line = preg_replace_callback(
-                $filePatternDetail,
-                function ($matches) use ($logLink, $severity) {
-                    return $logLink($matches, $severity);
-                },
-                $line
-            );
-        }
-
-
-        $lines[] .= $line;
-    }
-    return implode("\n", $lines);
-}
+use Icinga\Module\Director\StartupLogRenderer;
 
 ?><div class="controls">
 <?= $this->tabs ?>
@@ -162,10 +75,7 @@ if ($deployment->startup_succeeded === null) {
 <?php if ($deployment->startup_succeeded !== null): ?>
 <h2>Startup log</h2>
 <pre class="logfile">
-<?= colorize(
-    $this->escape($deployment->startup_log),
-    $logLink
-) ?>
+<?= StartupLogRenderer::beautify($deployment, $this) ?>
 </pre>
 <?php endif ?>
 </div>
diff --git a/library/Director/StartupLogRenderer.php b/library/Director/StartupLogRenderer.php
new file mode 100644
index 0000000..599f5a7
--- /dev/null
+++ b/library/Director/StartupLogRenderer.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Icinga\Module\Director;
+
+use Icinga\Module\Director\Objects\DirectorDeploymentLog;
+use Icinga\Web\View;
+
+class StartupLogRenderer
+{
+    public static function beautify(DirectorDeploymentLog $deploymentLog, View $view)
+    {
+        $log = $view->escape($deploymentLog->get('startup_log'));
+        $lines = array();
+        $severity = 'information';
+        $sevPattern = '/^(debug|notice|information|warning|critical)\/(\w+)/';
+        $filePatternHint = '~(/[\w/]+/api/packages/director/[^/]+/)([^:]+\.conf)(: (\d+))~';
+        $filePatternDetail = '~(/[\w/]+/api/packages/director/[^/]+/)([^:]+\.conf)(\((\d+)\))~';
+        $markPattern = null;
+        // len [stage] + 1
+        $markReplace = '        ^';
+        foreach (preg_split('/\n/', $log) as $line) {
+
+            if (preg_match($sevPattern, $line, $m)) {
+                $severity = $m[1];
+                $line = preg_replace(
+                    $sevPattern,
+                    '<span class="loglevel \1">\1</span>/<span class="application">\2</span>',
+                    $line
+                );
+            }
+
+            if ($markPattern !== null) {
+                $line = preg_replace($markPattern, $markReplace, $line);
+            }
+            $line = preg_replace('/([\^]{2,})/', '<span class="error-hint">\1</span>', $line);
+            $markPattern = null;
+
+            if (preg_match($filePatternHint, $line, $m)) {
+                $line = preg_replace_callback(
+                    $filePatternHint,
+                    function ($matches) use ($severity, $view, $deploymentLog) {
+                        return StartupLogRenderer::logLink($matches, $severity, $deploymentLog, $view);
+                    },
+                    $line
+                );
+                $line = preg_replace('/\(in/', "\n  (in", $line);
+                $line = preg_replace('/\), new declaration/', "),\n  new declaration", $line);
+            } elseif (preg_match($filePatternDetail, $line, $m)) {
+                $markIndent = strlen($m[1]);
+                $markPattern = '/\s{' . $markIndent . '}\^/';
+
+                $line = preg_replace_callback(
+                    $filePatternDetail,
+                    function ($matches) use ($severity, $view, $deploymentLog) {
+                        return StartupLogRenderer::logLink($matches, $severity, $deploymentLog, $view);
+                    },
+                    $line
+                );
+            }
+
+            $lines[] .= $line;
+        }
+        return implode("\n", $lines);
+    }
+
+    public static function logLink($match, $severity, DirectorDeploymentLog $deploymentLog, View $view)
+    {
+        $stageDir = $match[1];
+        $filename = $match[2];
+        $suffix = $match[3];
+        if (preg_match('/(\d+).*/', $suffix, $m)) {
+            $lineNumber = $m[1];
+        } else {
+            $lineNumber = null;
+        }
+
+        $params = array(
+            'config_checksum' => $deploymentLog->getConfigHexChecksum(),
+            'deployment_id'   => $deploymentLog->get('id'),
+            'file_path'       => $filename,
+            'fileOnly'        => true,
+        );
+        if ($lineNumber !== null) {
+            $params['highlight'] = $lineNumber;
+            $params['highlightSeverity'] = $severity;
+        }
+
+        return $view->qlink(
+            '[stage]/' . $filename,
+            'director/config/file',
+            $params,
+            array(
+                'data-base-target' => '_next',
+                'title' => $stageDir . $filename
+            )
+        ) . $suffix;
+    }
+}
\ No newline at end of file



More information about the icinga-checkins mailing list