source: branches/1.0/web/addons/job_monarch/search.php @ 836

Last change on this file since 836 was 817, checked in by ramonb, 11 years ago
  • find limits does not work for archive
  • Property svn:keywords set to Id
File size: 18.0 KB
Line 
1<?php
2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006-2013  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 * SVN $Id: search.php 817 2013-04-11 09:05:33Z ramonb $
23 */
24
25include_once "./dwoo/dwooAutoload.php";
26
27global $dwoo;
28
29global $clustername, $m, $metric;
30
31function validateFormInput() {
32    global $clustername, $dwoo, $id, $owner, $name, $start_from_time, $start_to_time, $queue;
33    global $end_from_time, $end_to_time, $period_start, $period_stop, $tpl_data;
34
35    $error = false;
36    $error_msg = "<FONT COLOR=\"red\"><B>";
37    $show_msg = 0;
38
39    $none_set = 0;
40
41    if( $id == '' and $owner== '' and $name == '' and $start_from_time == '' and $start_to_time == '' and $queue == '' and $end_from_time == '' and $end_to_time == '') {
42        $error = true;
43        $show_msg = 1;
44        $error_msg .= "No search criteria set!";
45    }
46
47    if( !$error and $period_start != '' ) 
48    {
49        $pstart_epoch = datetimeToEpoch( $period_start );
50        if( $period_stop != '' ) 
51        {
52
53            $pstop_epoch = datetimeToEpoch( $period_stop );
54
55            if( $pstart_epoch > $pstop_epoch ) {
56
57                $show_msg = 1;
58                $error = true;
59                $error_msg .= "Graph timeperiod reset: start date/time can't be later than end";
60                $period_stop = '';
61                $period_start = '';
62            } else if( $pstop_epoch == $pstart_epoch ) {
63
64                $show_msg = 1;
65                $error = true;
66                $error_msg .= "Graph timeperiod reset: start and end date/time can't be the same";
67                $period_stop = '';
68                $period_start = '';
69            }
70        }
71    }
72
73    $error_msg .= "</B></FONT>";
74
75    return array( $error_msg, $error);
76}
77
78function datetimeToEpoch( $datetime ) {
79
80    $datetime_fields = explode( ' ', $datetime );
81
82    $date = $datetime_fields[0];
83    $time = $datetime_fields[1];
84
85    $date_fields = explode( '-', $date );
86
87    $days = $date_fields[0];
88    $months = $date_fields[1];
89    $years = $date_fields[2];
90
91    $time_fields = explode( ':', $time );
92
93    $hours = $time_fields[0];
94    $minutes = $time_fields[1];
95    $seconds = $time_fields[2];
96
97    $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
98
99    return $timestamp;
100}
101
102function timeToEpoch( $time ) {
103
104        $time_fields = explode( ':', $time );
105
106        if( count($time_fields) == 3 ) {
107
108                $hours = $time_fields[0];
109                $minutes = $time_fields[1];
110                $seconds = $time_fields[2];
111
112        } else if( count($time_fields) == 2 ) {
113
114                $hours = 0;
115                $minutes = $time_fields[0];
116                $seconds = $time_fields[1];
117
118        } else if( count($time_fields) == 1 ) {
119
120                $hours = 0;
121                $minutes = 0;
122                $seconds = $time_fields[0];
123        }
124
125        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
126
127        return $myepoch;
128}
129
130function sortJobs( $jobs, $nodes, $sortby, $sortorder ) {
131
132        $sorted = array();
133
134        $cmp = create_function( '$a, $b',
135                "global \$sortby, \$sortorder;".
136
137                "if( \$a == \$b ) return 0;".
138
139                "if (\$sortorder==\"desc\")".
140                        "return ( \$a < \$b ) ? 1 : -1;".
141                "else if (\$sortorder==\"asc\")".
142                        "return ( \$a > \$b ) ? 1 : -1;" );
143
144        foreach( $jobs as $jobid => $jobattrs ) {
145
146                        $state = $jobattrs['status'];
147                        $owner = $jobattrs['owner'];
148                        $queue = $jobattrs['queue'];
149                        $name = $jobattrs['name'];
150                        $req_cpu = $jobattrs['requested_time'];
151                        $req_memory = $jobattrs['requested_memory'];
152
153                        $mynodes = count( $nodes[$jobid] );
154
155                        $ppn = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
156                        $cpus = $mynodes * $ppn;
157                        $start_time = (int) $jobattrs['start_timestamp'];
158                        $stop_time = (int) $jobattrs['stop_timestamp'];
159                        $runningtime = $stop_time - $start_time;
160
161                        switch( $sortby ) {
162                                case "id":
163                                        $sorted[$jobid] = $jobid;
164                                        break;
165
166                                case "state":
167                                        $sorted[$jobid] = $state;
168                                        break;
169
170                                case "owner":
171                                        $sorted[$jobid] = $owner;
172                                        break;
173
174                                case "queue":
175                                        $sorted[$jobid] = $queue;
176                                        break;
177
178                                case "name":
179                                        $sorted[$jobid] = $name;
180                                        break;
181
182                                case "req_cpu":
183                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
184                                        break;
185
186                                case "req_mem":
187                                        $sorted[$jobid] = $req_memory;
188                                        break;
189
190                                case "nodes":
191                                        $sorted[$jobid] = $mynodes;
192                                        break;
193
194                                case "cpus":
195                                        $sorted[$jobid] = $cpus;
196                                        break;
197
198                                case "start":
199                                        $sorted[$jobid] = $start_time;
200                                        break;
201
202                case "finished":
203                    $sorted[$jobid] = $stop_time;
204                    break;
205
206                                case "runningtime":
207                                        $sorted[$jobid] = $runningtime;
208                                        break;
209
210                                default:
211                                        break;
212
213                        }
214        }
215
216        if( $sortorder == "asc" )
217                arsort( $sorted );
218        else if( $sortorder == "desc" )
219                asort( $sorted );
220
221        return array_keys( $sorted );
222}
223
224function makeSearchPage() {
225    global $clustername, $dwoo, $id, $owner, $name, $start_from_time, $start_to_time, $queue;
226    global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up, $hc;
227    global $period_start, $period_stop, $sortby, $sortorder, $COLUMN_REQUESTED_MEMORY;
228    global $SEARCH_RESULT_LIMIT, $COLUMN_NODES, $metricname, $self, $conf;
229
230    $longtitle = "Batch Archive Search :: Powered by Job Monarch!";
231    $title = "Batch Archive Search";
232
233    makeHeader( 'search', $title, $longtitle );
234
235    $tpl = new Dwoo_Template_File("templates/search.tpl");
236    $tpl_data = new Dwoo_Data();
237
238    $tpl_data->assign( "self", $self );
239    $tpl_data->assign( "cluster", $clustername );
240    $tpl_data->assign( "id_value", $id );
241    $tpl_data->assign( "owner_value", $owner);
242    $tpl_data->assign( "queue_value", $queue );
243    $tpl_data->assign( "name_value", $name );
244    $tpl_data->assign( "start_from_value", rawurldecode( $start_from_time ) );
245    $tpl_data->assign( "start_to_value", rawurldecode( $start_to_time ) );
246    $tpl_data->assign( "end_from_value", rawurldecode( $end_from_time ) );
247    $tpl_data->assign( "end_to_value", rawurldecode( $end_to_time ) );
248
249    list( $form_error_msg, $form_error ) = validateFormInput();
250
251    if( $form_error == true )
252    {
253        $tpl_data->assign( "form_error_msg", $form_error_msg );
254    } 
255    else if( $form_error == false ) 
256    {
257        $tpl_data->assign( "search_results", "yes" );
258        $tpl_data->assign( "sortby", $sortby);
259        $tpl_data->assign( "sortorder", $sortorder);
260        $tpl_data->assign( "form_name", "archive_search_form" );
261        $tdb = new TarchDbase( "127.0.0.1" );
262        if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
263        if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
264        if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
265        if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
266        $search_ids = $tdb->searchDbase( $id, $queue, $owner, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
267
268        //print_r( $search_ids );
269        if( ($tdb->resultcount) > (int) $SEARCH_RESULT_LIMIT ) {
270       
271            $tpl_data->assign( "form_error_msg", "Got " . $tdb->resultcount . " search results, output limited to last " . $SEARCH_RESULT_LIMIT . " jobs." );
272        }
273
274        $jobs = array();
275        $nodes = array();
276
277        $even = 1;
278
279        foreach( $search_ids as $myid ) {
280
281            $jobs[$myid] = $tdb->getJobArray( $myid );
282            $nodes[$myid] = $tdb->getNodesForJob( $myid );
283        }
284
285        if( $COLUMN_REQUESTED_MEMORY ) {
286            $tpl_data->assign( "column_header_req_mem", "yes" );
287        }
288        if( $COLUMN_NODES ) {
289            $tpl_data->assign( "column_header_nodes", "yes" );
290        }
291
292        $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
293
294        $node_loop = array();
295        foreach( $sorted_search as $sortid ) {
296
297            $job = $jobs[$sortid];
298            $foundid = $job['id'];
299
300            $node_list = array();
301            $node_list["id"]= $job['id'];
302            $node_list["state"]= $job['status'];
303            $node_list["owner"]= $job['owner'];
304            $node_list["queue"]= $job['queue'];
305            $node_list["name"]= $job['name'];
306            $node_list["req_cpu"]= makeTime( TimeToEpoch( $job['requested_time'] ) );
307
308            if( $COLUMN_REQUESTED_MEMORY ) 
309            {
310                $node_list["column_req_mem"] = "yes";
311                $node_list["req_memory"]= $job['requested_memory'];
312            }
313            if( $COLUMN_NODES) 
314            {
315
316                $job_nodes    = array();
317
318                foreach( $nodes[$foundid] as $mynode )
319                {
320                    if( strpos( $mynode['hostname'], "." ) !== false )
321                    {
322                        $shorthost = explode( '.', $mynode['hostname'] );
323                        $job_nodes[] = $shorthost[0];
324                    }
325                    else
326                    {
327                        $job_nodes[] = $mynode['hostname'];
328                    }
329                }
330
331                $node_list["column_nodes_hostnames"] = "yes";
332                $nodes_hostnames = implode( " ", $job_nodes );
333                $node_list["nodes_hostnames"]= $nodes_hostnames;
334            }
335
336            $nodes_nr = count( $nodes[$foundid] );
337
338            if( $even ) {
339
340                $node_list["nodeclass"]= "even";
341                $even = 0;
342            } else {
343
344                $node_list["nodeclass"]= "odd";
345                $even = 1;
346            }
347
348            $ppn = (int) $job['ppn'] ? $job['ppn'] : 1;
349            $cpus = $nodes_nr * $ppn;
350
351            $node_list["nodes"]= $nodes_nr;
352            $node_list["cpus"]= $cpus;
353
354            $job_start = $job['start_timestamp'];
355            $job_stop = $job['stop_timestamp'];
356            $runningtime = intval( $job_stop - $job_start );
357            $node_list["started"]= makeDate( $job_start );
358            $node_list["finished"]= makeDate( $job_stop );
359            $node_list["runningtime"]= makeTime( $runningtime );
360           
361            $node_loop[]=$node_list;
362        }
363        //print_r( $node_loop );
364        $tpl_data->assign("node_list", $node_loop );
365
366        if( count( $search_ids ) == 1 ) {
367
368            $tpl_data->assign( "showhosts", "yes" );
369
370            $showhosts = isset($sh) ? $sh : $default_showhosts;
371            $tpl_data->assign("checked$showhosts", "checked");
372
373            if( $showhosts ) {
374
375                if( !$period_start ) // Add an extra 10% to graphstart
376                    $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
377                else
378                    $period_start = datetimeToEpoch( $period_start );
379
380                if( !$period_stop ) // Add an extra 10% to graphend
381                    $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
382                else
383                    $period_stop = datetimeToEpoch( $period_stop );
384
385                $tpl_data->assign( "timeperiod", "yes" );
386
387                $tpl_data->assign("period_start", epochToDatetime( $period_start ) );
388                $tpl_data->assign("period_stop", epochToDatetime( $period_stop ) );
389
390                $hosts_up = array();
391
392                foreach( $nodes[$id] as $mynode )
393                    $hosts_up[] = $mynode['hostname'];
394
395                $sorted_hosts = array();
396
397                foreach ($hosts_up as $host ) {
398                    $cpus = $metrics[$host]["cpu_num"]['VAL'];
399                    if (!$cpus) $cpus=1;
400                    $load_one  = $metrics[$host]["load_one"]['VAL'];
401                    $load = ((float) $load_one)/$cpus;
402                    $host_load[$host] = $load;
403                    $percent_hosts['load_color'.($load)] += 1;
404                    if ($metricname=="load_one")
405                        $sorted_hosts[$host] = $load;
406                    else
407                        $sorted_hosts[$host] = $metrics[$host][$metricname]['VAL'];
408                }
409                switch ($sort) {
410                    case "descending":
411                        arsort($sorted_hosts);
412                        break;
413                    case "by hostname":
414                        ksort($sorted_hosts);
415                        break;
416                    default:
417                    case "ascending":
418                        asort($sorted_hosts);
419                        break;
420                }
421
422                $trd = new TarchRrdGraph( $clustername, $host );
423                $rrd_dirs = $trd->getRrdDirs( $period_start, $period_stop );
424
425                $metric_menu = "<B>Metric</B>&nbsp;&nbsp;"
426                    ."<SELECT NAME=\"m\" OnChange=\"toga_form.submit();\">\n";
427
428                $archive_reports = array( "load_report", "cpu_report", "network_report", "packet_report", "mem_report" );
429                $file_metrics = $trd->dirList( $rrd_dirs[0] );
430
431                $archive_metrics = array();
432
433                foreach( $file_metrics as $k )
434                {
435                    $a_metric_file = explode( '.', $k);
436                    $archive_metrics[] = $a_metric_file[0];
437                }
438
439                $sorted_metrics = array_merge( $archive_reports, $archive_metrics );
440
441                asort( $sorted_metrics );
442
443                $selected_metric = $conf['default_metric'];
444
445                if( $metricname != '')
446                {
447                    $selected_metric = $metricname;
448                }
449
450                foreach( $sorted_metrics as $k )
451                {
452                    $url = rawurlencode($k);
453                    $metric_menu .= "<OPTION VALUE=\"$url\" ";
454                    if ($k == $selected_metric )
455                        $metric_menu .= "SELECTED";
456                    $metric_menu .= ">$k\n";
457                }
458                $metric_menu .= "</SELECT>\n";
459
460                $tpl_data->assign("metric_menu", $metric_menu );
461
462                # First pass to find the max value in all graphs for this
463                # metric. The $start,$end variables comes from get_context.php,
464                # included in index.php.
465                #list($min, $max) = find_limits($sorted_hosts, $metricname);
466
467                $sorted_loop = array();
468                # Second pass to output the graphs or metrics.
469                $i = 1;
470                foreach ( $sorted_hosts as $host=>$value  ) {
471                    $sorted_list = array();
472                    $host_url = rawurlencode($host);
473                    $cluster_url = rawurlencode($clustername);
474
475                    $textval = "";
476                    $val = $metrics[$host][$metricname];
477                    $class = "metric";
478                    $host_link="\"?j_view=host&self=$self&c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
479
480                    if ($val['TYPE']=="timestamp" or $always_timestamp[$metricname]) {
481                        $textval = date("r", $val['VAL']);
482                    } elseif ($val['TYPE']=="string" or $val['SLOPE']=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
483                        $textval = $val['VAL']." ".$val['UNITS'];
484                    } else {
485                        $graphargs = "z=medium&c=$cluster_url&m=$metricname&h=$host_url&v=".$val['VAL']."&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop";
486                    }
487                    if ($textval) {
488                        $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
489                    } else {
490                        $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
491                    }
492
493                    $sorted_list["metric_image"]= $cell;
494                    if (! ($i++ % $hostcols) )
495                         $sorted_list["br"]= "</tr><tr>";
496
497                    $sorted_loop[]=$sorted_list;
498                }
499                $tpl_data->assign("sorted_list", $sorted_loop );
500            }
501        }
502
503    }
504    $dwoo->output($tpl, $tpl_data);
505}
506?>
Note: See TracBrowser for help on using the repository browser.