source: branches/0.4/web/addons/job_monarch/search.php @ 778

Last change on this file since 778 was 778, checked in by ramonb, 11 years ago
  • more fixes to search
  • Property svn:keywords set to Id
File size: 16.9 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 778 2013-03-29 16:15:20Z 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( !is_numeric($id) and !$error and $id != '') 
48    {
49
50        $error = true;
51        $show_msg = 1;
52        $error_msg .= "Id must be a number";
53    }
54
55    if( !$error and $period_start != '' ) 
56    {
57        $pstart_epoch = datetimeToEpoch( $period_start );
58        if( $period_stop != '' ) 
59        {
60
61            $pstop_epoch = datetimeToEpoch( $period_stop );
62
63            if( $pstart_epoch > $pstop_epoch ) {
64
65                $show_msg = 1;
66                $error = true;
67                $error_msg .= "Graph timeperiod reset: start date/time can't be later than end";
68                $period_stop = '';
69                $period_start = '';
70            } else if( $pstop_epoch == $pstart_epoch ) {
71
72                $show_msg = 1;
73                $error = true;
74                $error_msg .= "Graph timeperiod reset: start and end date/time can't be the same";
75                $period_stop = '';
76                $period_start = '';
77            }
78        }
79    }
80
81    $error_msg .= "</B></FONT>";
82
83    return array( $error_msg, $error);
84}
85
86function datetimeToEpoch( $datetime ) {
87
88    $datetime_fields = explode( ' ', $datetime );
89
90    $date = $datetime_fields[0];
91    $time = $datetime_fields[1];
92
93    $date_fields = explode( '-', $date );
94
95    $days = $date_fields[0];
96    $months = $date_fields[1];
97    $years = $date_fields[2];
98
99    $time_fields = explode( ':', $time );
100
101    $hours = $time_fields[0];
102    $minutes = $time_fields[1];
103    $seconds = $time_fields[2];
104
105    $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
106
107    return $timestamp;
108}
109
110function timeToEpoch( $time ) {
111
112        $time_fields = explode( ':', $time );
113
114        if( count($time_fields) == 3 ) {
115
116                $hours = $time_fields[0];
117                $minutes = $time_fields[1];
118                $seconds = $time_fields[2];
119
120        } else if( count($time_fields) == 2 ) {
121
122                $hours = 0;
123                $minutes = $time_fields[0];
124                $seconds = $time_fields[1];
125
126        } else if( count($time_fields) == 1 ) {
127
128                $hours = 0;
129                $minutes = 0;
130                $seconds = $time_fields[0];
131        }
132
133        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
134
135        return $myepoch;
136}
137
138function sortJobs( $jobs, $nodes, $sortby, $sortorder ) {
139
140        $sorted = array();
141
142        $cmp = create_function( '$a, $b',
143                "global \$sortby, \$sortorder;".
144
145                "if( \$a == \$b ) return 0;".
146
147                "if (\$sortorder==\"desc\")".
148                        "return ( \$a < \$b ) ? 1 : -1;".
149                "else if (\$sortorder==\"asc\")".
150                        "return ( \$a > \$b ) ? 1 : -1;" );
151
152        foreach( $jobs as $jobid => $jobattrs ) {
153
154                        $state = $jobattrs['status'];
155                        $owner = $jobattrs['owner'];
156                        $queue = $jobattrs['queue'];
157                        $name = $jobattrs['name'];
158                        $req_cpu = $jobattrs['requested_time'];
159                        $req_memory = $jobattrs['requested_memory'];
160
161                        $mynodes = count( $nodes[$jobid] );
162
163                        $ppn = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
164                        $cpus = $mynodes * $ppn;
165                        $start_time = (int) $jobattrs['start_timestamp'];
166                        $stop_time = (int) $jobattrs['stop_timestamp'];
167                        $runningtime = $stop_time - $start_time;
168
169                        switch( $sortby ) {
170                                case "id":
171                                        $sorted[$jobid] = $jobid;
172                                        break;
173
174                                case "state":
175                                        $sorted[$jobid] = $state;
176                                        break;
177
178                                case "owner":
179                                        $sorted[$jobid] = $owner;
180                                        break;
181
182                                case "queue":
183                                        $sorted[$jobid] = $queue;
184                                        break;
185
186                                case "name":
187                                        $sorted[$jobid] = $name;
188                                        break;
189
190                                case "req_cpu":
191                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
192                                        break;
193
194                                case "req_mem":
195                                        $sorted[$jobid] = $req_memory;
196                                        break;
197
198                                case "nodes":
199                                        $sorted[$jobid] = $mynodes;
200                                        break;
201
202                                case "cpus":
203                                        $sorted[$jobid] = $cpus;
204                                        break;
205
206                                case "start":
207                                        $sorted[$jobid] = $start_time;
208                                        break;
209
210                case "finished":
211                    $sorted[$jobid] = $stop_time;
212                    break;
213
214                                case "runningtime":
215                                        $sorted[$jobid] = $runningtime;
216                                        break;
217
218                                default:
219                                        break;
220
221                        }
222        }
223
224        if( $sortorder == "asc" )
225                arsort( $sorted );
226        else if( $sortorder == "desc" )
227                asort( $sorted );
228
229        return array_keys( $sorted );
230}
231
232function makeSearchPage() {
233    global $clustername, $dwoo, $id, $owner, $name, $start_from_time, $start_to_time, $queue;
234    global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up, $hc;
235    global $period_start, $period_stop, $sortby, $sortorder, $COLUMN_REQUESTED_MEMORY;
236    global $SEARCH_RESULT_LIMIT, $COLUMN_NODES, $metricname;
237
238    $longtitle = "Batch Archive Search :: Powered by Job Monarch!";
239    $title = "Batch Archive Search";
240
241    makeHeader( 'search', $title, $longtitle );
242
243    $tpl = new Dwoo_Template_File("templates/search.tpl");
244    $tpl_data = new Dwoo_Data();
245
246    $tpl_data->assign( "cluster", $clustername );
247    $tpl_data->assign( "id_value", $id );
248    $tpl_data->assign( "owner_value", $owner);
249    $tpl_data->assign( "queue_value", $queue );
250    $tpl_data->assign( "name_value", $name );
251    $tpl_data->assign( "start_from_value", rawurldecode( $start_from_time ) );
252    $tpl_data->assign( "start_to_value", rawurldecode( $start_to_time ) );
253    $tpl_data->assign( "end_from_value", rawurldecode( $end_from_time ) );
254    $tpl_data->assign( "end_to_value", rawurldecode( $end_to_time ) );
255
256    list( $form_error_msg, $form_error ) = validateFormInput();
257
258    if( $form_error == true )
259    {
260        $tpl_data->assign( "form_error_msg", $form_error_msg );
261    } 
262    else if( $form_error == false ) 
263    {
264        $tpl_data->assign( "search_results", "yes" );
265        $tpl_data->assign( "sortby", $sortby);
266        $tpl_data->assign( "sortorder", $sortorder);
267        $tdb = new TarchDbase( "127.0.0.1" );
268        if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
269        if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
270        if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
271        if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
272        $search_ids = $tdb->searchDbase( $id, $queue, $owner, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
273
274        //print_r( $search_ids );
275        if( ($tdb->resultcount) > (int) $SEARCH_RESULT_LIMIT ) {
276       
277            $tpl_data->assign( "form_error_msg", "Got " . $tdb->resultcount . " search results, output limited to last " . $SEARCH_RESULT_LIMIT . " jobs." );
278        }
279
280        $jobs = array();
281        $nodes = array();
282
283        $even = 1;
284
285        foreach( $search_ids as $myid ) {
286
287            $jobs[$myid] = $tdb->getJobArray( $myid );
288            $nodes[$myid] = $tdb->getNodesForJob( $myid );
289        }
290
291        if( $COLUMN_REQUESTED_MEMORY ) {
292            $tpl_data->assign( "column_header_req_mem", "yes" );
293        }
294        if( $COLUMN_NODES ) {
295            $tpl_data->assign( "column_header_nodes", "yes" );
296        }
297
298        $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
299
300        $node_loop = array();
301        foreach( $sorted_search as $sortid ) {
302
303            $job = $jobs[$sortid];
304            $foundid = $job['id'];
305
306            $node_list = array();
307            $node_list["id"]= $job['id'];
308            $node_list["state"]= $job['status'];
309            $node_list["owner"]= $job['owner'];
310            $node_list["queue"]= $job['queue'];
311            $node_list["name"]= $job['name'];
312            $node_list["req_cpu"]= makeTime( TimeToEpoch( $job['requested_time'] ) );
313
314            if( $COLUMN_REQUESTED_MEMORY ) {
315                $node_list["column_req_mem"] = "yes";
316                $node_list["req_memory"]= $job['requested_memory'];
317            }
318            if( $COLUMN_NODES) {
319
320                $job_nodes    = array();
321
322                foreach( $nodes[$foundid] as $mynode )
323                    $job_nodes[] = $mynode['hostname'];
324
325                $node_list["column_nodes"] = "yes";
326                $nodes_hostnames = implode( " ", $job_nodes );
327                $node_list["nodes_hostnames"]= $nodes_hostnames;
328            }
329
330            $nodes_nr = count( $nodes[$foundid] );
331
332            if( $even ) {
333
334                $node_list["nodeclass"]= "even";
335                $even = 0;
336            } else {
337
338                $node_list["nodeclass"]= "odd";
339                $even = 1;
340            }
341
342            $ppn = (int) $job['ppn'] ? $job['ppn'] : 1;
343            $cpus = $nodes_nr * $ppn;
344
345            $node_list["nodes"]= $nodes_nr;
346            $node_list["cpus"]= $cpus;
347
348            $job_start = $job['start_timestamp'];
349            $job_stop = $job['stop_timestamp'];
350            $runningtime = intval( $job_stop - $job_start );
351            $node_list["started"]= makeDate( $job_start );
352            $node_list["finished"]= makeDate( $job_stop );
353            $node_list["runningtime"]= makeTime( $runningtime );
354           
355            $node_loop[]=$node_list;
356        }
357        //print_r( $node_loop );
358        $tpl_data->assign("node_list", $node_loop );
359
360        if( count( $search_ids ) == 1 ) {
361
362            $tpl_data->assign( "showhosts", "yes" );
363
364            $showhosts = isset($sh) ? $sh : $default_showhosts;
365            $tpl_data->assign("checked$showhosts", "checked");
366
367            # Present a width list
368            $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
369
370            $hostcols = ($hc) ? $hc : 4;
371
372            foreach(range(1,25) as $cols) {
373                $cols_menu .= "<OPTION VALUE=$cols ";
374                if ($cols == $hostcols)
375                    $cols_menu .= "SELECTED";
376                $cols_menu .= ">$cols\n";
377            }
378            $cols_menu .= "</SELECT>\n";
379
380            $tpl_data->assign("metric","$metricname $units");
381            $tpl_data->assign("id", $id);
382            # Host columns menu defined in header.php
383            $tpl_data->assign("cols_menu", $cols_menu);
384
385            if( $showhosts ) {
386
387                if( !$period_start ) // Add an extra 10% to graphstart
388                    $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
389                else
390                    $period_start = datetimeToEpoch( $period_start );
391
392                if( !$period_stop ) // Add an extra 10% to graphend
393                    $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
394                else
395                    $period_stop = datetimeToEpoch( $period_stop );
396
397                #        $tpl_data->gotoBlock( "timeperiod" );
398
399                #$tpl_data->assign("period_start", epochToDatetime( $period_start ) );
400                #$tpl_data->assign("period_stop", epochToDatetime( $period_stop ) );
401
402                $hosts_up = array();
403
404                foreach( $nodes[$id] as $mynode )
405                    $hosts_up[] = $mynode['hostname'];
406
407                $sorted_hosts = array();
408
409                foreach ($hosts_up as $host ) {
410                    $cpus = $metrics[$host]["cpu_num"]['VAL'];
411                    if (!$cpus) $cpus=1;
412                    $load_one  = $metrics[$host]["load_one"]['VAL'];
413                    $load = ((float) $load_one)/$cpus;
414                    $host_load[$host] = $load;
415                    $percent_hosts['load_color'.($load)] += 1;
416                    if ($metricname=="load_one")
417                        $sorted_hosts[$host] = $load;
418                    else
419                        $sorted_hosts[$host] = $metrics[$host][$metricname]['VAL'];
420                }
421                switch ($sort) {
422                    case "descending":
423                        arsort($sorted_hosts);
424                        break;
425                    case "by hostname":
426                        ksort($sorted_hosts);
427                        break;
428                    default:
429                    case "ascending":
430                        asort($sorted_hosts);
431                        break;
432                }
433
434                # First pass to find the max value in all graphs for this
435                # metric. The $start,$end variables comes from get_context.php,
436                # included in index.php.
437                list($min, $max) = find_limits($sorted_hosts, $metricname);
438
439                $sorted_loop = array();
440                # Second pass to output the graphs or metrics.
441                $i = 1;
442                foreach ( $sorted_hosts as $host=>$value  ) {
443                    $sorted_list = array();
444                    $host_url = rawurlencode($host);
445                    $cluster_url = rawurlencode($clustername);
446
447                    $textval = "";
448                    $val = $metrics[$host][$metricname];
449                    $class = "metric";
450                    $host_link="\"?j_view=host&c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
451
452                    if ($val['TYPE']=="timestamp" or $always_timestamp[$metricname]) {
453                        $textval = date("r", $val['VAL']);
454                    } elseif ($val['TYPE']=="string" or $val['SLOPE']=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
455                        $textval = $val['VAL']." ".$val['UNITS'];
456                    } else {
457                        $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";
458                    }
459                    if ($textval) {
460                        $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
461                    } else {
462                        $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
463                    }
464
465                    $sorted_list["metric_image"]= $cell;
466                    if (! ($i++ % $hostcols) )
467                         $sorted_list["br"]= "</tr><tr>";
468
469                    $sorted_loop[]=$sorted_list;
470                }
471                $tpl_data->assign("sorted_list", $sorted_loop );
472            }
473        }
474
475    }
476    $dwoo->output($tpl, $tpl_data);
477}
478?>
Note: See TracBrowser for help on using the repository browser.