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

Last change on this file since 713 was 713, checked in by ramonb, 11 years ago
  • cleanup
  • Property svn:keywords set to Id
File size: 14.3 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 713 2013-03-21 20:19:07Z ramonb $
23 */
24
25global $clustername, $tpl, $m, $metric;
26
27function validateFormInput() {
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;
30
31        $error = 0;
32        $error_msg = "<FONT COLOR=\"red\"><B>";
33        $show_msg = 0;
34
35        $none_set = 0;
36
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        }
42
43        if( !is_numeric($id) and !$error and $id != '') 
44    {
45
46                $error = 1;
47                $show_msg = 1;
48                $error_msg .= "Id must be a number";
49        }
50
51        if( !$error and $period_start != '' ) 
52    {
53                $pstart_epoch = datetimeToEpoch( $period_start );
54                if( $period_stop != '' ) 
55        {
56
57                        $pstop_epoch = datetimeToEpoch( $period_stop );
58
59                        if( $pstart_epoch > $pstop_epoch ) {
60
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 ) {
66
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        }
74
75        $error_msg .= "</B></FONT>";
76        // doe checks en set error en error_msg in case shit
77
78        if( $show_msg )
79                $tpl->assign( "form_error_msg", $error_msg );
80
81        return ($error ? 0 : 1 );
82}
83
84function datetimeToEpoch( $datetime ) {
85
86        $datetime_fields = explode( ' ', $datetime );
87
88        $date = $datetime_fields[0];
89        $time = $datetime_fields[1];
90
91        $date_fields = explode( '-', $date );
92
93        $days = $date_fields[0];
94        $months = $date_fields[1];
95        $years = $date_fields[2];
96
97        $time_fields = explode( ':', $time );
98
99        $hours = $time_fields[0];
100        $minutes = $time_fields[1];
101        $seconds = $time_fields[2];
102
103        $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
104
105        return $timestamp;
106}
107
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
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
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'];
158
159                        $mynodes = count( $nodes[$jobid] );
160
161                        $ppn = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
162                        $cpus = $mynodes * $ppn;
163                        $start_time = (int) $jobattrs['start_timestamp'];
164                        $stop_time = (int) $jobattrs['stop_timestamp'];
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
176                                case "owner":
177                                        $sorted[$jobid] = $owner;
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
208                                case "finished":
209                                        $sorted[$jobid] = $stop_time;
210                                        break;
211
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
230function makeSearchPage() {
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;
235
236        $longtitle = "Batch Archive Search :: Powered by Job Monarch!";
237        $title = "Batch Archive Search";
238
239        makeHeader( 'search', $title, $longtitle );
240
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 ) );
250
251        if( validateFormInput() ) {
252
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 );
262
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                }
269
270                $jobs = array();
271                $nodes = array();
272
273                $even = 1;
274
275                foreach( $search_ids as $myid ) {
276
277                        $jobs[$myid] = $tdb->getJobArray( $myid );
278                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
279                }
280
281                if( $COLUMN_REQUESTED_MEMORY ) {
282                        $tpl->newBlock( "column_header_req_mem" );
283                }
284                if( $COLUMN_NODES ) {
285                        $tpl->newBlock( "column_header_nodes" );
286                }
287
288                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
289
290                foreach( $sorted_search as $sortid ) {
291
292                        $job = $jobs[$sortid];
293                        $foundid = $job['id'];
294
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'] ) ) );
302
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) {
309
310                                $job_nodes      = array();
311
312                                foreach( $nodes[$foundid] as $mynode )
313                                        $job_nodes[] = $mynode['hostname'];
314
315                                $tpl->newBlock( "column_nodes" );
316                                $nodes_hostnames = implode( " ", $job_nodes );
317                                $tpl->assign( "nodes_hostnames", $nodes_hostnames );
318                                $tpl->gotoBlock( "node" );
319                        }
320
321                        $nodes_nr = count( $nodes[$foundid] );
322
323                        if( $even ) {
324
325                                $tpl->assign("nodeclass", "even");
326                                $even = 0;
327                        } else {
328
329                                $tpl->assign("nodeclass", "odd");
330                                $even = 1;
331                        }
332
333                        $ppn = (int) $job['ppn'] ? $job['ppn'] : 1;
334                        $cpus = $nodes_nr * $ppn;
335
336                        $tpl->assign( "nodes", $nodes_nr );
337                        $tpl->assign( "cpus", $cpus );
338
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                }
347
348                if( count( $search_ids ) == 1 ) {
349
350                        $tpl->newBlock( "showhosts" );
351
352                        $showhosts = isset($sh) ? $sh : $default_showhosts;
353                        $tpl->assign("checked$showhosts", "checked");
354
355                        # Present a width list
356                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
357
358                        $hostcols = ($hc) ? $hc : 4;
359
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";
367
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);
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->gotoBlock( "timeperiod" );
386
387                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
388                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
389
390                                $tpl->gotoBlock( "_ROOT" );
391
392                                $hosts_up = array();
393
394                                foreach( $nodes[$id] as $mynode )
395                                        $hosts_up[] = $mynode['hostname'];
396
397                                $sorted_hosts = array();
398
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                                }
423
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);
428
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);
435
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\"";
440
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                                        }
453
454                                        $tpl->assign("metric_image", $cell);
455                                        if (! ($i++ % $hostcols) )
456                                                 $tpl->assign ("br", "</tr><tr>");
457                                }
458
459                        }
460                }
461
462        }
463}
464?>
Note: See TracBrowser for help on using the repository browser.