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

Last change on this file since 246 was 246, checked in by bastiaans, 18 years ago

web/addons/job_monarch/conf.php:

  • added DATETIME_FORMAT

web/addons/job_monarch/libtoga.php:

  • added makeDate()

web/addons/job_monarch/search.php,
web/addons/job_monarch/overview.php:

  • disable local makeDate, use from libtoga

web/addons/job_monarch/search.php

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