wattage_consumption: fix to work with 0.20.4
[racktables-contribs] / wattage_consumption.php
CommitLineData
fba27978
CB
1<?php
2
3//-----------------------------------------------------------------------------------
4// Wattage Consumption - written by curtisb
b2fa3593
MW
5//
6// Updated for Racktables-0.20.4 by Mark Wilkinson
7// bonus of Rack Wattage portlet in Rack Page if portlet patch applied (bugs.racktables.org ID 691)
8//
9//
10// Usage - Create attribute 'Wattage consumption' and map to Servers, Chassis, etc
11// Set power values for servers,etc.
12// View Report page (or Rack page)
13//
fba27978
CB
14$tab['reports']['watts_per_row'] = 'Enviromental Totals';
15$tabhandler['reports']['watts_per_row'] = 'getWattsPerRow';
b2fa3593
MW
16if ( function_exists('registerPortletHandler') )
17{
18 registerPortletHandler( 'rack', 'default', 'left', 'Wattage Consumption', 'renderPortletWattConsumption');
19}
fba27978
CB
20
21function getWattsPerRow ()
22{
b62db262
DO
23 // assertions
24 // find the needed attributes
fba27978 25
b62db262 26 global $nextorder;
fba27978 27
b62db262
DO
28 // Was this function called with a specific row_id?
29 if (isset ($_REQUEST['row_id']))
30 {
31 assertStringArg ('row_id');
32 $row_toshow = $_REQUEST['row_id'];
33 }
34 else
35 {
b2fa3593 36 $row_toshow = -1;
b62db262 37 }
fba27978 38
b62db262
DO
39 //from renderRackspace(), interface.php:151
40 $found_racks = array();
41 $rows = array();
42 $cellfilter = getCellFilter();
43 $rackCount = 0;
44 $order = 'odd';
45 // get rackspace information
b2fa3593
MW
46 foreach (getAllRows() as $row_id => $rowInfo)
47 {
b62db262 48 $rackList = filterCellList (listCells ('rack', $row_id), $cellfilter['expression']);
b2fa3593
MW
49 $found_racks = array_merge ($found_racks, $rackList);
50 $rows[] = array (
51 'location_id' => $rowInfo['location_id'],
52 'location_name' => $rowInfo['location_name'],
b62db262 53 'row_id' => $row_id,
b2fa3593 54 'row_name' => $rowInfo['name'],
b62db262
DO
55 'racks' => $rackList
56 );
57 $rackCount += count($rackList);
58 }
fba27978 59
b62db262
DO
60 // Main layout starts.
61 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
fba27978 62
b62db262 63 // Left portlet with list of rows.
b2fa3593 64 echo "<tr><td class=pcleft width='50%'>";
b62db262
DO
65 startPortlet ('Rack Rows (' . count ($rows) . ')');
66 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
67 foreach ($rows as $row)
68 {
69 $row_id = $row['row_id'];
70 $row_name = $row['row_name'];
b2fa3593 71 $row_location = $row['location_name'];
b62db262 72 $rackList = $row['racks'];
fba27978 73
b62db262
DO
74 echo "<tr class=row_${order}><td width='20%'></td><td class=tdleft>";
75 if (!count ($rackList))
76 {
b2fa3593 77 echo "${row_location} - ${row_name} (empty row)";
b62db262
DO
78 }
79 else
80 {
b2fa3593 81 echo "<a href='" . makeHref(array('page'=>'reports', 'tab'=>'watts_per_row', 'row_id'=>$row_id)) . "'>${row_location} - ${row_name}</a>";
b62db262
DO
82 }
83 echo "<td><tr>\n";
84 $order = $nextorder[$order];
85 }
fba27978 86
b62db262
DO
87 echo "</td></tr>\n";
88 echo "</table><br>\n";
89 finishPortlet();
fba27978 90
b62db262 91 echo "</td><td class=pcright>";
fba27978 92
b62db262 93 // Right Portlet: Draw the racks in the selected row
b2fa3593 94 if ( $row_toshow > -1 )
b62db262 95 {
b2fa3593
MW
96 $rowInfo = getRowInfo ($row_toshow);
97 $cellfilter = getCellFilter();
98 $rackList = filterCellList (listCells ('rack', $row_toshow), $cellfilter['expression']);
99
100 global $nextorder;
101 $rackwidth = getRackImageWidth() * getConfigVar ('ROW_SCALE');
102 // Maximum number of racks per row is proportionally less, but at least 1.
103 $maxPerRow = max (floor (getConfigVar ('RACKS_PER_ROW') / getConfigVar ('ROW_SCALE')), 1);
104 $rackListIdx = 0;
105 $rowTotalWattage = 0;
106 $order = 'odd';
107 startPortlet ('Racks within '. $rowInfo['name'] . ' (' . count($rackList) . ')' );
108 echo "<table border=0 cellspacing=5 align='center'><tr>";
109 foreach ($rackList as $rack)
b62db262 110 {
b2fa3593
MW
111 $rackTotalWattage = 0;
112 // see renderRack(), interface.php:311
113 $rackData = spotEntity ('rack', $rack['id']);
114 amplifyCell ($rackData);
115 $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
116 foreach ($rackData['mountedObjects'] as $object)
b62db262 117 {
b2fa3593
MW
118 $objectData = spotEntity ('object', $object);
119 amplifyCell ($objectData);
120 foreach (getAttrValues ($objectData['id']) as $record)
b62db262 121 {
b2fa3593
MW
122 if ($record['name'] == 'Wattage consumption')
123 {
124 $rackTotalWattage += $record['value'];
125 }
b62db262
DO
126 }
127 }
b2fa3593
MW
128 if ($rackListIdx % $maxPerRow == 0)
129 {
130 if ($rackListIdx > 0)
131 echo '</tr>';
132 echo '<tr>';
133 }
134 echo "<td align=center class=row_${order}><a href='".makeHref(array('page'=>'rack', 'rack_id'=>$rack['id']))."'>";
135 echo "<img border=0 width=${rackwidth} height=" . (getRackImageHeight ($rack['height']) * getConfigVar ('ROW_SCALE'));
136 echo " title='${rack['height']} units'";
137 echo "src='?module=image&img=minirack&rack_id=${rack['id']}'>";
138 echo "<br>${rack['name']} ($rackTotalWattage)</a></td>";
139 $order = $nextorder[$order];
140 $rackListIdx++;
141 $rowTotalWattage += $rackTotalWattage;
b62db262 142 }
b2fa3593
MW
143
144 echo "</tr><tr><td align=center colspan=";
145 print (count($rackList));
146 echo "><br><b>The row total for attribute Wattage consuption is: $rowTotalWattage</b></td>\n";
147
148 echo "</tr></table>\n";
149 finishPortlet();
150 }
151 echo "</td></tr></table>";
152}
153
154function renderPortletWattConsumption ( $info )
155{
156 $rackTotalWattage = 0;
157 $rackData = spotEntity ('rack', $info['id']);
158 amplifyCell ($rackData);
159 $objectChildren = getEntityRelatives ('children', 'object', $objectData['id']);
160 foreach ($rackData['mountedObjects'] as $object)
161 {
162 $objectData = spotEntity ('object', $object);
163 amplifyCell ($objectData);
164 foreach (getAttrValues ($objectData['id']) as $record)
b62db262 165 {
b2fa3593
MW
166 if ($record['name'] == 'Wattage consumption')
167 {
168 $rackTotalWattage += $record['value'];
169 }
b62db262 170 }
b62db262 171 }
b2fa3593
MW
172 startPortlet ('Wattage Consumption' );
173 echo "<table border=0 cellspacing=5 align='center'><tr>";
174 echo "<td>The total for attribute Wattage consuption is: <b>$rackTotalWattage</b></td>\n";
b62db262
DO
175 echo "</tr></table>\n";
176 finishPortlet();
fba27978
CB
177}
178
179?>