wattage_consumption: fix to work with 0.20.4
authorMark Wilkinson <Mark.Wilkinson@gamma.co.uk>
Wed, 17 Apr 2013 05:28:24 +0000 (09:28 +0400)
committerDenis Ovsienko <infrastation@yandex.ru>
Wed, 17 Apr 2013 05:28:24 +0000 (09:28 +0400)
wattage_consumption.php

index 0b7059c..430b11d 100644 (file)
@@ -2,9 +2,21 @@
 
 //-----------------------------------------------------------------------------------
 // Wattage Consumption - written by curtisb
-//$tab['reports']['watts_per_row'] = 'Energy Per Row';
+//
+//  Updated for Racktables-0.20.4 by Mark Wilkinson
+//          bonus of Rack Wattage portlet in Rack Page if portlet patch applied (bugs.racktables.org ID 691)
+//
+//
+// Usage - Create attribute 'Wattage consumption' and map to Servers, Chassis, etc
+//         Set power values for servers,etc.
+//         View Report page (or Rack page)
+//
 $tab['reports']['watts_per_row'] = 'Enviromental Totals';
 $tabhandler['reports']['watts_per_row'] = 'getWattsPerRow';
+if ( function_exists('registerPortletHandler') )
+{
+       registerPortletHandler( 'rack', 'default', 'left', 'Wattage Consumption', 'renderPortletWattConsumption');
+}
 
 function getWattsPerRow ()
 {
@@ -21,7 +33,7 @@ function getWattsPerRow ()
        }
        else
        {
-               $row_toshow = '50032';
+               $row_toshow = -1;
        }
 
        //from renderRackspace(), interface.php:151
@@ -31,12 +43,15 @@ function getWattsPerRow ()
        $rackCount = 0;
        $order = 'odd';
        // get rackspace information
