source: trunk/web/addons/toga/overview.php @ 118

Last change on this file since 118 was 118, checked in by bastiaans, 19 years ago

toga/styles.css, toga/overview.php, toga/templates/overview.tpl:

  • Cosmetic HTML changes
File size: 7.4 KB
RevLine 
[113]1<?php
[117]2global $GANGLIA_PATH, $clustername, $tpl;
[114]3
4$data_gatherer = new DataGatherer();
5
[117]6//$tpl->assign( "self", "./index.php" );
[116]7$tpl->assign( "clustername", $clustername );
8$tpl->assign( "clusterimage", "./image.php?c=".rawurlencode($clustername)."&view=big-clusterimage" );
9
[114]10$data_gatherer->parseXML();
[117]11
[114]12$heartbeat = $data_gatherer->getHeartbeat();
13$jobs = $data_gatherer->getJobs();
14$nodes = $data_gatherer->getNodes();
15
16$tpl->assign("heartbeat", makeDate( $heartbeat ) );
17
18$pie = drawPie();
19$tpl->assign("pie", $pie );
20
21function makeTime( $time ) {
22
23        $days = intval( $time / 86400 );
[115]24        $time = $days ? $time % ($days * 86400) : $time;
25
[114]26        if( $days > 0 ) {
27                if( $days > 1 )
[116]28                        $date_str .= $days . ' days - ';
[114]29                else
[116]30                        $date_str .= $days . ' day - ';
[114]31        }
[115]32
[114]33        $hours = intval( $time / 3600 );
[115]34        $time = $hours ? $time % ($hours * 3600) : $time;
35
[114]36        if( $hours > 0 ) {
37                $date_str .= $hours . ':';
38                $date_unit = ' hours';
39        }
[115]40               
[114]41        $minutes = intval( $time / 60 );
[115]42        $seconds = $minutes ? $time % ($minutes * 60) : $time;
43
[114]44        if( $minutes > 0 ) {
[115]45
46                if( $minutes >= 10 )
47                        $date_str .= $minutes . ':';
48                else
49                        $date_str .= '0' . $minutes . ':';
50
[114]51                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
[115]52        } else if( $days > 0 or $hours > 0 ) {
53                $date_str .= '00:';
54                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
[114]55        }
[115]56
[114]57        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
58
[115]59        if( $seconds > 0 ) {
60
61                if( $seconds >= 10 )
62                        $date_str .= $seconds . ' ' . $date_unit;
63                else
64                        $date_str .= '0' . $seconds . ' ' . $date_unit;
65                       
66        } else if ( $days > 0 or $hours > 0 or $minutes > 0 )
67                $date_str .= '00 ' . $date_unit;
68
[114]69        return $date_str;
70}
71
72function makeDate( $time ) {
73        return strftime( "%a %d %b %Y %H:%M:%S", $time );
74}
75
76function colorRed( $color ) {
77        return substr( $color, 0, 2 );
78}
79function colorGreen( $color ) {
80        return substr( $color, 2, 2 );
81}
82function colorBlue( $color ) {
83        return substr( $color, 4, 2 );
84}
85
86function colorDiffer( $first, $second ) {
87
88        // Make sure these two colors differ atleast 50 R/G/B
89        $min_diff = 50;
90
91        $c1r = decHex( colorRed( $first ) );
92        $c1g = decHex( colorGreen( $first ) );
93        $c1b = decHex( colorBlue( $first ) );
94
95        $c2r = decHex( colorRed( $second ) );
96        $c2g = decHex( colorGreen( $second ) );
97        $c2b = decHex( colorBlue( $second ) );
98
99        $rdiff = ($c1r >= $c2r) ? $c1r - $c2r : $c2r - $c1r;
100        $gdiff = ($c1g >= $c2g) ? $c1g - $c2g : $c2g - $c1g;
101        $bdiff = ($c1b >= $c2b) ? $c1b - $c2b : $c2b - $c1b;
102
103        if( $rdiff >= $min_diff or $gdiff >= $min_diff or $bdiff >= $min_diff )
104                return TRUE;
105        else
106                return FALSE;
107}
108
109function randomColor( $known_colors ) {
110
111        $start = hexdec( "004E00" );
112        $end = hexdec( "FFFFFF" );
113
114        if( count( $known_colors ) == 0 )
115                return dechex(rand( $start, $end ));
116
117        $color_changed = TRUE;
118
119        while( $color_changed ) {
120
121                $color_changed = FALSE;
122
123                foreach( $known_colors as $old ) {
124
125                        if( !isset( $new ) )
126                                $new = rand( $start, $end );
127
128                        if( !colorDiffer( dechex( $new ), $old ) )
129
130                                while( !colorDiffer( $new, $old ) ) {
131
132                                        $new = rand( $start, $end );
133                                        $color_changed = TRUE;
134                                }
135                }
136        }
137
138        // Whoa! Actually found a good color ;)
139        return dechex( $new );
140}
141
142function drawPie() {
143
144        global $jobs, $nodes;
145
146        $pie_args = "title=" . rawurlencode("Cluster Jobload");
147        $pie_args .= "&size=250x150";
148
149        $queues = array();
150        $nr_jobs = count( $jobs );
151        $nr_nodes = count( $nodes );
152
153        $emptynodes = 0;
154
155        foreach( $nodes as $node ) {
156
157                $myjobs = $node->getJobs();
158
159                if( count( $myjobs ) == 0 )
160                        $emptynodes++;
161                else
162                        $nodes_jobs = $nodes_jobs + count( $myjobs );
163        }
164
165        $empty_percentage = ($emptynodes / $nr_nodes) * 100;
166        $job_percentage = 100 - $empty_percentage; 
167
168        $color = randomColor( $qcolors );
169        $qcolors[] = $color;
170        $pie_args .= "&free=$empty_percentage,$color";
171
172        foreach( $jobs as $jobid => $jobattrs ) {
173
174                $qname = $jobattrs[queue];
175
176                if( !array_search( $qname, $queues ) ) {
177
178                        if( !isset( $queues[$qname] ) )
179                                $queues[$qname] = array();
180
181                        $queues[$qname][] = $jobid;
182                }
183        }
184
185        $qcolors = array();
186        foreach( $queues as $queue => $myjobs ) {
187
188                $qjobs = count ( $myjobs );
189                $percentage = ( $qjobs / $nr_jobs ) * $job_percentage;
190                $color = randomColor( $qcolors );
191                $qcolors[] = $color;
192                $pie_args .= "&$queue=$percentage,$color";
193        }
194        $pie = "../../pie.php?$pie_args";
195
196        return $pie;
197}
198
199
[117]200function sortJobs( $jobs, $sortby, $sortorder ) {
[114]201
[117]202        $sorted = array();
[114]203
[117]204        $cmp = create_function( '$a, $b', 
205                "global \$sortby, \$sortorder;".
[116]206
[117]207                "if( \$a == \$b ) return 0;".
[116]208
[117]209                "if (\$sortorder==\"desc\")".
210                        "return ( \$a < \$b ) ? 1 : -1;".
211                "else if (\$sortorder==\"asc\")".
212                        "return ( \$a > \$b ) ? 1 : -1;" );
213
214        foreach( $jobs as $jobid => $jobattrs ) {
215
216                        $state = $jobattrs[status];
217                        $user = $jobattrs[owner];
218                        $queue = $jobattrs[queue];
219                        $name = $jobattrs[name];
220                        $req_cpu = $jobattrs[requested_time];
221                        $req_memory = $jobattrs[requested_memory];
222                        $nodes = count( $jobattrs[nodes] );
223                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
224                        $cpus = $nodes * $ppn;
225                        $start_time = (int) $jobattrs[start_timestamp];
226                        $runningtime = $report_time - $start_time;
227
228                        switch( $sortby ) {
229                                case "id":
230                                        $sorted[$jobid] = $jobid;
231                                        break;
232
233                                case "state":
234                                        $sorted[$jobid] = $state;
235                                        break;
236
237                                case "user":
238                                        $sorted[$jobid] = $user;
239                                        break;
240
241                                case "queue":
242                                        $sorted[$jobid] = $queue;
243                                        break;
244
245                                case "name":
246                                        $sorted[$jobid] = $name;
247                                        break;
248
249                                case "req_cpu":
250                                        $sorted[$jobid] = $req_cpu;
251                                        break;
252
253                                case "req_mem":
254                                        $sorted[$jobid] = $req_memory;
255                                        break;
256
257                                case "nodes":
258                                        $sorted[$jobid] = $nodes;
259                                        break;
260
261                                case "cpus":
262                                        $sorted[$jobid] = $cpus;
263                                        break;
264
265                                case "start":
266                                        $sorted[$jobid] = $start_time;
267                                        break;
268
269                                case "runningtime":
270                                        $sorted[$jobid] = $runningtime;
271                                        break;
272
273                                default:
274                                        break;
275
276                        }
277        }
278
279        uasort( $sorted, $cmp );
280
281        return $sorted;
282}
283
284function makeOverview() {
285
286        global $jobs, $nodes, $heartbeat, $clustername, $tpl;
287        global $sortorder, $sortby;
288
289        $tpl->assign("sortorder", $sortorder );
290        $tpl->assign("sortby", $sortby );
291
292        $sorted_jobs = sortJobs( $jobs, $sortby, $sortorder );
293
[118]294        $even = 1;
295
[117]296        foreach( $sorted_jobs as $jobid => $sortdec ) {
297
298                $report_time = $jobs[$jobid][reported];
299
300                if( $report_time == $heartbeat ) {
301
302                        $tpl->newBlock("node");
303                        $tpl->assign( "clustername", $clustername );
304                        $tpl->assign("id", $jobid );
305                        $tpl->assign("state", $jobs[$jobid][status] );
306                        $tpl->assign("user", $jobs[$jobid][owner] );
307                        $tpl->assign("queue", $jobs[$jobid][queue] );
308                        $tpl->assign("name", $jobs[$jobid][name] );
309                        $tpl->assign("req_cpu", $jobs[$jobid][requested_time] );
310                        $tpl->assign("req_memory", $jobs[$jobid][requested_memory] );
311                        $nodes = count( $jobs[$jobid][nodes] );
312                        $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
313                        $cpus = $nodes * $ppn;
314                        $tpl->assign("nodes", $nodes );
315                        $tpl->assign("cpus", $cpus );
316                        $start_time = (int) $jobs[$jobid][start_timestamp];
317
[118]318                        if( $even ) {
319
320                                $tpl->assign("nodeclass", "even");
321                                $even = 0;
322                        } else {
323
324                                $tpl->assign("nodeclass", "odd");
325                                $even = 1;
326                        }
327
[117]328                        if( $start_time ) {
329
330                                $runningtime = makeTime( $report_time - $start_time );
331                                $tpl->assign("started", makeDate( $start_time ) );
332                                $tpl->assign("runningtime", $runningtime );
333                        }
[116]334                }
[114]335        }
336}
[113]337?>
Note: See TracBrowser for help on using the repository browser.