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

Last change on this file since 291 was 291, checked in by bastiaans, 17 years ago

web/addons/job_monarch/search.php:

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