-       foreach (getRackRows() as $row_id => $row_name) {
+       foreach (getAllRows() as $row_id => $rowInfo)
+       {
                $rackList = filterCellList (listCells ('rack', $row_id), $cellfilter['expression']);
-               $found_racks = array_merge($found_racks, $rackList);
-               $rows[] = array(
+               $found_racks = array_merge ($found_racks, $rackList);
+               $rows[] = array (
+                       'location_id' => $rowInfo['location_id'],
+                       'location_name' => $rowInfo['location_name'],
                        'row_id' => $row_id,
-                       'row_name' => $row_name,
+                       'row_name' => $rowInfo['name'],
                        'racks' => $rackList
                );
                $rackCount += count($rackList);
@@ -46,23 +61,24 @@ function getWattsPerRow ()
        echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
 
        // Left portlet with list of rows.
-       echo "<tr><td class=pcleft>";
+       echo "<tr><td class=pcleft width='50%'>";
        startPortlet ('Rack Rows (' . count ($rows) . ')');
        echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
        foreach ($rows as $row)
        {
                $row_id = $row['row_id'];
                $row_name = $row['row_name'];
+               $row_location = $row['location_name'];
                $rackList = $row['racks'];
 
                echo "<tr class=row_${order}><td width='20%'></td><td class=tdleft>";
                if (!count ($rackList))
                {
-                       echo "${row_name} (empty row)";
+                       echo "${row_location} - ${row_name} (empty row)";
                }
                else
                {
-                       echo "<a href='" . makeHref(array('page'=>'reports', 'tab'=>'watts_per_row', 'row_id'=>$row_id)) . "'>${row_name}</a>";
+                       echo "<a href='" . makeHref(array('page'=>'reports', 'tab'=>'watts_per_row', 'row_id'=>$row_id)) . "'>${row_location} - ${row_name}</a>";
                }
                echo "<td><tr>\n";
                $order = $nextorder[$order];
@@ -75,61 +91,89 @@ function getWattsPerRow ()
        echo "</td><td class=pcright>";
 
        // Right Portlet: Draw the racks in the selected row
-       $rowInfo = getRackRowInfo ($row_toshow);
-       $cellfilter = getCellFilter();
-       $rackList = filterCellList (listCells ('rack', $row_toshow), $cellfilter['expression']);
-
-       global $nextorder;
-       $rackwidth = getRackImageWidth() * getConfigVar ('ROW_SCALE');
-       // Maximum number of racks per row is proportionally less, but at least 1.
-       $maxPerRow = max (floor (getConfigVar ('RACKS_PER_ROW') / getConfigVar ('ROW_SCALE')), 1);
-       $rackListIdx = 0;
-       $rowTotalWattage = 0;
-       $order = 'odd';
-       startPortlet ('Racks within '. $rowInfo['name'] . ' (' . count($rackList) . ')' );
-       echo "<table border=0 cellspacing=5 align='center'><tr>";
-       foreach ($rackList as $rack)
+       if ( $row_toshow > -1 )
        {
-               $rackTotalWattage = 0;
-               // see renderRack(), interface.php:311
-               $rackData = spotEntity ('rack', $rack['id']);
-               amplifyCell ($rackData);
-               $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
-               foreach ($rackData['mountedObjects'] as $object)
+               $rowInfo = getRowInfo ($row_toshow);
+               $cellfilter = getCellFilter();
+               $rackList = filterCellList (listCells ('rack', $row_toshow), $cellfilter['expression']);
+
+               global $nextorder;
+               $rackwidth = getRackImageWidth() * getConfigVar ('ROW_SCALE');
+               // Maximum number of racks per row is proportionally less, but at least 1.
+               $maxPerRow = max (floor (getConfigVar ('RACKS_PER_ROW') / getConfigVar ('ROW_SCALE')), 1);
+               $rackListIdx = 0;
+               $rowTotalWattage = 0;
+               $order = 'odd';
+               startPortlet ('Racks within '. $rowInfo['name'] . ' (' . count($rackList) . ')' );
+               echo "<table border=0 cellspacing=5 align='center'><tr>";
+               foreach ($rackList as $rack)
                {
-                       $objectData = spotEntity ('object', $object);
-                       amplifyCell ($objectData);
-                       foreach (getAttrValues ($objectData['id']) as $record)
+                       $rackTotalWattage = 0;
+                       // see renderRack(), interface.php:311
+                       $rackData = spotEntity ('rack', $rack['id']);
+                       amplifyCell ($rackData);
+                       $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
+                       foreach ($rackData['mountedObjects'] as $object)
                        {
-                               if ($record['name'] == 'Wattage consuption')
+                               $objectData = spotEntity ('object', $object);
+                               amplifyCell ($objectData);
+                               foreach (getAttrValues ($objectData['id']) as $record)
                                {
-                                       $rackTotalWattage += $record['value'];
+                                       if ($record['name'] == 'Wattage consumption')
+                                       {
+                                               $rackTotalWattage += $record['value'];
+                                       }
                                }
                        }
+                       if ($rackListIdx % $maxPerRow == 0)
+                       {
+                               if ($rackListIdx > 0)
+                                       echo '</tr>';
+                               echo '<tr>';
+                       }
+                       echo "<td align=center class=row_${order}><a href='".makeHref(array('page'=>'rack', 'rack_id'=>$rack['id']))."'>";
+                       echo "<img border=0 width=${rackwidth} height=" . (getRackImageHeight ($rack['height']) * getConfigVar ('ROW_SCALE'));
+                       echo " title='${rack['height']} units'";
+                       echo "src='?module=image&img=minirack&rack_id=${rack['id']}'>";
+                       echo "<br>${rack['name']} ($rackTotalWattage)</a></td>";
+                       $order = $nextorder[$order];
+                       $rackListIdx++;
+                       $rowTotalWattage += $rackTotalWattage;
                }
-               if ($rackListIdx % $maxPerRow == 0)
+
+               echo "</tr><tr><td align=center colspan=";
+               print (count($rackList));
+               echo "><br><b>The row total for attribute Wattage consuption is:  $rowTotalWattage</b></td>\n";
+
+               echo "</tr></table>\n";
+               finishPortlet();
+       }
+       echo "</td></tr></table>";
+}
+
+function renderPortletWattConsumption ( $info )
+{
+       $rackTotalWattage = 0;
+       $rackData = spotEntity ('rack', $info['id']);
+       amplifyCell ($rackData);
+       $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
+       foreach ($rackData['mountedObjects'] as $object)
+       {
+               $objectData = spotEntity ('object', $object);
+               amplifyCell ($objectData);
+               foreach (getAttrValues ($objectData['id']) as $record)
                {
-                       if ($rackListIdx > 0)
-                               echo '</tr>';
-                       echo '<tr>';
+                       if ($record['name'] == 'Wattage consumption')
+                       {
+                               $rackTotalWattage += $record['value'];
+                       }
                }
-               echo "<td align=center class=row_${order}><a href='".makeHref(array('page'=>'rack', 'rack_id'=>$rack['id']))."'>";
-               echo "<img border=0 width=${rackwidth} height=" . (getRackImageHeight ($rack['height']) * getConfigVar ('ROW_SCALE'));
-               echo " title='${rack['height']} units'";
-               echo "src='?module=image&img=minirack&rack_id=${rack['id']}'>";
-               echo "<br>${rack['name']} ($rackTotalWattage)</a></td>";
-               $order = $nextorder[$order];
-               $rackListIdx++;
-               $rowTotalWattage += $rackTotalWattage;
        }
-
-       echo "</tr><tr><td align=center colspan=";
-       print (count($rackList));
-       echo "><br><b>The row total for attribute Wattage consuption is:  $rowTotalWattage</b></td>\n";
-
+       startPortlet ('Wattage Consumption' );
+       echo "<table border=0 cellspacing=5 align='center'><tr>";
+       echo "<td>The total for attribute Wattage consuption is:  <b>$rackTotalWattage</b></td>\n";
        echo "</tr></table>\n";
        finishPortlet();
-       echo "</td></tr></table>";
 }
 
 ?>