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

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

toga/overview.php:

  • Fixed time display of queued jobs
File size: 5.5 KB
Line 
1<?php
2
3$my_dir = getcwd();
4
5include_once "./libtoga.php";
6
7global $GANGLIA_PATH;
8chdir( $GANGLIA_PATH );
9include_once "./class.TemplatePower.inc.php";
10chdir( $my_dir );
11
12$httpvars = new HTTPVariables( $HTTP_GET_VARS );
13$clustername = $httpvars->getClusterName();
14printf( "clustername = %s\n", $clustername );
15$queue_select = $httpvars->getHttpVar( "queue" );
16printf( "queue = %s\n", $queue );
17
18$data_gatherer = new DataGatherer();
19
20$tpl = new TemplatePower("templates/overview.tpl");
21$tpl->prepare();
22
23$tpl->assign( "self", "./index.php" );
24$tpl->assign( "clustername", $clustername );
25
26$tpl->assign( "clusterimage", "./image.php?c=".rawurlencode($clustername)."&view=big-clusterimage" );
27
28$data_gatherer->parseXML();
29$heartbeat = $data_gatherer->getHeartbeat();
30$jobs = $data_gatherer->getJobs();
31$nodes = $data_gatherer->getNodes();
32
33$tpl->assign("heartbeat", makeDate( $heartbeat ) );
34
35$pie = drawPie();
36$tpl->assign("pie", $pie );
37
38function makeTime( $time ) {
39
40        $days = intval( $time / 86400 );
41        $time = $days ? $time % ($days * 86400) : $time;
42
43        if( $days > 0 ) {
44                if( $days > 1 )
45                        $date_str .= $days . ' days - ';
46                else
47                        $date_str .= $days . ' day - ';
48        }
49
50        $hours = intval( $time / 3600 );
51        $time = $hours ? $time % ($hours * 3600) : $time;
52
53        if( $hours > 0 ) {
54                $date_str .= $hours . ':';
55                $date_unit = ' hours';
56        }
57               
58        $minutes = intval( $time / 60 );
59        $seconds = $minutes ? $time % ($minutes * 60) : $time;
60
61        if( $minutes > 0 ) {
62
63                if( $minutes >= 10 )
64                        $date_str .= $minutes . ':';
65                else
66                        $date_str .= '0' . $minutes . ':';
67
68                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
69        } else if( $days > 0 or $hours > 0 ) {
70                $date_str .= '00:';
71                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
72        }
73
74        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
75
76        if( $seconds > 0 ) {
77
78                if( $seconds >= 10 )
79                        $date_str .= $seconds . ' ' . $date_unit;
80                else
81                        $date_str .= '0' . $seconds . ' ' . $date_unit;
82                       
83        } else if ( $days > 0 or $hours > 0 or $minutes > 0 )
84                $date_str .= '00 ' . $date_unit;
85
86        return $date_str;
87}
88
89function makeDate( $time ) {
90        return strftime( "%a %d %b %Y %H:%M:%S", $time );
91}
92
93function colorRed( $color ) {
94        return substr( $color, 0, 2 );
95}
96function colorGreen( $color ) {
97        return substr( $color, 2, 2 );
98}
99function colorBlue( $color ) {
100        return substr( $color, 4, 2 );
101}
102
103function colorDiffer( $first, $second ) {
104
105        // Make sure these two colors differ atleast 50 R/G/B
106        $min_diff = 50;
107
108        $c1r = decHex( colorRed( $first ) );
109        $c1g = decHex( colorGreen( $first ) );
110        $c1b = decHex( colorBlue( $first ) );
111
112        $c2r = decHex( colorRed( $second ) );
113        $c2g = decHex( colorGreen( $second ) );
114        $c2b = decHex( colorBlue( $second ) );
115
116        $rdiff = ($c1r >= $c2r) ? $c1r - $c2r : $c2r - $c1r;
117        $gdiff = ($c1g >= $c2g) ? $c1g - $c2g : $c2g - $c1g;
118        $bdiff = ($c1b >= $c2b) ? $c1b - $c2b : $c2b - $c1b;
119
120        if( $rdiff >= $min_diff or $gdiff >= $min_diff or $bdiff >= $min_diff )
121                return TRUE;
122        else
123                return FALSE;
124}
125
126function randomColor( $known_colors ) {
127
128        $start = hexdec( "004E00" );
129        $end = hexdec( "FFFFFF" );
130
131        if( count( $known_colors ) == 0 )
132                return dechex(rand( $start, $end ));
133
134        $color_changed = TRUE;
135
136        while( $color_changed ) {
137
138                $color_changed = FALSE;
139
140                foreach( $known_colors as $old ) {
141
142                        if( !isset( $new ) )
143                                $new = rand( $start, $end );
144
145                        if( !colorDiffer( dechex( $new ), $old ) )
146
147                                while( !colorDiffer( $new, $old ) ) {
148
149                                        $new = rand( $start, $end );
150                                        $color_changed = TRUE;
151                                }
152                }
153        }
154
155        // Whoa! Actually found a good color ;)
156        return dechex( $new );
157}
158
159function drawPie() {
160
161        global $jobs, $nodes;
162
163        $pie_args = "title=" . rawurlencode("Cluster Jobload");
164        $pie_args .= "&size=250x150";
165
166        $queues = array();
167        $nr_jobs = count( $jobs );
168        $nr_nodes = count( $nodes );
169
170        $emptynodes = 0;
171
172        foreach( $nodes as $node ) {
173
174                $myjobs = $node->getJobs();
175
176                if( count( $myjobs ) == 0 )
177                        $emptynodes++;
178                else
179                        $nodes_jobs = $nodes_jobs + count( $myjobs );
180        }
181
182        $empty_percentage = ($emptynodes / $nr_nodes) * 100;
183        $job_percentage = 100 - $empty_percentage; 
184
185        $color = randomColor( $qcolors );
186        $qcolors[] = $color;
187        $pie_args .= "&free=$empty_percentage,$color";
188
189        foreach( $jobs as $jobid => $jobattrs ) {
190
191                $qname = $jobattrs[queue];
192
193                if( !array_search( $qname, $queues ) ) {
194
195                        if( !isset( $queues[$qname] ) )
196                                $queues[$qname] = array();
197
198                        $queues[$qname][] = $jobid;
199                }
200        }
201
202        $qcolors = array();
203        foreach( $queues as $queue => $myjobs ) {
204
205                $qjobs = count ( $myjobs );
206                $percentage = ( $qjobs / $nr_jobs ) * $job_percentage;
207                $color = randomColor( $qcolors );
208                $qcolors[] = $color;
209                $pie_args .= "&$queue=$percentage,$color";
210        }
211        $pie = "../../pie.php?$pie_args";
212
213        return $pie;
214}
215
216foreach( $jobs as $jobid => $jobattrs ) {
217
218        $report_time = $jobattrs[reported];
219
220        if( $report_time == $heartbeat ) {
221
222                $tpl->newBlock("node");
223                $tpl->assign( "clustername", $clustername );
224                $tpl->assign("id", $jobid );
225                $tpl->assign("state", $jobattrs[status] );
226                $tpl->assign("user", $jobattrs[owner] );
227                $tpl->assign("queue", $jobattrs[queue] );
228                $tpl->assign("name", $jobattrs[name] );
229                $tpl->assign("req_cpu", $jobattrs[requested_time] );
230                $tpl->assign("req_memory", $jobattrs[requested_memory] );
231                $nodes = count( $jobattrs[nodes] );
232                $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
233                $cpus = $nodes * $ppn;
234                $tpl->assign("nodes", $nodes );
235                $tpl->assign("cpus", $cpus );
236                $start_time = (int) $jobattrs[start_timestamp];
237
238                if( $start_time ) {
239
240                        $runningtime = makeTime( $report_time - $start_time );
241                        $tpl->assign("started", makeDate( $start_time ) );
242                        $tpl->assign("runningtime", $runningtime );
243                }
244        }
245}
246
247$tpl->printToScreen();
248
249?>
Note: See TracBrowser for help on using the repository browser.