source: trunk/web2/addons/job_monarch/search.php @ 532

Last change on this file since 532 was 532, checked in by ramonb, 16 years ago

web2:

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