source: trunk/web/addons/job_monarch/search.php @ 203

Last change on this file since 203 was 203, checked in by bastiaans, 18 years ago

Renamed: web/addons/toga -> web/addons/job_monarch

web/templates/job_monarch/cluster_extra.tpl:

  • changed addon path
File size: 16.3 KB
Line 
1<?php
2
3global $clustername, $tpl;
4
5function validateFormInput() {
6        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
7        global $end_from_time, $end_to_time, $period_start, $period_stop;
8
9        $error = 0;
10        $error_msg = "<FONT COLOR=\"red\"><B>";
11        $show_msg = 0;
12
13        $none_set = 0;
14
15        if( $id == '' and $user == '' and $name == '' and $start_from_time == '' and $start_to_time == '' and $queue == '' and $end_from_time == '' and $end_to_time == '') {
16                $error = 1;
17                $show_msg = 1;
18                $error_msg .= "No search criteria set!";
19        }
20
21        if( !is_numeric($id) and !$error and $id != '') {
22
23                $error = 1;
24                $show_msg = 1;
25                $error_msg .= "Id must be a number";
26        }
27
28        //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
29
30        if( !$error and $period_start != '' ) {
31                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
32                $pstart_epoch = datetimeToEpoch( $period_start );
33                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
34                if( $period_stop != '' ) {
35
36                        $pstop_epoch = datetimeToEpoch( $period_stop );
37                        //printf( "pstop_epoch = %s pstart_epoch = %s\n", $pstop_epoch, $pstart_epoch );
38
39                        if( $pstart_epoch > $pstop_epoch ) {
40
41                                $show_msg = 1;
42                                $error_msg .= "Graph timeperiod reset: start date/time can't be later than end";
43                                $period_stop = '';
44                                $period_start = '';
45                        } else if( $pstop_epoch == $pstart_epoch ) {
46
47                                $show_msg = 1;
48                                $error_msg .= "Graph timeperiod reset: start and end date/time can't be the same";
49                                $period_stop = '';
50                                $period_start = '';
51                        }
52                }
53        }
54
55        $error_msg .= "</B></FONT>";
56        // doe checks en set error en error_msg in case shit
57
58        if( $show_msg )
59                $tpl->assign( "form_error_msg", $error_msg );
60
61        return ($error ? 0 : 1 );
62}
63
64function makeTime( $time ) {
65
66        $days = intval( $time / 86400 );
67        $time = ($days>0) ? $time % ($days * 86400) : $time;
68
69        //printf( "time = %s, days = %s\n", $time, $days );
70
71        $date_str = '';
72        $day_str = '';
73
74        if( $days > 0 ) {
75                if( $days > 1 )
76                        $day_str .= $days . ' days';
77                else
78                        $day_str .= $days . ' day';
79        }
80
81        $hours = intval( $time / 3600 );
82        $time = $hours ? $time % ($hours * 3600) : $time;
83
84        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
85
86        if( $hours > 0 ) {
87                $date_str .= $hours . ':';
88                $date_unit = 'hours';
89        }
90
91        $minutes = intval( $time / 60 );
92        $seconds = $minutes ? $time % ($minutes * 60) : $time;
93
94        if( $minutes > 0 ) {
95
96                if( $minutes >= 10 )
97                        $date_str .= $minutes . ':';
98                else
99                        $date_str .= '0' . $minutes . ':';
100
101                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
102        } else {
103                if($hours > 0 ) {
104                        $date_str .= '00:';
105                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
106                }
107        }
108
109
110        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
111
112        if( $seconds > 0 ) {
113
114                if( $seconds >= 10 )
115                        $date_str .= $seconds . ' ' . $date_unit;
116                else
117                        $date_str .= '0' . $seconds . ' ' . $date_unit;
118
119        } else if ( $hours > 0 or $minutes > 0 )
120
121                $date_str .= '00 ' . $date_unit;
122
123        if( $days > 0) {
124
125                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
126                        $date_str = $day_str . ' - ' . $date_str;
127                else
128                        $date_str = $day_str;
129        }
130
131        return $date_str;
132}
133
134function makeDate( $time ) {
135        return strftime( "%a %d %b %Y %H:%M:%S", $time );
136}
137
138function datetimeToEpoch( $datetime ) {
139
140        //printf("datetime = %s\n", $datetime );
141        $datetime_fields = explode( ' ', $datetime );
142
143        $date = $datetime_fields[0];
144        $time = $datetime_fields[1];
145
146        $date_fields = explode( '-', $date );
147
148        $days = $date_fields[0];
149        $months = $date_fields[1];
150        $years = $date_fields[2];
151
152        //printf( "days = %s months = %s years = %s\n", $days, $months, $years );
153
154        $time_fields = explode( ':', $time );
155
156        $hours = $time_fields[0];
157        $minutes = $time_fields[1];
158        $seconds = $time_fields[2];
159
160        //printf( "hours = %s minutes = %s seconds = %s\n", $hours, $minutes, $seconds );
161
162        $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
163
164        //printf( "timestamp = %s\n", $timestamp );
165
166        return $timestamp;
167}
168
169function timeToEpoch( $time ) {
170
171        $time_fields = explode( ':', $time );
172
173        if( count($time_fields) == 3 ) {
174
175                $hours = $time_fields[0];
176                $minutes = $time_fields[1];
177                $seconds = $time_fields[2];
178
179        } else if( count($time_fields) == 2 ) {
180
181                $hours = 0;
182                $minutes = $time_fields[0];
183                $seconds = $time_fields[1];
184
185        } else if( count($time_fields) == 1 ) {
186
187                $hours = 0;
188                $minutes = 0;
189                $seconds = $time_fields[0];
190        }
191
192        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
193
194        return $myepoch;
195}
196
197function sortJobs( $jobs, $nodes, $sortby, $sortorder ) {
198
199        //printf("sortby = %s sortorder = %s\n", $sortby, $sortorder );
200
201        $sorted = array();
202
203        $cmp = create_function( '$a, $b',
204                "global \$sortby, \$sortorder;".
205
206                "if( \$a == \$b ) return 0;".
207
208                "if (\$sortorder==\"desc\")".
209                        "return ( \$a < \$b ) ? 1 : -1;".
210                "else if (\$sortorder==\"asc\")".
211                        "return ( \$a > \$b ) ? 1 : -1;" );
212
213        //print_r( $jobs );
214
215        foreach( $jobs as $jobid => $jobattrs ) {
216
217                        $state = $jobattrs[status];
218                        $user = $jobattrs[owner];
219                        $queue = $jobattrs[queue];
220                        $name = $jobattrs[name];
221                        $req_cpu = $jobattrs[requested_time];
222                        $req_memory = $jobattrs[requested_memory];
223
224                        //if( $state == 'R' )
225                        $mynodes = count( $nodes[$jobid] );
226                        //else
227                        //        $nodes = $jobattrs[nodes];
228
229                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
230                        $cpus = $mynodes * $ppn;
231                        $start_time = (int) $jobattrs[start_timestamp];
232                        $stop_time = (int) $jobattrs[stop_timestamp];
233                        $runningtime = $stop_time - $start_time;
234
235                        switch( $sortby ) {
236                                case "id":
237                                        $sorted[$jobid] = $jobid;
238                                        break;
239
240                                case "state":
241                                        $sorted[$jobid] = $state;
242                                        break;
243
244                                case "user":
245                                        $sorted[$jobid] = $user;
246                                        break;
247
248                                case "queue":
249                                        $sorted[$jobid] = $queue;
250                                        break;
251
252                                case "name":
253                                        $sorted[$jobid] = $name;
254                                        break;
255
256                                case "req_cpu":
257                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
258                                        break;
259
260                                case "req_mem":
261                                        $sorted[$jobid] = $req_memory;
262                                        break;
263
264                                case "nodes":
265                                        $sorted[$jobid] = $mynodes;
266                                        break;
267
268                                case "cpus":
269                                        $sorted[$jobid] = $cpus;
270                                        break;
271
272                                case "start":
273                                        $sorted[$jobid] = $start_time;
274                                        break;
275
276                                case "finished":
277                                        $sorted[$jobid] = $stop_time;
278                                        break;
279
280                                case "runningtime":
281                                        $sorted[$jobid] = $runningtime;
282                                        break;
283
284                                default:
285                                        break;
286
287                        }
288        }
289
290        //uasort( $sorted, $cmp );
291        if( $sortorder == "asc" )
292                arsort( $sorted );
293        else if( $sortorder == "desc" )
294                asort( $sorted );
295
296        //print_r( $sorted );
297
298        return array_keys( $sorted );
299}
300
301function makeSearchPage() {
302        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
303        global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up;
304        global $period_start, $period_stop, $sortby, $sortorder;
305
306        $metricname = $m;
307        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
308        //printf("start = %s stop = %s\n", $start, $stop );
309
310        $tpl->assign( "cluster", $clustername );
311        $tpl->assign( "id_value", $id );
312        $tpl->assign( "user_value", $user );
313        $tpl->assign( "queue_value", $queue );
314        $tpl->assign( "name_value", $name );
315        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
316        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
317        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
318        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
319
320        if( validateFormInput() ) {
321
322                $tpl->newBlock( "search_results" );
323                $tpl->assign( "sortby", $sortby);
324                $tpl->assign( "sortorder", $sortorder);
325                $tdb = new TarchDbase();
326                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
327                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
328                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
329                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
330                $search_ids = $tdb->searchDbase( $id, $queue, $user, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
331
332                $jobs = array();
333                $nodes = array();
334
335                //print_r( $search_ids );
336
337                foreach( $search_ids as $myid ) {
338
339                        //printf( "myid %s\n", $myid );
340                        $jobs[$myid] = $tdb->getJobArray( $myid );
341                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
342                }
343
344                //print_r( $nodes );
345                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
346
347                //print_r( $sorted_search );
348                foreach( $sorted_search as $sortid ) {
349
350                        $job = $jobs[$sortid];
351                        //print_r( $job );
352                        $foundid = $job[id];
353                        //printf( "foundid %s\n", $foundid );
354
355                        //$job = $tdb->getJobArray( $foundid );
356                        //$nodes = $tdb->getNodesForJob( $foundid );
357
358                        $tpl->newBlock( "node" );
359                        $tpl->assign( "id", $job[id] );
360                        $tpl->assign( "state", $job[status] );
361                        $tpl->assign( "user", $job[owner] );
362                        $tpl->assign( "queue", $job[queue] );
363                        $tpl->assign( "name", $job[name] );
364                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job[requested_time] ) ) );
365                        $tpl->assign( "req_memory", $job[requested_memory] );
366
367                        $nodes_nr = count( $nodes[$foundid] );
368
369                        // need to replace later with domain stored from dbase
370                        //
371                        //$job_domain = $job[domain];
372
373                        //$myhost = $_SERVER[HTTP_HOST];
374                        //$myhf = explode( '.', $myhost );
375                        //$myhf = array_reverse( $myhf );
376                        //array_pop( $myhf );
377                        //$myhf = array_reverse( $myhf );
378                        //$job_domain = implode( '.', $myhf );
379                       
380                        //print_r( $job );
381                        //printf( "job domain = %s\n", $job_domain);
382                        $ppn = (int) $job[ppn] ? $job[ppn] : 1;
383                        $cpus = $nodes_nr * $ppn;
384
385                        $tpl->assign( "nodes", $nodes_nr );
386                        $tpl->assign( "cpus", $cpus );
387
388                        $job_start = $job[start_timestamp];
389                        $job_stop = $job[stop_timestamp];
390                        $runningtime = intval( $job_stop - $job_start );
391                        $tpl->assign( "started", makeDate( $job_start ) );
392                        $tpl->assign( "finished", makeDate( $job_stop ) );
393                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
394                       
395                        //print_r( $job );
396                        //print_r( $nodes );
397                }
398
399                if( count( $search_ids ) == 1 ) {
400
401                        $tpl->newBlock( "showhosts" );
402
403                        $showhosts = isset($sh) ? $sh : $default_showhosts;
404                        //if( !$showhosts) $showhosts = $default_showhosts;
405                        $tpl->assign("checked$showhosts", "checked");
406
407                        # Present a width list
408                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
409
410                        $hostcols = ($hc) ? $hc : 4;
411
412                        foreach(range(1,25) as $cols) {
413                                $cols_menu .= "<OPTION VALUE=$cols ";
414                                if ($cols == $hostcols)
415                                        $cols_menu .= "SELECTED";
416                                $cols_menu .= ">$cols\n";
417                        }
418                        $cols_menu .= "</SELECT>\n";
419
420                        $tpl->assign("metric","$metricname $units");
421                        $tpl->assign("id", $id);
422                        # Host columns menu defined in header.php
423                        $tpl->assign("cols_menu", $cols_menu);
424
425                        if( $showhosts ) {
426                                //bla
427
428                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
429                                //printf("start = %s stop = %s\n", $start, $stop );
430
431                                if( !$period_start ) // Add an extra 10% to graphstart
432                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
433                                else
434                                        $period_start = datetimeToEpoch( $period_start );
435
436                                if( !$period_stop ) // Add an extra 10% to graphend
437                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
438                                else
439                                        $period_stop = datetimeToEpoch( $period_stop );
440
441                                //printf("start = %s stop = %s\n", $start, $stop );
442
443                                $tpl->gotoBlock( "timeperiod" );
444
445                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
446                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
447
448                                $tpl->gotoBlock( "_ROOT" );
449
450                                $hosts_up = array();
451
452                                foreach( $nodes[$id] as $mynode )
453                                        $hosts_up[] = $mynode[hostname];
454
455                                $sorted_hosts = array();
456                                //$hosts_up = $jobs[$filter[id]][nodes];
457
458                                foreach ($hosts_up as $host ) {
459                                        //$host = $host. '.'.$job_domain;
460                                        $cpus = $metrics[$host]["cpu_num"][VAL];
461                                        if (!$cpus) $cpus=1;
462                                        $load_one  = $metrics[$host]["load_one"][VAL];
463                                        $load = ((float) $load_one)/$cpus;
464                                        $host_load[$host] = $load;
465                                        $percent_hosts[load_color($load)] += 1;
466                                        if ($metricname=="load_one")
467                                                $sorted_hosts[$host] = $load;
468                                        else
469                                                $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
470                                }
471                                switch ($sort) {
472                                        case "descending":
473                                                arsort($sorted_hosts);
474                                                break;
475                                        case "by hostname":
476                                                ksort($sorted_hosts);
477                                                break;
478                                        default:
479                                        case "ascending":
480                                                asort($sorted_hosts);
481                                                break;
482                                }
483
484                                //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
485
486                                # First pass to find the max value in all graphs for this
487                                # metric. The $start,$end variables comes from get_context.php,
488                                # included in index.php.
489                                list($min, $max) = find_limits($sorted_hosts, $metricname);
490
491                                # Second pass to output the graphs or metrics.
492                                $i = 1;
493                                foreach ( $sorted_hosts as $host=>$value  ) {
494                                        $tpl->newBlock ("sorted_list");
495                                        //$host = $host. '.'.$domain;
496                                        $host_url = rawurlencode($host);
497                                        $cluster_url = rawurlencode($clustername);
498
499                                        $textval = "";
500                                        //printf("host = %s, value = %s", $host, $value);
501                                        //echo "$host: $value, ";
502                                        $val = $metrics[$host][$metricname];
503                                        $class = "metric";
504                                        $host_link="\"?c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
505
506                                        if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
507                                                $textval = date("r", $val[VAL]);
508                                        } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
509                                                $textval = "$val[VAL] $val[UNITS]";
510                                        } else {
511                                                $graphargs = "z=small&c=$cluster_url&m=$metricname&h=$host_url&v=$val[VAL]&x=$max&n=$min&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop&min=$min&max=$max";
512                                        }
513                                        if ($textval) {
514                                                $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
515                                        } else {
516                                                $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
517                                        }
518
519                                        $tpl->assign("metric_image", $cell);
520                                        if (! ($i++ % $hostcols) )
521                                                 $tpl->assign ("br", "</tr><tr>");
522                                }
523
524                                //einde bla
525                        }
526                }
527
528        }
529}
530?>
Note: See TracBrowser for help on using the repository browser.