r4647 adding a new "cabling plan" extension by Dennis Plöger (#434)
authorDenis Ovsienko <infrastation@yandex.ru>
Sat, 2 Jul 2011 20:19:49 +0000 (20:19 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Mon, 26 Mar 2012 10:12:24 +0000 (14:12 +0400)
cablingplan/cablingplan.php [new file with mode: 0644]
cablingplan/cablingplan.png [new file with mode: 0644]
cablingplan/index.php.patch [new file with mode: 0644]

diff --git a/cablingplan/cablingplan.php b/cablingplan/cablingplan.php
new file mode 100644 (file)
index 0000000..c997f2a
--- /dev/null
@@ -0,0 +1,159 @@
+<?php
+/*
+
+Copyright 2011 Dennis Plöger
+
+This is a RackTables extension to display a schematic cabling plan of all
+objects in the system. The current revision consists of:
+1. PHP file to use with the well-known "local.php" file
+2. PNG file to copy into "pix" directory of RackTables instance
+3. patch file to modify index.php
+
+This extension requires an installed PEAR Image-GraphViz module.
+
+*/
+
+require_once 'Image/GraphViz.php';
+
+if (isset($indexlayout)) {
+
+    array_push($indexlayout[2], 'cablingplan');
+
+}
+
+$page['cablingplan']['title'] = 'Cabling plan';
+$page['cablingplan']['parent'] = 'index';
+$tab['cablingplan']['default'] = 'Cabling plan (PNG)';
+$tab['cablingplan']['defaultsvg'] = 'Cabling plan (SVG)';
+$tabhandler['cablingplan']['default'] = 'showCablingPlan';
+$tabhandler['cablingplan']['defaultsvg'] = 'showCablingPlanSvg';
+
+$image['cablingplan']['path'] = 'pix/cablingplan.png';
+$image['cablingplan']['width'] = 218;
+$image['cablingplan']['height'] = 200;
+
+function showCablingPlan ()
+{
+
+    // Show cabling plan image
+
+    echo "<img hspace='10' vspace='10' src='?module=rendercablingplan&format=png' />\n";
+
+}
+
+function showCablingPlanSvg ()
+{
+
+    // Show cabling plan image
+
+    echo "<img hspace='10' vspace='10' src='?module=rendercablingplan&format=svg' />\n";
+
+}
+
+function renderCablingPlan ()
+{
+
+    // Build cabling plan
+
+    connectDB();
+
+    // Select edges
+
+    $sql = "select `oa`.`id` AS `source`,`ob`.`id` AS `target`,concat(`pa`.`name`,_utf8' <> ',`pb`.`name`) AS `label`,0 AS `weight` from ((`Link` `l` join `Port` `pa` on((`l`.`porta` = `pa`.`id`))) join `RackObject` `oa` on `pa`.`object_id` = `oa`.`id` join `Port` `pb` on((`l`.`portb` = `pb`.`id`)) join `RackObject` `ob` on `pb`.`object_id` = `ob`.`id`)";
+
+
+    $result = usePreparedSelectBlade($sql); 
+
+    $edges = array();
+
+    while ($row = $result->fetch (PDO::FETCH_ASSOC)) {
+
+        $found = false;
+
+        foreach ($edges as $key => $edge) {
+
+            if (($edge['source'] == $row['source']) && ($edge['target'] == $row['target'])) {
+
+                // Edge already exists ("Parallel"). Alter label and add weight
+
+                $edges[$key]['label'] .= "\n" . $row['label'];
+                $edges[$key]['weight']++;
+
+                $found = true;
+
+            }
+
+        }
+
+        if (!$found) {
+        
+            $edges[] = $row;
+
+        }
+
+    }
+
+    // Select nodes
+
+    $sql = "select distinct `o`.`id` AS `id`,`o`.`name` AS `label`, '' as `url` from `Port` `p` join `RackObject` `o` on `p`.`object_id` = `o`.`id` where (`p`.`id` in (select `Link`.`porta` AS `porta` from `Link`) or `p`.`id` in (select `Link`.`portb` AS `portb` from `Link`))";
+
+    $result = usePreparedSelectBlade($sql); 
+
+    $nodes = array();
+
+    while ($row = $result->fetch (PDO::FETCH_ASSOC)) {
+
+        $nodes[] = $row;
+
+    }
+
+    $graph = new Image_GraphViz(
+        true, 
+        array(
+
+
+        ), 
+        'Cabling Plan', 
+        false, 
+        false
+    );
+
+    foreach ($nodes as $node) {
+
+        $graph->addNode(
+            $node['id'],
+            array(
+                'label' => $node['label'],
+                'shape' => 'box3d'
+            )
+        );
+
+    }
+
+    foreach ($edges as $edge) {
+
+        $graph->addEdge(
+            array($edge['source'] => $edge['target']),
+            array(
+
+                'label' => $edge['label'],
+                'weight' => floatval($edge['weight']),
+                'fontsize' => 8.0,
+                'arrowhead' => 'dot',
+                'arrowtail' => 'dot',
+                'arrowsize' => 0.5
+
+            )
+            
+        );
+
+
+    }
+
+    if (in_array($_REQUEST['format'], array('svg', 'png'))) {
+
+        $graph->image($_REQUEST['format']);
+
+    }
+
+}
diff --git a/cablingplan/cablingplan.png b/cablingplan/cablingplan.png
new file mode 100644 (file)
index 0000000..0dea622
Binary files /dev/null and b/cablingplan/cablingplan.png differ
diff --git a/cablingplan/index.php.patch b/cablingplan/index.php.patch
new file mode 100644 (file)
index 0000000..9ceaa42
--- /dev/null
@@ -0,0 +1,15 @@
+===================================================================
+--- wwwroot/index.php  (Revision 212)
++++ wwwroot/index.php  (Revision 215)
+@@ -190,6 +190,11 @@
+               require_once 'inc/install.php';
+               renderInstallerHTML();
+               break;
++    case 'rendercablingplan' == $_REQUEST['module']:
++        require_once 'inc/database.php';
++        require_once 'inc/cablingplan.php';
++        renderCablingPlan();
++        break;
+       default:
+               throw new InvalidRequestArgException ('module', $_REQUEST['module']);
+       }