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

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

toga/libtoga.php:

  • Only search jobs in 'F' state

toga/search.php:

  • Fixed form validation

web/addons/toga/graph.php:

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