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

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

web/addons/toga/search.php:

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