source: trunk/web/addons/toga/search.php @ 157

Last change on this file since 157 was 157, checked in by bastiaans, 19 years ago

toga/search.php:

  • Added checks for graph timeperiod

toga/templates/search.tpl:

  • Changed place of error_msg
File size: 12.1 KB
Line 
1<?php
2
3global $clustername, $tpl;
4
5function validateFormInput() {
6        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
7        global $end_from_time, $end_to_time, $period_start, $period_stop;
8
9        $error = 0;
10        $error_msg = "<FONT COLOR=\"red\"><B>";
11        $show_msg = 0;
12
13        $none_set = 0;
14
15        if( $id == '' and $user == '' and $name == '' and $start_from_time == '' and $start_to_time == '' and $queue == '' and $end_from_time == '' and $end_to_time == '') {
16                $error = 1;
17                $show_msg = 1;
18                $error_msg .= "No search criteria set!";
19        }
20
21        if( !is_numeric($id) and !$error and $id != '') {
22
23                $error = 1;
24                $show_msg = 1;
25                $error_msg .= "Id must be a number";
26        }
27
28        //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
29
30        if( !$error and $period_start != '' ) {
31                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
32                $pstart_epoch = datetimeToEpoch( $period_start );
33                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
34                if( $period_stop != '' ) {
35
36                        $pstop_epoch = datetimeToEpoch( $period_stop );
37                        printf( "pstop_epoch = %s pstart_epoch = %s\n", $pstop_epoch, $pstart_epoch );
38
39                        if( $pstart_epoch > $pstop_epoch ) {
40
41                                $show_msg = 1;
42                                $error_msg .= "Graph timeperiod reset: start date/time can't be later than end";
43                                $period_stop = '';
44                                $period_start = '';
45                        } else if( $pstop_epoch == $pstart_epoch ) {
46
47                                $show_msg = 1;
48                                $error_msg .= "Graph timeperiod reset: start and end date/time can't be the same";
49                                $period_stop = '';
50                                $period_start = '';
51                        }
52                }
53        }
54
55        $error_msg .= "</B></FONT>";
56        // doe checks en set error en error_msg in case shit
57
58        if( $show_msg )
59                $tpl->assign( "form_error_msg", $error_msg );
60
61        return ($error ? 0 : 1 );
62}
63
64function makeTime( $time ) {
65
66        $days = intval( $time / 86400 );
67        $time = ($days>0) ? $time % ($days * 86400) : $time;
68
69        //printf( "time = %s, days = %s\n", $time, $days );
70
71        $date_str = '';
72        $day_str = '';
73
74        if( $days > 0 ) {
75                if( $days > 1 )
76                        $day_str .= $days . ' days';
77                else
78                        $day_str .= $days . ' day';
79        }
80
81        $hours = intval( $time / 3600 );
82        $time = $hours ? $time % ($hours * 3600) : $time;
83
84        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
85
86        if( $hours > 0 ) {
87                $date_str .= $hours . ':';
88                $date_unit = 'hours';
89        }
90
91        $minutes = intval( $time / 60 );
92        $seconds = $minutes ? $time % ($minutes * 60) : $time;
93
94        if( $minutes > 0 ) {
95
96                if( $minutes >= 10 )
97                        $date_str .= $minutes . ':';
98                else
99                        $date_str .= '0' . $minutes . ':';
100
101                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
102        } else {
103                if($hours > 0 ) {
104                        $date_str .= '00:';
105                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
106                }
107        }
108
109
110        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
111
112        if( $seconds > 0 ) {
113
114                if( $seconds >= 10 )
115                        $date_str .= $seconds . ' ' . $date_unit;
116                else
117                        $date_str .= '0' . $seconds . ' ' . $date_unit;
118
119        } else if ( $hours > 0 or $minutes > 0 )
120
121                $date_str .= '00 ' . $date_unit;
122
123        if( $days > 0) {
124
125                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
126                        $date_str = $day_str . ' - ' . $date_str;
127                else
128                        $date_str = $day_str;
129        }
130
131        return $date_str;
132}
133
134function makeDate( $time ) {
135        return strftime( "%a %d %b %Y %H:%M:%S", $time );
136}
137
138function datetimeToEpoch( $datetime ) {
139
140        //printf("datetime = %s\n", $datetime );
141        $datetime_fields = explode( ' ', $datetime );
142
143        $date = $datetime_fields[0];
144        $time = $datetime_fields[1];
145
146        $date_fields = explode( '-', $date );
147
148        $days = $date_fields[0];
149        $months = $date_fields[1];
150        $years = $date_fields[2];
151
152        //printf( "days = %s months = %s years = %s\n", $days, $months, $years );
153
154        $time_fields = explode( ':', $time );
155
156        $hours = $time_fields[0];
157        $minutes = $time_fields[1];
158        $seconds = $time_fields[2];
159
160        //printf( "hours = %s minutes = %s seconds = %s\n", $hours, $minutes, $seconds );
161
162        $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
163
164        //printf( "timestamp = %s\n", $timestamp );
165
166        return $timestamp;
167}
168
169function epochToDatetime( $epoch ) {
170
171        return strftime( "%d-%m-%Y %H:%M:%S", $epoch );
172}
173
174function timeToEpoch( $time ) {
175
176        $time_fields = explode( ':', $time );
177
178        if( count($time_fields) == 3 ) {
179
180                $hours = $time_fields[0];
181                $minutes = $time_fields[1];
182                $seconds = $time_fields[2];
183
184        } else if( count($time_fields) == 2 ) {
185
186                $hours = 0;
187                $minutes = $time_fields[0];
188                $seconds = $time_fields[1];
189
190        } else if( count($time_fields) == 1 ) {
191
192                $hours = 0;
193                $minutes = 0;
194                $seconds = $time_fields[0];
195        }
196
197        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
198
199        return $myepoch;
200}
201
202function makeSearchPage() {
203        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
204        global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up;
205        global $period_start, $period_stop;
206
207        $metricname = $m;
208        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
209        //printf("start = %s stop = %s\n", $start, $stop );
210
211        $tpl->assign( "cluster", $clustername );
212        $tpl->assign( "id_value", $id );
213        $tpl->assign( "user_value", $user );
214        $tpl->assign( "queue_value", $queue );
215        $tpl->assign( "name_value", $name );
216        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
217        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
218        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
219        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
220
221        if( validateFormInput() ) {
222
223                $tpl->newBlock( "search_results" );
224                $tdb = new TarchDbase();
225                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
226                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
227                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
228                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
229                $search_ids = $tdb->searchDbase( $id, $queue, $user, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
230
231                foreach( $search_ids as $foundid ) {
232
233                        $job = $tdb->getJobArray( $foundid );
234                        $nodes = $tdb->getNodesForJob( $foundid );
235
236                        $tpl->newBlock( "node" );
237                        $tpl->assign( "id", $job[id] );
238                        $tpl->assign( "state", $job[status] );
239                        $tpl->assign( "user", $job[owner] );
240                        $tpl->assign( "queue", $job[queue] );
241                        $tpl->assign( "name", $job[name] );
242                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job[requested_time] ) ) );
243                        $tpl->assign( "req_memory", $job[requested_memory] );
244
245                        $nodes_nr = count( $nodes );
246
247                        // need to replace later with domain stored from dbase
248                        //
249                        //$job_domain = $job[domain];
250
251                        //$myhost = $_SERVER[HTTP_HOST];
252                        //$myhf = explode( '.', $myhost );
253                        //$myhf = array_reverse( $myhf );
254                        //array_pop( $myhf );
255                        //$myhf = array_reverse( $myhf );
256                        //$job_domain = implode( '.', $myhf );
257                       
258                        //print_r( $job );
259                        //printf( "job domain = %s\n", $job_domain);
260                        $ppn = (int) $job[ppn] ? $job[ppn] : 1;
261                        $cpus = $nodes_nr * $ppn;
262
263                        $tpl->assign( "nodes", $nodes_nr );
264                        $tpl->assign( "cpus", $cpus );
265
266                        $job_start = $job[start_timestamp];
267                        $job_stop = $job[stop_timestamp];
268                        $runningtime = intval( $job_stop - $job_start );
269                        $tpl->assign( "started", makeDate( $job_start ) );
270                        $tpl->assign( "finished", makeDate( $job_stop ) );
271                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
272                       
273                        //print_r( $job );
274                        //print_r( $nodes );
275                }
276
277               
278                if( count( $search_ids ) == 1 ) {
279
280                        $tpl->newBlock( "showhosts" );
281
282                        $showhosts = isset($sh) ? $sh : $default_showhosts;
283                        //if( !$showhosts) $showhosts = $default_showhosts;
284                        $tpl->assign("checked$showhosts", "checked");
285
286                        # Present a width list
287                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
288
289                        $hostcols = ($hc) ? $hc : 4;
290
291                        foreach(range(1,25) as $cols) {
292                                $cols_menu .= "<OPTION VALUE=$cols ";
293                                if ($cols == $hostcols)
294                                        $cols_menu .= "SELECTED";
295                                $cols_menu .= ">$cols\n";
296                        }
297                        $cols_menu .= "</SELECT>\n";
298
299                        $tpl->assign("metric","$metricname $units");
300                        $tpl->assign("id", $id);
301                        # Host columns menu defined in header.php
302                        $tpl->assign("cols_menu", $cols_menu);
303
304                        if( $showhosts ) {
305                                //bla
306
307                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
308                                //printf("start = %s stop = %s\n", $start, $stop );
309
310                                if( !$period_start ) // Add an extra 10% to graphstart
311                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
312                                else
313                                        $period_start = datetimeToEpoch( $period_start );
314
315                                if( !$period_stop ) // Add an extra 10% to graphend
316                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
317                                else
318                                        $period_stop = datetimeToEpoch( $period_stop );
319
320                                //printf("start = %s stop = %s\n", $start, $stop );
321
322                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
323                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
324
325                                $hosts_up = array();
326
327                                foreach( $nodes as $mynode )
328                                        $hosts_up[] = $mynode[hostname];
329
330                                //print_r( $hosts_up );
331
332                                $sorted_hosts = array();
333                                //$hosts_up = $jobs[$filter[id]][nodes];
334
335                                foreach ($hosts_up as $host ) {
336                                        //$host = $host. '.'.$job_domain;
337                                        $cpus = $metrics[$host]["cpu_num"][VAL];
338                                        if (!$cpus) $cpus=1;
339                                        $load_one  = $metrics[$host]["load_one"][VAL];
340                                        $load = ((float) $load_one)/$cpus;
341                                        $host_load[$host] = $load;
342                                        $percent_hosts[load_color($load)] += 1;
343                                        if ($metricname=="load_one")
344                                                $sorted_hosts[$host] = $load;
345                                        else
346                                                $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
347                                }
348                                switch ($sort) {
349                                        case "descending":
350                                                arsort($sorted_hosts);
351                                                break;
352                                        case "by hostname":
353                                                ksort($sorted_hosts);
354                                                break;
355                                        default:
356                                        case "ascending":
357                                                asort($sorted_hosts);
358                                                break;
359                                }
360
361                                //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
362
363                                # First pass to find the max value in all graphs for this
364                                # metric. The $start,$end variables comes from get_context.php,
365                                # included in index.php.
366                                list($min, $max) = find_limits($sorted_hosts, $metricname);
367
368                                # Second pass to output the graphs or metrics.
369                                $i = 1;
370                                foreach ( $sorted_hosts as $host=>$value  ) {
371                                        $tpl->newBlock ("sorted_list");
372                                        //$host = $host. '.'.$domain;
373                                        $host_url = rawurlencode($host);
374                                        $cluster_url = rawurlencode($clustername);
375
376                                        $textval = "";
377                                        //printf("host = %s, value = %s", $host, $value);
378                                        //echo "$host: $value, ";
379                                        $val = $metrics[$host][$metricname];
380                                        $class = "metric";
381                                        $host_link="\"?c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
382
383                                        if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
384                                                $textval = date("r", $val[VAL]);
385                                        } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
386                                                $textval = "$val[VAL] $val[UNITS]";
387                                        } else {
388                                                $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";
389                                        }
390                                        if ($textval) {
391                                                $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
392                                        } else {
393                                                $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
394                                        }
395
396                                        $tpl->assign("metric_image", $cell);
397                                        if (! ($i++ % $hostcols) )
398                                                 $tpl->assign ("br", "</tr><tr>");
399                                }
400
401                                //einde bla
402                        }
403                }
404
405        }
406}
407?>
Note: See TracBrowser for help on using the repository browser.