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

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

job_monarch/search.php,
job_monarch/index.php:

  • fixes to search page header
  • Property svn:keywords set to Id
File size: 16.5 KB
Line 
1<?php
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 *
22 * SVN $Id: search.php 410 2007-07-06 12:20:56Z bastiaans $
23 */
24
25global $clustername, $tpl;
26
27function validateFormInput() {
28        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
29        global $end_from_time, $end_to_time, $period_start, $period_stop;
30
31        $error = 0;
32        $error_msg = "<FONT COLOR=\"red\"><B>";
33        $show_msg = 0;
34
35        $none_set = 0;
36
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;
39                $show_msg = 1;
40                $error_msg .= "No search criteria set!";
41        }
42
43        if( !is_numeric($id) and !$error and $id != '') {
44
45                $error = 1;
46                $show_msg = 1;
47                $error_msg .= "Id must be a number";
48        }
49
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 );
59                        //printf( "pstop_epoch = %s pstart_epoch = %s\n", $pstop_epoch, $pstart_epoch );
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>";
78        // doe checks en set error en error_msg in case shit
79
80        if( $show_msg )
81                $tpl->assign( "form_error_msg", $error_msg );
82
83        return ($error ? 0 : 1 );
84}
85
86//function makeDate( $time ) {
87//        return strftime( "%a %d %b %Y %H:%M:%S", $time );
88//}
89
90function datetimeToEpoch( $datetime ) {
91
92        //printf("datetime = %s\n", $datetime );
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
104        //printf( "days = %s months = %s years = %s\n", $days, $months, $years );
105
106        $time_fields = explode( ':', $time );
107
108        $hours = $time_fields[0];
109        $minutes = $time_fields[1];
110        $seconds = $time_fields[2];
111
112        //printf( "hours = %s minutes = %s seconds = %s\n", $hours, $minutes, $seconds );
113
114        $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
115
116        //printf( "timestamp = %s\n", $timestamp );
117
118        return $timestamp;
119}
120
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
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
228                                case "finished":
229                                        $sorted[$jobid] = $stop_time;
230                                        break;
231
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
253function makeSearchPage() {
254        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
255        global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up, $hc;
256        global $period_start, $period_stop, $sortby, $sortorder, $COLUMN_REQUESTED_MEMORY;
257        global $SEARCH_RESULT_LIMIT, $COLUMN_NODES;
258
259        $metricname = $m;
260        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
261        //printf("start = %s stop = %s\n", $start, $stop );
262
263        $longtitle = "Batch Archive Search :: Powered by Job Monarch!";
264        $title = "Batch Archive Search";
265
266        makeHeader( 'search', $title, $longtitle );
267
268        $tpl->assign( "cluster", $clustername );
269        $tpl->assign( "id_value", $id );
270        $tpl->assign( "user_value", $user );
271        $tpl->assign( "queue_value", $queue );
272        $tpl->assign( "name_value", $name );
273        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
274        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
275        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
276        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
277
278        if( validateFormInput() ) {
279
280                $tpl->newBlock( "search_results" );
281                $tpl->assign( "sortby", $sortby);
282                $tpl->assign( "sortorder", $sortorder);
283                $tdb = new TarchDbase( "127.0.0.1" );
284                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
285                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
286                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
287                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
288                $search_ids = $tdb->searchDbase( $id, $queue, $user, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
289
290                if( ($tdb->resultcount) > (int) $SEARCH_RESULT_LIMIT ) {
291                        $tpl->gotoBlock( "_ROOT" );
292               
293                        $tpl->assign( "form_error_msg", "Got " . $tdb->resultcount . " search results, output limited to last " . $SEARCH_RESULT_LIMIT . " jobs." );
294                        $tpl->gotoBlock( "search_results" );
295                }
296
297                $jobs = array();
298                $nodes = array();
299
300                $even = 1;
301
302                //print_r( $search_ids );
303
304                foreach( $search_ids as $myid ) {
305
306                        //printf( "myid %s\n", $myid );
307                        $jobs[$myid] = $tdb->getJobArray( $myid );
308                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
309                }
310
311                if( $COLUMN_REQUESTED_MEMORY ) {
312                        $tpl->newBlock( "column_header_req_mem" );
313                }
314                if( $COLUMN_NODES ) {
315                        $tpl->newBlock( "column_header_nodes" );
316                }
317
318                //print_r( $nodes );
319                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
320
321                //print_r( $sorted_search );
322                foreach( $sorted_search as $sortid ) {
323
324                        $job = $jobs[$sortid];
325                        //print_r( $job );
326                        $foundid = $job[id];
327                        //printf( "foundid %s\n", $foundid );
328
329                        //$job = $tdb->getJobArray( $foundid );
330                        //$nodes = $tdb->getNodesForJob( $foundid );
331
332                        $tpl->newBlock( "node" );
333                        $tpl->assign( "id", $job[id] );
334                        $tpl->assign( "state", $job[status] );
335                        $tpl->assign( "user", $job[owner] );
336                        $tpl->assign( "queue", $job[queue] );
337                        $tpl->assign( "name", $job[name] );
338                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job[requested_time] ) ) );
339
340                        if( $COLUMN_REQUESTED_MEMORY ) {
341                                $tpl->newBlock( "column_req_mem" );
342                                //$tpl->assign( "req_memory", $jobs[$jobid][requested_memory] );
343                                $tpl->assign( "req_memory", $job[requested_memory] );
344                                $tpl->gotoBlock( "node" );
345                        }
346                        if( $COLUMN_NODES) {
347
348                                $job_nodes      = array();
349
350                                foreach( $nodes[$foundid] as $mynode )
351                                        $job_nodes[] = $mynode[hostname];
352
353                                $tpl->newBlock( "column_nodes" );
354                                $nodes_hostnames = implode( " ", $job_nodes );
355                                $tpl->assign( "nodes_hostnames", $nodes_hostnames );
356                                $tpl->gotoBlock( "node" );
357                        }
358
359                        $nodes_nr = count( $nodes[$foundid] );
360
361                        if( $even ) {
362
363                                $tpl->assign("nodeclass", "even");
364                                $even = 0;
365                        } else {
366
367                                $tpl->assign("nodeclass", "odd");
368                                $even = 1;
369                        }
370
371
372                        // need to replace later with domain stored from dbase
373                        //
374                        //$job_domain = $job[domain];
375
376                        //$myhost = $_SERVER[HTTP_HOST];
377                        //$myhf = explode( '.', $myhost );
378                        //$myhf = array_reverse( $myhf );
379                        //array_pop( $myhf );
380                        //$myhf = array_reverse( $myhf );
381                        //$job_domain = implode( '.', $myhf );
382                       
383                        //print_r( $job );
384                        //printf( "job domain = %s\n", $job_domain);
385                        $ppn = (int) $job[ppn] ? $job[ppn] : 1;
386                        $cpus = $nodes_nr * $ppn;
387
388                        $tpl->assign( "nodes", $nodes_nr );
389                        $tpl->assign( "cpus", $cpus );
390
391                        $job_start = $job[start_timestamp];
392                        $job_stop = $job[stop_timestamp];
393                        $runningtime = intval( $job_stop - $job_start );
394                        $tpl->assign( "started", makeDate( $job_start ) );
395                        $tpl->assign( "finished", makeDate( $job_stop ) );
396                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
397                       
398                        //print_r( $job );
399                        //print_r( $nodes );
400                }
401
402                if( count( $search_ids ) == 1 ) {
403
404                        $tpl->newBlock( "showhosts" );
405
406                        $showhosts = isset($sh) ? $sh : $default_showhosts;
407                        //if( !$showhosts) $showhosts = $default_showhosts;
408                        $tpl->assign("checked$showhosts", "checked");
409
410                        # Present a width list
411                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
412
413                        $hostcols = ($hc) ? $hc : 4;
414
415                        foreach(range(1,25) as $cols) {
416                                $cols_menu .= "<OPTION VALUE=$cols ";
417                                if ($cols == $hostcols)
418                                        $cols_menu .= "SELECTED";
419                                $cols_menu .= ">$cols\n";
420                        }
421                        $cols_menu .= "</SELECT>\n";
422
423                        $tpl->assign("metric","$metricname $units");
424                        $tpl->assign("id", $id);
425                        # Host columns menu defined in header.php
426                        $tpl->assign("cols_menu", $cols_menu);
427
428                        if( $showhosts ) {
429                                //bla
430
431                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
432                                //printf("start = %s stop = %s\n", $start, $stop );
433
434                                if( !$period_start ) // Add an extra 10% to graphstart
435                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
436                                else
437                                        $period_start = datetimeToEpoch( $period_start );
438
439                                if( !$period_stop ) // Add an extra 10% to graphend
440                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
441                                else
442                                        $period_stop = datetimeToEpoch( $period_stop );
443
444                                //printf("start = %s stop = %s\n", $start, $stop );
445
446                                $tpl->gotoBlock( "timeperiod" );
447
448                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
449                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
450
451                                $tpl->gotoBlock( "_ROOT" );
452
453                                $hosts_up = array();
454
455                                foreach( $nodes[$id] as $mynode )
456                                        $hosts_up[] = $mynode[hostname];
457
458                                $sorted_hosts = array();
459                                //$hosts_up = $jobs[$filter[id]][nodes];
460
461                                foreach ($hosts_up as $host ) {
462                                        //$host = $host. '.'.$job_domain;
463                                        $cpus = $metrics[$host]["cpu_num"][VAL];
464                                        if (!$cpus) $cpus=1;
465                                        $load_one  = $metrics[$host]["load_one"][VAL];
466                                        $load = ((float) $load_one)/$cpus;
467                                        $host_load[$host] = $load;
468                                        $percent_hosts[load_color($load)] += 1;
469                                        if ($metricname=="load_one")
470                                                $sorted_hosts[$host] = $load;
471                                        else
472                                                $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
473                                }
474                                switch ($sort) {
475                                        case "descending":
476                                                arsort($sorted_hosts);
477                                                break;
478                                        case "by hostname":
479                                                ksort($sorted_hosts);
480                                                break;
481                                        default:
482                                        case "ascending":
483                                                asort($sorted_hosts);
484                                                break;
485                                }
486
487                                //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
488
489                                # First pass to find the max value in all graphs for this
490                                # metric. The $start,$end variables comes from get_context.php,
491                                # included in index.php.
492                                list($min, $max) = find_limits($sorted_hosts, $metricname);
493
494                                # Second pass to output the graphs or metrics.
495                                $i = 1;
496                                foreach ( $sorted_hosts as $host=>$value  ) {
497                                        $tpl->newBlock ("sorted_list");
498                                        //$host = $host. '.'.$domain;
499                                        $host_url = rawurlencode($host);
500                                        $cluster_url = rawurlencode($clustername);
501
502                                        $textval = "";
503                                        //printf("host = %s, value = %s", $host, $value);
504                                        //echo "$host: $value, ";
505                                        $val = $metrics[$host][$metricname];
506                                        $class = "metric";
507                                        $host_link="\"?view=host&c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
508
509                                        if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
510                                                $textval = date("r", $val[VAL]);
511                                        } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
512                                                $textval = "$val[VAL] $val[UNITS]";
513                                        } else {
514                                                $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";
515                                        }
516                                        if ($textval) {
517                                                $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
518                                        } else {
519                                                $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
520                                        }
521
522                                        $tpl->assign("metric_image", $cell);
523                                        if (! ($i++ % $hostcols) )
524                                                 $tpl->assign ("br", "</tr><tr>");
525                                }
526
527                                //einde bla
528                        }
529                }
530
531        }
532}
533?>
Note: See TracBrowser for help on using the repository browser.