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

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

web/addons/job_monarch/search.php:

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