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

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