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

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

ALL:

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