source: trunk/web/addons/toga/search.php @ 181

Last change on this file since 181 was 181, checked in by bastiaans, 19 years ago

web/addons/toga/search.php:

  • Added sorting based on job finished time
File size: 16.3 KB
Line 
1<?php
2
3global $clustername, $tpl;
4
5function validateFormInput() {
6        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
7        global $end_from_time, $end_to_time, $period_start, $period_stop;
8
9        $error = 0;
10        $error_msg = "<FONT COLOR=\"red\"><B>";
11        $show_msg = 0;
12
13        $none_set = 0;
14
15        if( $id == '' and $user == '' and $name == '' and $start_from_time == '' and $start_to_time == '' and $queue == '' and $end_from_time == '' and $end_to_time == '') {
16                $error = 1;
17                $show_msg = 1;
18                $error_msg .= "No search criteria set!";
19        }
20
21        if( !is_numeric($id) and !$error and $id != '') {
22
23                $error = 1;
24                $show_msg = 1;
25                $error_msg .= "Id must be a number";
26        }
27
28        //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
29
30        if( !$error and $period_start != '' ) {
31                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
32                $pstart_epoch = datetimeToEpoch( $period_start );
33                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
34                if( $period_stop != '' ) {
35
36                        $pstop_epoch = datetimeToEpoch( $period_stop );
37                        //printf( "pstop_epoch = %s pstart_epoch = %s\n", $pstop_epoch, $pstart_epoch );
38
39                        if( $pstart_epoch > $pstop_epoch ) {
40
41                                $show_msg = 1;
42                                $error_msg .= "Graph timeperiod reset: start date/time can't be later than end";
43                                $period_stop = '';
44                                $period_start = '';
45                        } else if( $pstop_epoch == $pstart_epoch ) {
46
47                                $show_msg = 1;
48                                $error_msg .= "Graph timeperiod reset: start and end date/time can't be the same";
49                                $period_stop = '';
50                                $period_start = '';
51                        }
52                }
53        }
54
55        $error_msg .= "</B></FONT>";
56        // doe checks en set error en error_msg in case shit
57
58        if( $show_msg )
59                $tpl->assign( "form_error_msg", $error_msg );
60
61        return ($error ? 0 : 1 );
62}
63
64function makeTime( $time ) {
65
66        $days = intval( $time / 86400 );
67        $time = ($days>0) ? $time % ($days * 86400) : $time;
68
69        //printf( "time = %s, days = %s\n", $time, $days );
70
71        $date_str = '';
72        $day_str = '';
73
74        if( $days > 0 ) {
75                if( $days > 1 )
76                        $day_str .= $days . ' days';
77                else
78                        $day_str .= $days . ' day';
79        }
80
81        $hours = intval( $time / 3600 );
82        $time = $hours ? $time % ($hours * 3600) : $time;
83
84        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
85
86        if( $hours > 0 ) {
87                $date_str .= $hours . ':';
88                $date_unit = 'hours';
89        }
90
91        $minutes = intval( $time / 60 );
92        $seconds = $minutes ? $time % ($minutes * 60) : $time;
93
94        if( $minutes > 0 ) {
95
96                if( $minutes >= 10 )
97                        $date_str .= $minutes . ':';
98                else
99                        $date_str .= '0' . $minutes . ':';
100
101                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
102        } else {
103                if($hours > 0 ) {
104                        $date_str .= '00:';
105                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
106                }
107        }
108
109
110        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
111
112        if( $seconds > 0 ) {
113
114                if( $seconds >= 10 )
115                        $date_str .= $seconds . ' ' . $date_unit;
116                else
117                        $date_str .= '0' . $seconds . ' ' . $date_unit;
118
119        } else if ( $hours > 0 or $minutes > 0 )
120
121                $date_str .= '00 ' . $date_unit;
122
123        if( $days > 0) {
124
125                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
126                        $date_str = $day_str . ' - ' . $date_str;
127                else
128                        $date_str = $day_str;
129        }
130
131        return $date_str;
132}
133
134function makeDate( $time ) {
135        return strftime( "%a %d %b %Y %H:%M:%S", $time );
136}
137
138function datetimeToEpoch( $datetime ) {
139
140        //printf("datetime = %s\n", $datetime );
141        $datetime_fields = explode( ' ', $datetime );
142
143        $date = $datetime_fields[0];
144        $time = $datetime_fields[1];
145
146        $date_fields = explode( '-', $date );
147
148        $days = $date_fields[0];
149        $months = $date_fields[1];
150        $years = $date_fields[2];
151
152        //printf( "days = %s months = %s years = %s\n", $days, $months, $years );
153
154        $time_fields = explode( ':', $time );
155
156        $hours = $time_fields[0];
157        $minutes = $time_fields[1];
158        $seconds = $time_fields[2];
159
160        //printf( "hours = %s minutes = %s seconds = %s\n", $hours, $minutes, $seconds );
161
162        $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
163
164        //printf( "timestamp = %s\n", $timestamp );
165
166        return $timestamp;
167}
168
169function epochToDatetime( $epoch ) {
170
171        return strftime( "%d-%m-%Y %H:%M:%S", $epoch );
172}
173
174function timeToEpoch( $time ) {
175
176        $time_fields = explode( ':', $time );
177
178        if( count($time_fields) == 3 ) {
179
180                $hours = $time_fields[0];
181                $minutes = $time_fields[1];
182                $seconds = $time_fields[2];
183
184        } else if( count($time_fields) == 2 ) {
185
186                $hours = 0;
187                $minutes = $time_fields[0];
188                $seconds = $time_fields[1];
189
190        } else if( count($time_fields) == 1 ) {
191
192                $hours = 0;
193                $minutes = 0;
194                $seconds = $time_fields[0];
195        }
196
197        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
198
199        return $myepoch;
200}
201
202function sortJobs( $jobs, $nodes, $sortby, $sortorder ) {
203
204        //printf("sortby = %s sortorder = %s\n", $sortby, $sortorder );
205
206        $sorted = array();
207
208        $cmp = create_function( '$a, $b',
209                "global \$sortby, \$sortorder;".
210
211                "if( \$a == \$b ) return 0;".
212
213                "if (\$sortorder==\"desc\")".
214                        "return ( \$a < \$b ) ? 1 : -1;".
215                "else if (\$sortorder==\"asc\")".
216                        "return ( \$a > \$b ) ? 1 : -1;" );
217
218        //print_r( $jobs );
219
220        foreach( $jobs as $jobid => $jobattrs ) {
221
222                        $state = $jobattrs[status];
223                        $user = $jobattrs[owner];
224                        $queue = $jobattrs[queue];
225                        $name = $jobattrs[name];
226                        $req_cpu = $jobattrs[requested_time];
227                        $req_memory = $jobattrs[requested_memory];
228
229                        //if( $state == 'R' )
230                        $mynodes = count( $nodes[$jobid] );
231                        //else
232                        //        $nodes = $jobattrs[nodes];
233
234                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
235                        $cpus = $mynodes * $ppn;
236                        $start_time = (int) $jobattrs[start_timestamp];
237                        $stop_time = (int) $jobattrs[stop_timestamp];
238                        $runningtime = $stop_time - $start_time;
239
240                        switch( $sortby ) {
241                                case "id":
242                                        $sorted[$jobid] = $jobid;
243                                        break;
244
245                                case "state":
246                                        $sorted[$jobid] = $state;
247                                        break;
248
249                                case "user":
250                                        $sorted[$jobid] = $user;
251                                        break;
252
253                                case "queue":
254                                        $sorted[$jobid] = $queue;
255                                        break;
256
257                                case "name":
258                                        $sorted[$jobid] = $name;
259                                        break;
260
261                                case "req_cpu":
262                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
263                                        break;
264
265                                case "req_mem":
266                                        $sorted[$jobid] = $req_memory;
267                                        break;
268
269                                case "nodes":
270                                        $sorted[$jobid] = $mynodes;
271                                        break;
272
273                                case "cpus":
274                                        $sorted[$jobid] = $cpus;
275                                        break;
276
277                                case "start":
278                                        $sorted[$jobid] = $start_time;
279                                        break;
280
281                                case "finished":
282                                        $sorted[$jobid] = $stop_time;
283                                        break;
284
285                                case "runningtime":
286                                        $sorted[$jobid] = $runningtime;
287                                        break;
288
289                                default:
290                                        break;
291
292                        }
293        }
294
295        //uasort( $sorted, $cmp );
296        if( $sortorder == "asc" )
297                arsort( $sorted );
298        else if( $sortorder == "desc" )
299                asort( $sorted );
300
301        //print_r( $sorted );
302
303        return array_keys( $sorted );
304}
305
306function makeSearchPage() {
307        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
308        global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up;
309        global $period_start, $period_stop, $sortby, $sortorder;
310
311        $metricname = $m;
312        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
313        //printf("start = %s stop = %s\n", $start, $stop );
314
315        $tpl->assign( "cluster", $clustername );
316        $tpl->assign( "id_value", $id );
317        $tpl->assign( "user_value", $user );
318        $tpl->assign( "queue_value", $queue );
319        $tpl->assign( "name_value", $name );
320        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
321        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
322        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
323        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
324
325        if( validateFormInput() ) {
326
327                $tpl->newBlock( "search_results" );
328                $tpl->assign( "sortby", $sortby);
329                $tpl->assign( "sortorder", $sortorder);
330                $tdb = new TarchDbase();
331                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
332                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
333                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
334                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
335                $search_ids = $tdb->searchDbase( $id, $queue, $user, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
336
337                $jobs = array();
338                $nodes = array();
339
340                //print_r( $search_ids );
341
342                foreach( $search_ids as $myid ) {
343
344                        //printf( "myid %s\n", $myid );
345                        $jobs[$myid] = $tdb->getJobArray( $myid );
346                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
347                }
348
349                //print_r( $nodes );
350                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
351
352                //print_r( $sorted_search );
353                foreach( $sorted_search as $sortid ) {
354
355                        $job = $jobs[$sortid];
356                        //print_r( $job );
357                        $foundid = $job[id];
358                        //printf( "foundid %s\n", $foundid );
359
360                        //$job = $tdb->getJobArray( $foundid );
361                        //$nodes = $tdb->getNodesForJob( $foundid );
362
363                        $tpl->newBlock( "node" );
364                        $tpl->assign( "id", $job[id] );
365                        $tpl->assign( "state", $job[status] );
366                        $tpl->assign( "user", $job[owner] );
367                        $tpl->assign( "queue", $job[queue] );
368                        $tpl->assign( "name", $job[name] );
369                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job[requested_time] ) ) );
370                        $tpl->assign( "req_memory", $job[requested_memory] );
371
372                        $nodes_nr = count( $nodes[$foundid] );
373
374                        // need to replace later with domain stored from dbase
375                        //
376                        //$job_domain = $job[domain];
377
378                        //$myhost = $_SERVER[HTTP_HOST];
379                        //$myhf = explode( '.', $myhost );
380                        //$myhf = array_reverse( $myhf );
381                        //array_pop( $myhf );
382                        //$myhf = array_reverse( $myhf );
383                        //$job_domain = implode( '.', $myhf );
384                       
385                        //print_r( $job );
386                        //printf( "job domain = %s\n", $job_domain);
387                        $ppn = (int) $job[ppn] ? $job[ppn] : 1;
388                        $cpus = $nodes_nr * $ppn;
389
390                        $tpl->assign( "nodes", $nodes_nr );
391                        $tpl->assign( "cpus", $cpus );
392
393                        $job_start = $job[start_timestamp];
394                        $job_stop = $job[stop_timestamp];
395                        $runningtime = intval( $job_stop - $job_start );
396                        $tpl->assign( "started", makeDate( $job_start ) );
397                        $tpl->assign( "finished", makeDate( $job_stop ) );
398                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
399                       
400                        //print_r( $job );
401                        //print_r( $nodes );
402                }
403
404               
405                if( count( $search_ids ) == 1 ) {
406
407                        $tpl->newBlock( "showhosts" );
408
409                        $showhosts = isset($sh) ? $sh : $default_showhosts;
410                        //if( !$showhosts) $showhosts = $default_showhosts;
411                        $tpl->assign("checked$showhosts", "checked");
412
413                        # Present a width list
414                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
415
416                        $hostcols = ($hc) ? $hc : 4;
417
418                        foreach(range(1,25) as $cols) {
419                                $cols_menu .= "<OPTION VALUE=$cols ";
420                                if ($cols == $hostcols)
421                                        $cols_menu .= "SELECTED";
422                                $cols_menu .= ">$cols\n";
423                        }
424                        $cols_menu .= "</SELECT>\n";
425
426                        $tpl->assign("metric","$metricname $units");
427                        $tpl->assign("id", $id);
428                        # Host columns menu defined in header.php
429                        $tpl->assign("cols_menu", $cols_menu);
430
431                        if( $showhosts ) {
432                                //bla
433
434                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
435                                //printf("start = %s stop = %s\n", $start, $stop );
436
437                                if( !$period_start ) // Add an extra 10% to graphstart
438                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
439                                else
440                                        $period_start = datetimeToEpoch( $period_start );
441
442                                if( !$period_stop ) // Add an extra 10% to graphend
443                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
444                                else
445                                        $period_stop = datetimeToEpoch( $period_stop );
446
447                                //printf("start = %s stop = %s\n", $start, $stop );
448
449                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
450                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
451
452                                $hosts_up = array();
453
454                                foreach( $nodes[$jobid] as $mynode )
455                                        $hosts_up[] = $mynode[hostname];
456
457                                //print_r( $hosts_up );
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="\"?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.