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

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

web/addons/toga/templates/overview.tpl:

  • Setup javascript for multiple filters
  • Cosmetic updates

web/addons/toga/index.php:

  • Rearranged filter order

web/addons/toga/overview.php:

  • Changed for multiple filters
  • Cosmetic updates

web/addons/toga/image.php:

  • Changed for multiple filters

web/addons/toga/libtoga.php:

  • Changed filterNodes() for multiple filters
File size: 9.6 KB
RevLine 
[113]1<?php
[122]2global $GANGLIA_PATH, $clustername, $tpl, $filter, $cluster, $get_metric_string, $cluster_url;
[114]3
4$data_gatherer = new DataGatherer();
5
[117]6//$tpl->assign( "self", "./index.php" );
[116]7$tpl->assign( "clustername", $clustername );
8
[114]9$data_gatherer->parseXML();
[117]10
[114]11$heartbeat = $data_gatherer->getHeartbeat();
12$jobs = $data_gatherer->getJobs();
13$nodes = $data_gatherer->getNodes();
[124]14$cpus = $data_gatherer->getCpus();
[114]15
[122]16$filter_image_url = "";
17
[119]18foreach( $filter as $filtername => $filtervalue ) {
19        $tpl->assign( "f_".$filtername, $filtervalue );
[122]20        $filter_image_url .= "&$filtername=$filtervalue";
[119]21}
22
[122]23$tpl->assign( "clusterimage", "./image.php?c=".rawurlencode($clustername)."&view=big-clusterimage".$filter_image_url );
24
[114]25
[124]26$pie = drawClusterPie();
[114]27$tpl->assign("pie", $pie );
28
[122]29//if( !array_key_exists( 'id', $filter ) ) {
30
31//      $graph_args = "c=$cluster_url&$get_metric_string&st=$cluster[LOCALTIME]";
32//      $tpl->newBlock( "average_graphs" );
33//      $tpl->assign( "graph_args", $graph_args );
34//}
35
[120]36function timeToEpoch( $time ) {
37
38        $time_fields = explode( ':', $time );
39
40        if( count($time_fields) == 3 ) {
41
42                $hours = $time_fields[0];
43                $minutes = $time_fields[1];
44                $seconds = $time_fields[2];
45
46                $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
47                return $myepoch;
48        }
49}
50
[114]51function makeTime( $time ) {
52
53        $days = intval( $time / 86400 );
[115]54        $time = $days ? $time % ($days * 86400) : $time;
55
[114]56        if( $days > 0 ) {
57                if( $days > 1 )
[116]58                        $date_str .= $days . ' days - ';
[114]59                else
[116]60                        $date_str .= $days . ' day - ';
[114]61        }
[115]62
[114]63        $hours = intval( $time / 3600 );
[115]64        $time = $hours ? $time % ($hours * 3600) : $time;
65
[114]66        if( $hours > 0 ) {
67                $date_str .= $hours . ':';
68                $date_unit = ' hours';
69        }
[115]70               
[114]71        $minutes = intval( $time / 60 );
[115]72        $seconds = $minutes ? $time % ($minutes * 60) : $time;
73
[114]74        if( $minutes > 0 ) {
[115]75
76                if( $minutes >= 10 )
77                        $date_str .= $minutes . ':';
78                else
79                        $date_str .= '0' . $minutes . ':';
80
[114]81                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
[115]82        } else if( $days > 0 or $hours > 0 ) {
83                $date_str .= '00:';
84                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
[114]85        }
[115]86
[114]87        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
88
[115]89        if( $seconds > 0 ) {
90
91                if( $seconds >= 10 )
92                        $date_str .= $seconds . ' ' . $date_unit;
93                else
94                        $date_str .= '0' . $seconds . ' ' . $date_unit;
95                       
96        } else if ( $days > 0 or $hours > 0 or $minutes > 0 )
97                $date_str .= '00 ' . $date_unit;
98
[114]99        return $date_str;
100}
101
102function makeDate( $time ) {
103        return strftime( "%a %d %b %Y %H:%M:%S", $time );
104}
105
106function colorRed( $color ) {
107        return substr( $color, 0, 2 );
108}
109function colorGreen( $color ) {
110        return substr( $color, 2, 2 );
111}
112function colorBlue( $color ) {
113        return substr( $color, 4, 2 );
114}
115
116function colorDiffer( $first, $second ) {
117
118        // Make sure these two colors differ atleast 50 R/G/B
119        $min_diff = 50;
120
121        $c1r = decHex( colorRed( $first ) );
122        $c1g = decHex( colorGreen( $first ) );
123        $c1b = decHex( colorBlue( $first ) );
124
125        $c2r = decHex( colorRed( $second ) );
126        $c2g = decHex( colorGreen( $second ) );
127        $c2b = decHex( colorBlue( $second ) );
128
129        $rdiff = ($c1r >= $c2r) ? $c1r - $c2r : $c2r - $c1r;
130        $gdiff = ($c1g >= $c2g) ? $c1g - $c2g : $c2g - $c1g;
131        $bdiff = ($c1b >= $c2b) ? $c1b - $c2b : $c2b - $c1b;
132
133        if( $rdiff >= $min_diff or $gdiff >= $min_diff or $bdiff >= $min_diff )
134                return TRUE;
135        else
136                return FALSE;
137}
138
139function randomColor( $known_colors ) {
140
141        $start = hexdec( "004E00" );
142        $end = hexdec( "FFFFFF" );
143
144        if( count( $known_colors ) == 0 )
145                return dechex(rand( $start, $end ));
146
147        $color_changed = TRUE;
148
149        while( $color_changed ) {
150
151                $color_changed = FALSE;
152
153                foreach( $known_colors as $old ) {
154
155                        if( !isset( $new ) )
156                                $new = rand( $start, $end );
157
158                        if( !colorDiffer( dechex( $new ), $old ) )
159
160                                while( !colorDiffer( $new, $old ) ) {
161
162                                        $new = rand( $start, $end );
163                                        $color_changed = TRUE;
164                                }
165                }
166        }
167
168        // Whoa! Actually found a good color ;)
169        return dechex( $new );
170}
171
172
[124]173
174function drawClusterPie() {
175
[114]176        global $jobs, $nodes;
177
[124]178        $pie_args = "title=" . rawurlencode("Cluster queue usage");
[114]179        $pie_args .= "&size=250x150";
180
181        $queues = array();
182        $nr_jobs = count( $jobs );
183        $nr_nodes = count( $nodes );
184
185        $emptynodes = 0;
186
[123]187        $job_weight = array();
188
[114]189        foreach( $nodes as $node ) {
190
191                $myjobs = $node->getJobs();
192
193                if( count( $myjobs ) == 0 )
194                        $emptynodes++;
195        }
[123]196        $used_nodes = $nr_nodes - $emptynodes;
[114]197
198        $empty_percentage = ($emptynodes / $nr_nodes) * 100;
199        $job_percentage = 100 - $empty_percentage; 
200
201        $color = randomColor( $qcolors );
202        $qcolors[] = $color;
203        $pie_args .= "&free=$empty_percentage,$color";
204
[123]205        foreach( $nodes as $node ) {
[114]206
[123]207                $node_jobs = $node->getJobs();
208                $nr_node_jobs = count( $node_jobs );
209                $myhost = $node->getHostname();
[114]210
[123]211                foreach( $node_jobs as $myjob ) {
[114]212
[123]213                        // Determine the weight of this job on the node it is running
214                        // - what percentage of the node is in use by this job
215                        //
216                        $job_weight[$myjob] = ( 100 / count( $node_jobs ) ) / 100;
217                        $qname = $jobs[$myjob][queue];
218
[114]219                        if( !isset( $queues[$qname] ) )
[123]220                                $queues[$qname] = $job_weight[$myjob];
221                        else
222                                $queues[$qname] = $queues[$qname] + $job_weight[$myjob];
[114]223                }
224        }
225
226        $qcolors = array();
[123]227        foreach( $queues as $queue => $totalweight) {
[114]228
[123]229                $percentage = ( $totalweight / $used_nodes ) * $job_percentage;
230               
[114]231                $color = randomColor( $qcolors );
232                $qcolors[] = $color;
233                $pie_args .= "&$queue=$percentage,$color";
234        }
235        $pie = "../../pie.php?$pie_args";
236
237        return $pie;
238}
239
240
[117]241function sortJobs( $jobs, $sortby, $sortorder ) {
[114]242
[117]243        $sorted = array();
[114]244
[117]245        $cmp = create_function( '$a, $b', 
246                "global \$sortby, \$sortorder;".
[116]247
[117]248                "if( \$a == \$b ) return 0;".
[116]249
[117]250                "if (\$sortorder==\"desc\")".
251                        "return ( \$a < \$b ) ? 1 : -1;".
252                "else if (\$sortorder==\"asc\")".
253                        "return ( \$a > \$b ) ? 1 : -1;" );
254
255        foreach( $jobs as $jobid => $jobattrs ) {
256
257                        $state = $jobattrs[status];
258                        $user = $jobattrs[owner];
259                        $queue = $jobattrs[queue];
260                        $name = $jobattrs[name];
261                        $req_cpu = $jobattrs[requested_time];
262                        $req_memory = $jobattrs[requested_memory];
263                        $nodes = count( $jobattrs[nodes] );
264                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
265                        $cpus = $nodes * $ppn;
266                        $start_time = (int) $jobattrs[start_timestamp];
267                        $runningtime = $report_time - $start_time;
268
269                        switch( $sortby ) {
270                                case "id":
271                                        $sorted[$jobid] = $jobid;
272                                        break;
273
274                                case "state":
275                                        $sorted[$jobid] = $state;
276                                        break;
277
278                                case "user":
279                                        $sorted[$jobid] = $user;
280                                        break;
281
282                                case "queue":
283                                        $sorted[$jobid] = $queue;
284                                        break;
285
286                                case "name":
287                                        $sorted[$jobid] = $name;
288                                        break;
289
290                                case "req_cpu":
291                                        $sorted[$jobid] = $req_cpu;
292                                        break;
293
294                                case "req_mem":
295                                        $sorted[$jobid] = $req_memory;
296                                        break;
297
298                                case "nodes":
299                                        $sorted[$jobid] = $nodes;
300                                        break;
301
302                                case "cpus":
303                                        $sorted[$jobid] = $cpus;
304                                        break;
305
306                                case "start":
307                                        $sorted[$jobid] = $start_time;
308                                        break;
309
310                                case "runningtime":
311                                        $sorted[$jobid] = $runningtime;
312                                        break;
313
314                                default:
315                                        break;
316
317                        }
318        }
319
320        uasort( $sorted, $cmp );
321
322        return $sorted;
323}
324
325function makeOverview() {
326
327        global $jobs, $nodes, $heartbeat, $clustername, $tpl;
[119]328        global $sortorder, $sortby, $filter;
[117]329
330        $tpl->assign("sortorder", $sortorder );
331        $tpl->assign("sortby", $sortby );
332
333        $sorted_jobs = sortJobs( $jobs, $sortby, $sortorder );
334
[118]335        $even = 1;
336
[124]337        $overview_jobs = count( $sorted_jobs );
338        $overview_nodes = count( $nodes );
339        $overview_cpus = 0;
340
341        $f_cpus = 0;
342        $f_jobs = 0;
343
[117]344        foreach( $sorted_jobs as $jobid => $sortdec ) {
345
346                $report_time = $jobs[$jobid][reported];
347
[124]348                $nodes = count( $jobs[$jobid][nodes] );
349                $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
350                $cpus = $nodes * $ppn;
351
352                $overview_cpus = $overview_cpus + $cpus;
353
[117]354                if( $report_time == $heartbeat ) {
355
[124]356                        $display_job = 1;
[117]357
[119]358                        foreach( $filter as $filtername=>$filtervalue ) {
[118]359
[124]360                                if( $filtername == 'id' && $jobid != $filtervalue )
361                                        $display_job = 0;
362                                else if( $filtername == 'state' && $jobs[$jobid][status] != $filtervalue )
363                                        $display_job = 0;
364                                else if( $filtername == 'queue' && $jobs[$jobid][queue] != $filtervalue )
365                                        $display_job = 0;
366                                else if( $filtername == 'user' && $jobs[$jobid][owner] != $filtervalue )
367                                        $display_job = 0;
[118]368                        }
369
[119]370                        if( $display_job ) {
[117]371
[124]372
[119]373                                $tpl->newBlock("node");
374                                $tpl->assign( "clustername", $clustername );
375                                $tpl->assign("id", $jobid );
376                                $tpl->assign("state", $jobs[$jobid][status] );
377                                $tpl->assign("user", $jobs[$jobid][owner] );
378                                $tpl->assign("queue", $jobs[$jobid][queue] );
379                                $tpl->assign("name", $jobs[$jobid][name] );
[120]380                                $tpl->assign("req_cpu", makeTime( timeToEpoch( $jobs[$jobid][requested_time] ) ) );
[119]381                                $tpl->assign("req_memory", $jobs[$jobid][requested_memory] );
382                                $nodes = count( $jobs[$jobid][nodes] );
383                                $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
384                                $cpus = $nodes * $ppn;
385                                $tpl->assign("nodes", $nodes );
386                                $tpl->assign("cpus", $cpus );
387                                $start_time = (int) $jobs[$jobid][start_timestamp];
388
[124]389                                $f_cpus = $f_cpus + $cpus;
390                                $f_jobs++;
391
[119]392                                if( $even ) {
393
394                                        $tpl->assign("nodeclass", "even");
395                                        $even = 0;
396                                } else {
397
398                                        $tpl->assign("nodeclass", "odd");
399                                        $even = 1;
400                                }
401
402                                if( $start_time ) {
403
404                                        $runningtime = makeTime( $report_time - $start_time );
405                                        $tpl->assign("started", makeDate( $start_time ) );
406                                        $tpl->assign("runningtime", $runningtime );
407                                }
[117]408                        }
[116]409                }
[114]410        }
[124]411
412        $tpl->assignGlobal("cpus_nr", $overview_cpus );
413        $tpl->assignGlobal("jobs_nr", $overview_jobs );
414        $tpl->assignGlobal("nodes_nr", $overview_nodes );
415        $tpl->assignGlobal("report_time", makeDate( $heartbeat));
416       
417        $tpl->assignGlobal("f_cpus_nr", $f_cpus );
418        $tpl->assignGlobal("f_jobs_nr", $f_jobs );
[114]419}
[113]420?>
Note: See TracBrowser for help on using the repository browser.