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

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