source: branches/0.4/web/addons/job_monarch/search.php @ 712

Last change on this file since 712 was 712, checked in by ramonb, 11 years ago
  • some fix to unquoted associative array key
  • Property svn:keywords set to Id
File size: 16.0 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 712 2013-03-21 20:12:19Z ramonb $
23 */
24
25global $clustername, $tpl, $m, $metric;
26
27function validateFormInput() {
28        global $clustername, $tpl, $id, $owner, $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 $owner== '' 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                        $owner = $jobattrs['owner'];
171                        $queue = $jobattrs['queue'];
172                        $name = $jobattrs['name'];
173                        $req_cpu = $jobattrs['requested_time'];
174                        $req_memory = $jobattrs['requested_memory'];
175
176                        $mynodes = count( $nodes[$jobid] );
177
178                        $ppn = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
179                        $cpus = $mynodes * $ppn;
180                        $start_time = (int) $jobattrs['start_timestamp'];
181                        $stop_time = (int) $jobattrs['stop_timestamp'];
182                        $runningtime = $stop_time - $start_time;
183
184                        switch( $sortby ) {
185                                case "id":
186                                        $sorted[$jobid] = $jobid;
187                                        break;
188
189                                case "state":
190                                        $sorted[$jobid] = $state;
191                                        break;
192
193                                case "owner":
194                                        $sorted[$jobid] = $owner;
195                                        break;
196
197                                case "queue":
198                                        $sorted[$jobid] = $queue;
199                                        break;
200
201                                case "name":
202                                        $sorted[$jobid] = $name;
203                                        break;
204
205                                case "req_cpu":
206                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
207                                        break;
208
209                                case "req_mem":
210                                        $sorted[$jobid] = $req_memory;
211                                        break;
212
213                                case "nodes":
214                                        $sorted[$jobid] = $mynodes;
215                                        break;
216
217                                case "cpus":
218                                        $sorted[$jobid] = $cpus;
219                                        break;
220
221                                case "start":
222                                        $sorted[$jobid] = $start_time;
223                                        break;
224
225                                case "finished":
226                                        $sorted[$jobid] = $stop_time;
227                                        break;
228
229                                case "runningtime":
230                                        $sorted[$jobid] = $runningtime;
231                                        break;
232
233                                default:
234                                        break;
235
236                        }
237        }
238
239        //uasort( $sorted, $cmp );
240        if( $sortorder == "asc" )
241                arsort( $sorted );
242        else if( $sortorder == "desc" )
243                asort( $sorted );
244
245        //print_r( $sorted );
246
247        return array_keys( $sorted );
248}
249
250function makeSearchPage() {
251        global $clustername, $tpl, $id, $owner, $name, $start_from_time, $start_to_time, $queue;
252        global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up, $hc;
253        global $period_start, $period_stop, $sortby, $sortorder, $COLUMN_REQUESTED_MEMORY;
254        global $SEARCH_RESULT_LIMIT, $COLUMN_NODES, $metricname;
255
256        //$metricname = $m;
257        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
258        //printf("start = %s stop = %s\n", $start, $stop );
259        //printf("m %s\n", $metricname );
260
261        $longtitle = "Batch Archive Search :: Powered by Job Monarch!";
262        $title = "Batch Archive Search";
263
264        makeHeader( 'search', $title, $longtitle );
265
266        $tpl->assign( "cluster", $clustername );
267        $tpl->assign( "id_value", $id );
268        $tpl->assign( "owner_value", $owner);
269        $tpl->assign( "queue_value", $queue );
270        $tpl->assign( "name_value", $name );
271        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
272        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
273        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
274        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
275
276        if( validateFormInput() ) {
277
278                $tpl->newBlock( "search_results" );
279                $tpl->assign( "sortby", $sortby);
280                $tpl->assign( "sortorder", $sortorder);
281                $tdb = new TarchDbase( "127.0.0.1" );
282                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
283                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
284                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
285                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
286                $search_ids = $tdb->searchDbase( $id, $queue, $owner, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
287
288                if( ($tdb->resultcount) > (int) $SEARCH_RESULT_LIMIT ) {
289                        $tpl->gotoBlock( "_ROOT" );
290               
291                        $tpl->assign( "form_error_msg", "Got " . $tdb->resultcount . " search results, output limited to last " . $SEARCH_RESULT_LIMIT . " jobs." );
292                        $tpl->gotoBlock( "search_results" );
293                }
294
295                $jobs = array();
296                $nodes = array();
297
298                $even = 1;
299
300                //print_r( $search_ids );
301
302                foreach( $search_ids as $myid ) {
303
304                        //printf( "myid %s\n", $myid );
305                        $jobs[$myid] = $tdb->getJobArray( $myid );
306                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
307                }
308
309                if( $COLUMN_REQUESTED_MEMORY ) {
310                        $tpl->newBlock( "column_header_req_mem" );
311                }
312                if( $COLUMN_NODES ) {
313                        $tpl->newBlock( "column_header_nodes" );
314                }
315
316                //print_r( $nodes );
317                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
318
319                //print_r( $sorted_search );
320                foreach( $sorted_search as $sortid ) {
321
322                        $job = $jobs[$sortid];
323                        //print_r( $job );
324                        $foundid = $job['id'];
325                        //printf( "foundid %s\n", $foundid );
326
327                        //$job = $tdb->getJobArray( $foundid );
328                        //$nodes = $tdb->getNodesForJob( $foundid );
329
330                        $tpl->newBlock( "node" );
331                        $tpl->assign( "id", $job['id'] );
332                        $tpl->assign( "state", $job['status'] );
333                        $tpl->assign( "owner", $job['owner'] );
334                        $tpl->assign( "queue", $job['queue'] );
335                        $tpl->assign( "name", $job['name'] );
336                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job['requested_time'] ) ) );
337
338                        if( $COLUMN_REQUESTED_MEMORY ) {
339                                $tpl->newBlock( "column_req_mem" );
340                                $tpl->assign( "req_memory", $job['requested_memory'] );
341                                $tpl->gotoBlock( "node" );
342                        }
343                        if( $COLUMN_NODES) {
344
345                                $job_nodes      = array();
346
347                                foreach( $nodes[$foundid] as $mynode )
348                                        $job_nodes[] = $mynode['hostname'];
349
350                                $tpl->newBlock( "column_nodes" );
351                                $nodes_hostnames = implode( " ", $job_nodes );
352                                $tpl->assign( "nodes_hostnames", $nodes_hostnames );
353                                $tpl->gotoBlock( "node" );
354                        }
355
356                        $nodes_nr = count( $nodes[$foundid] );
357
358                        if( $even ) {
359
360                                $tpl->assign("nodeclass", "even");
361                                $even = 0;
362                        } else {
363
364                                $tpl->assign("nodeclass", "odd");
365                                $even = 1;
366                        }
367
368                        $ppn = (int) $job['ppn'] ? $job['ppn'] : 1;
369                        $cpus = $nodes_nr * $ppn;
370
371                        $tpl->assign( "nodes", $nodes_nr );
372                        $tpl->assign( "cpus", $cpus );
373
374                        $job_start = $job['start_timestamp'];
375                        $job_stop = $job['stop_timestamp'];
376                        $runningtime = intval( $job_stop - $job_start );
377                        $tpl->assign( "started", makeDate( $job_start ) );
378                        $tpl->assign( "finished", makeDate( $job_stop ) );
379                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
380                       
381                        //print_r( $job );
382                        //print_r( $nodes );
383                }
384
385                if( count( $search_ids ) == 1 ) {
386
387                        $tpl->newBlock( "showhosts" );
388
389                        $showhosts = isset($sh) ? $sh : $default_showhosts;
390                        //if( !$showhosts) $showhosts = $default_showhosts;
391                        $tpl->assign("checked$showhosts", "checked");
392
393                        # Present a width list
394                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
395
396                        $hostcols = ($hc) ? $hc : 4;
397
398                        foreach(range(1,25) as $cols) {
399                                $cols_menu .= "<OPTION VALUE=$cols ";
400                                if ($cols == $hostcols)
401                                        $cols_menu .= "SELECTED";
402                                $cols_menu .= ">$cols\n";
403                        }
404                        $cols_menu .= "</SELECT>\n";
405
406                        $tpl->assign("metric","$metricname $units");
407                        $tpl->assign("id", $id);
408                        # Host columns menu defined in header.php
409                        $tpl->assign("cols_menu", $cols_menu);
410
411                        if( $showhosts ) {
412                                //bla
413
414                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
415                                //printf("start = %s stop = %s\n", $start, $stop );
416
417                                if( !$period_start ) // Add an extra 10% to graphstart
418                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
419                                else
420                                        $period_start = datetimeToEpoch( $period_start );
421
422                                if( !$period_stop ) // Add an extra 10% to graphend
423                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
424                                else
425                                        $period_stop = datetimeToEpoch( $period_stop );
426
427                                //printf("start = %s stop = %s\n", $start, $stop );
428
429                                $tpl->gotoBlock( "timeperiod" );
430
431                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
432                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
433
434                                $tpl->gotoBlock( "_ROOT" );
435
436                                $hosts_up = array();
437
438                                foreach( $nodes[$id] as $mynode )
439                                        $hosts_up[] = $mynode['hostname'];
440
441                                $sorted_hosts = array();
442
443                                foreach ($hosts_up as $host ) {
444                                        //$host = $host. '.'.$job_domain;
445                                        $cpus = $metrics[$host]["cpu_num"]['VAL'];
446                                        if (!$cpus) $cpus=1;
447                                        $load_one  = $metrics[$host]["load_one"]['VAL'];
448                                        $load = ((float) $load_one)/$cpus;
449                                        $host_load[$host] = $load;
450                                        $percent_hosts['load_color'.($load)] += 1;
451                                        if ($metricname=="load_one")
452                                                $sorted_hosts[$host] = $load;
453                                        else
454                                                $sorted_hosts[$host] = $metrics[$host][$metricname]['VAL'];
455                                }
456                                switch ($sort) {
457                                        case "descending":
458                                                arsort($sorted_hosts);
459                                                break;
460                                        case "by hostname":
461                                                ksort($sorted_hosts);
462                                                break;
463                                        default:
464                                        case "ascending":
465                                                asort($sorted_hosts);
466                                                break;
467                                }
468
469                                //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
470
471                                # First pass to find the max value in all graphs for this
472                                # metric. The $start,$end variables comes from get_context.php,
473                                # included in index.php.
474                                list($min, $max) = find_limits($sorted_hosts, $metricname);
475
476                                # Second pass to output the graphs or metrics.
477                                $i = 1;
478                                foreach ( $sorted_hosts as $host=>$value  ) {
479                                        $tpl->newBlock ("sorted_list");
480                                        //$host = $host. '.'.$domain;
481                                        $host_url = rawurlencode($host);
482                                        $cluster_url = rawurlencode($clustername);
483
484                                        $textval = "";
485                                        //printf("host = %s, value = %s", $host, $value);
486                                        //echo "$host: $value, ";
487                                        $val = $metrics[$host][$metricname];
488                                        $class = "metric";
489                                        $host_link="\"?j_view=host&c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
490
491                                        if ($val['TYPE']=="timestamp" or $always_timestamp[$metricname]) {
492                                                $textval = date("r", $val['VAL']);
493                                        } elseif ($val['TYPE']=="string" or $val['SLOPE']=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
494                                                $textval = "$val['VAL'] $val['UNITS']";
495                                        } else {
496                                                $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";
497                                        }
498                                        if ($textval) {
499                                                $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
500                                        } else {
501                                                $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
502                                        }
503
504                                        $tpl->assign("metric_image", $cell);
505                                        if (! ($i++ % $hostcols) )
506                                                 $tpl->assign ("br", "</tr><tr>");
507                                }
508
509                                //einde bla
510                        }
511                }
512
513        }
514}
515?>
Note: See TracBrowser for help on using the repository browser.