/[omni_gantt]/db2gantt.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /db2gantt.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (show annotations)
Mon Dec 2 14:35:17 2002 UTC (21 years, 3 months ago) by dpavlin
Branch: MAIN
Changes since 1.24: +37 -15 lines
move by 12 hours, day or week using drop-down menu

1 #!/usr/bin/perl -w
2
3 use strict;
4 use DBI;
5 use Data::Dumper;
6 use Date::Parse;
7 use CGI qw/:standard/;
8 use CGI::Carp qw(fatalsToBrowser);
9 use POSIX qw(strftime);
10
11 my $debug = 0;
12
13 my $width = 600; # width of bar
14 my $height = 15; # height of bar
15 my $use_js = 1; # use JavaScript pop-up
16
17 # status colors
18 my %cols = (
19 'In Progress' => '0,255,0',
20 'In Progress/Failures' => '192,64,192',
21 'In Progress/Errors' => '255,128,128',
22 'Queuing' => '255,255,0',
23 'Queuing/Errors' => '255,192,64',
24 'Aborted' => '255,0,0',
25 'Failed' => '255,0,0',
26 'Completed' => '64,255,64',
27 'Completed/Errors' => '255,128,0',
28 'Completed/Failure' => '128,0,128',
29 'Mount Request' => '128,128,255',
30 'Mount/Errors' => '255,64,128',
31 'Mount/Failures' => '255,128,192',
32 );
33
34 my $int_t = (12 * 60 * 60); # interval to display on one screen
35 my $min_l = 3; # min length of bar segment (in pixels)
36
37 #--- no user servicable parts below this line
38
39 my $dbh = DBI->connect("DBI:Pg:dbname=gantt","","") || die $DBI::errstr;
40 my $q=new CGI;
41
42 my ($from_t,$to_t) = (time()-$int_t,time());
43
44 $from_t = param('from_t') if (param('from_t'));
45 $to_t = param('to_t') if (param('to_t'));
46
47 if (param('f-1')) {
48 $from_t = $from_t - param('int_f-1') || $int_t;
49 } elsif (param('f+1')) {
50 $from_t = $from_t + param('int_f+1') || $int_t;
51 } elsif (param('t-1')) {
52 $to_t = $from_t - param('int_t-1') || $int_t;
53 } elsif (param('t+1')) {
54 $to_t = $from_t + $int_t;
55 }
56
57 # time range
58 $int_t = m_round($int_t);
59
60 # round to nearest minute
61 sub m_round {
62 my $t = shift @_;
63 return ($t - ($t % 60));
64 }
65
66 $to_t = m_round($to_t);
67 $from_t = m_round($from_t);
68
69 my $to=strftime("%Y-%m-%d %H:%M",localtime ($to_t));
70 my $from=strftime("%Y-%m-%d %H:%M",localtime ($from_t));
71
72
73 # keep count of each status
74 my %count;
75
76 if (path_info()) {
77 print "Content-type: image/png\nCache-Control: max-age=86400, must-revalidate\nExpires: ",scalar localtime(time()+24*60*60),"\nLast-Modified: ",scalar localtime(0),"\n";
78 # create picture using GD
79 use GD;
80 my $im = new GD::Image(1,$height);
81 my $back = $im->colorAllocate(255,255,255);
82 $im->transparent($back);
83 my $col = path_info(); $col =~ s,/,,g;
84 my ($r,$g,$b) = split(/,/,$col);
85 $col = $im->colorAllocate($r,$g,$b);
86 $im->fill(0,0,$col);
87 print "Content-Length: ",length($im->png),"\n\n";
88 binmode STDOUT;
89 print $im->png;
90 exit;
91 }
92
93 print "Content-type: text/html
94 Cache-Control: max-age=60, must-revalidate
95
96 <html>
97 <head>
98 <title>OmniBack Gantt: $from - $to</title>
99 <meta HTTP-EQUIV=\"Refresh\" CONTENT=600>";
100 if ($use_js) {
101 print '
102 <script type="text/javascript" language="javascript" src="1k.js"></script>
103 <script type="text/javascript" language="javascript" src="tooltip.js"></script>
104 <script language="javascript" type="text/javascript">
105 onload=function(){
106 T.init()
107 T.follows = false // false by default
108 T.delay = .3 // any nonnegative value (0.7 by default)
109 }
110 </script>
111 ';
112 }
113 print "</head><body>
114 <form action=".$q->url(-relative=>1)." method=get>
115 <input type=hidden name=from_t value=$from_t>
116 <input type=hidden name=to_t value=$to_t>
117 ";
118
119 # all vars ending in *_t have utime in them.
120 #
121 my $len_t = $to_t - $from_t;
122
123 die "interval must be positive and bigger than 1 sec !" if (abs($len_t) < 1);
124
125
126 sub mknav {
127 my $f = shift @_; # from_t
128 my $t = shift @_; # to_t
129 my $d = shift @_; # delta
130 my $ch = shift @_; # char
131
132 return "
133 <select name=int_${d}>
134 <option value=".(12*60*60).">12h</option>
135 <option value=".(24*60*60).">day</option>
136 <option value=".(7*24*60*60).">week</option>
137 </select>
138 <input type=submit name=$d value=\"$ch\">
139 ";
140 #return "<a href=\"".$q->url(-relative=>1)."?from_t=${f}&to_t=${t}\">$ch</a>";
141 }
142
143 print "<table cellspacing=0 cellpadding=0>";
144 print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>";
145 print mknav($from_t,$to_t,'f-1','&lt;&lt;'),$from;
146 print mknav($from_t,$to_t,'f+1','&gt;&gt;') if ($from_t+$int_t < $to_t);
147 print "</td><td align=right>";
148 print mknav($from_t,$to_t,'t-1','&lt;&lt;') if ($to_t+$int_t > $from_t);
149 print $to,mknav($from_t,$to_t,'t+1','&gt;&gt;'),"</td></tr>\n";
150
151 # draw hour grid
152 sub hour_grid {
153 print "<tr><td align=right><small>hour grid</small></td><td colspan=2>";
154 my @c = ("255,255,128","255,192,128");
155 my $hr=strftime("%H",localtime ($from_t));
156 sub hour_bar {
157 my ($t,$c,$hr) = @_;
158 my $clock_hr = $hr % 24;
159 my $alt = sprintf("%02d:00",$clock_hr);
160 if ($clock_hr == 0) {
161 my $pix_hr = int(1 / $width);
162 print color_bar($pix_hr,"0,0,0",strftime("%Y-%m-%d", localtime($from_t + $hr*3600)));
163 print color_bar($t-$pix_hr,$c,$alt);
164 } else {
165 print color_bar($t,$c,$alt);
166 }
167 }
168 hour_bar(3600 - $from_t % 3600,$c[0],$hr++);
169 for (my $i=1; $i<int(($to_t-$from_t)/3600); $i++) {
170 hour_bar(3600,$c[1],$hr++);
171 push @c, shift @c;
172 }
173 hour_bar($from_t % 3600,$c[1],$hr);
174 print "</td></tr>\n";
175 }
176
177 hour_grid();
178
179 my $fix_d = 0; # used to fix graph len (in pixels)
180 my $fix_s = 0; # used to collect round errors of size
181
182 sub bar {
183 my $l = shift @_;
184 my $status = shift @_;
185 my $alt = shift @_;
186
187 my $col;
188
189 if ($status) {
190 if ($cols{$status}) {
191 $col .= $cols{$status};
192 } else {
193 $col .= "0,0,0"; # unknown status, black
194 }
195 $count{$status}++;
196 } else {
197 # $col .= '240,240,240';
198 $col .= '220,220,220';
199 }
200 return color_bar($l,$col,$alt,$min_l);
201 }
202
203 sub color_bar {
204 my $l = shift @_; # lenght of event utime
205 my $col = shift @_ || '240,240,240'; # default color (filler)
206 my $alt = shift @_ || undef;
207 my $min_l = shift @_ || 1;
208 my $h = shift @_ || $height;
209
210 my $size = $l / ($len_t / $width);
211 $fix_s += $size - int($size);
212 $size=int($size);
213 # add rounding error to size
214 if ($fix_s > 1) {
215 my $i = $fix_s ; $i = int($i);
216 $fix_s -= $i;
217 $size += $i;
218 }
219
220 if ($alt && $size < $min_l) {
221 $fix_d += ($min_l - $size);
222 print STDERR "fix_d: $fix_d\n" if ($debug);
223 $size = $min_l;
224 }
225 if ($fix_d && $size > $fix_d+$min_l) {
226 $size -= $fix_d;
227 $fix_d = 0;
228 print STDERR "fix_d: $fix_d\n" if ($debug);
229 }
230
231 print STDERR "bar[$col] len:$l s scale:",($len_t/$width)," size:$size px<br> alt:$alt\n" if ($debug);
232
233 my $html = "<img src=\"".$q->url(-relative=>1)."/$col\" width=\"$size\" height=\"$h\"";
234
235 if ($use_js && $alt) {
236 $html .= " onmouseover=\"T('$alt')\" onmouseout=\"T()\"";
237 } elsif ($alt) {
238 $html .= " alt=\"$alt\"";
239 }
240 $html .= ">";
241
242 return($html);
243 }
244
245 my $sql = "select start,finish,specification,status,user_group_host,
246 type,sessionid,device,host
247 from gantt
248 where (start < '$from' and finish > '$from') or
249 (start > '$from' and start < '$to')
250 order by device,specification,start
251 ";
252
253 my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr;
254
255 my %spec; # specification hash
256
257 my $curr_spec = "";
258 my $curr_t = $from_t;
259
260 $sth->execute() || die "sql: $sql ".$dbh->errstr;
261
262 my $dev; # current device
263 my @dev_t; # all minutes in one hour [1440]
264 my @dev_sum_t;
265
266 sub sum_bar {
267 my $label = shift @_;
268 my @dev_t = @_;
269 # display device summary
270 my $max_use = 0;
271 for (my $i=0; $i < $len_t; $i++) {
272 $max_use = $dev_t[$i] if (defined $dev_t[$i] && $dev_t[$i] > $max_use);
273 }
274 print "<tr><td align=right><small>$label [$max_use]</small></td><td colspan=2>";
275 my $last_var = 0;
276 my $len = 0;
277 for (my $i=1; $i<= ($to_t-$from_t); $i++) {
278 my $v = $dev_t[$i] || 0;
279 if ($v == $last_var) {
280 $len++;
281 } else {
282 my $h = $last_var / $max_use * $height;
283 my $c = 255 - int (255 * $last_var / $max_use);
284 print color_bar($len,"$c,$c,$c","$last_var concurrent jobs",1,$h+1);
285 $len = 0;
286 $last_var = $v;
287 }
288 }
289 my $h = $last_var / $max_use * $height;
290 my $c = 255 - int (255 * $last_var / $max_use);
291 print color_bar($len,"$c,$c,$c","$last_var concurrent jobs",1,$h+1) if ($len);
292 print "</td></tr>";
293 }
294
295 my @line_t; # all seconds for this specification
296 my @line_arr = (""); # all segments on line which are not fillers
297
298 sub html_spec {
299 print "<tr><td>$curr_spec</td><td colspan=2>";
300
301 my $last_var = 0;
302 my $len = 0;
303 my $i;
304 for (my $t=0; $t<=$len_t; $t++) {
305 $i = $line_t[$t] || 0;
306 if ($i == $last_var) {
307 $len++;
308 } else {
309 if ($last_var == 0) {
310 print bar($len);
311 } else {
312 print bar($len,split(/\t/,$line_arr[$last_var],2));
313 }
314 $len = 0;
315 $last_var = $i;
316 }
317 }
318 print bar($len,split(/\t/,$line_arr[$i],2));
319
320 print "</td></tr>\n";
321 }
322
323 while(my $row = $sth->fetchrow_hashref) {
324 if ($row->{specification} ne $curr_spec) {
325
326 if ($curr_t < $to_t && $curr_spec) {
327 my $t = $to_t - $curr_t;
328 print STDERR "[end filler $curr_t:$t]" if ($debug);
329 # print bar($t);
330 }
331
332 # print "</td></tr>\n" if ($curr_t != $from_t);
333
334
335 # init vars for next line
336 ($fix_s,$fix_d) = (0,0); # init fix vars for bar
337
338 html_spec if ($curr_spec ne "");
339
340 $curr_t = $from_t; # init timeline
341 $curr_spec = $row->{specification};
342
343 @line_t = ();
344 @line_arr = ( "" );
345
346 if ($dev && @dev_t && $dev ne $row->{device}) {
347 sum_bar("summary for $dev",@dev_t);
348 $dev = $row->{device};
349 @dev_t = ();
350 } elsif (! $dev) {
351 $dev = $row->{device};
352 }
353 }
354
355 my $start_t = str2time($row->{start});
356 my $fin_t = str2time($row->{finish});
357
358 # Can I squeeze here 1 pixel of time (many seconds) ?
359 if ($start_t > $curr_t + ($len_t / $width)) {
360 my $t = $start_t - $curr_t;
361 print STDERR "[middle filler $curr_t:$t]" if ($debug);
362 # print bar($t);
363 $curr_t = $start_t;
364 } else {
365 # prepend too few seconds to next event
366 $start_t = $curr_t;
367 }
368
369 my $len = $fin_t - $start_t;
370 # $len = $len_t if ($len > $len_t);
371 my $less = '';
372 my $more = '';
373
374 if ($start_t < $from_t) {
375 # $len += ($from_t - $start_t);
376 $len = ($fin_t - $curr_t);
377 $less = "<<";
378 }
379
380 if ($fin_t > $to_t) {
381 # $len -= ($fin_t - $to_t);
382 $len = ($to_t - $curr_t);
383 $more = ">>"; # event now shown whole
384 }
385
386 print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug);
387
388 my $alt = $row->{start}." - ".$row->{finish}."<br>";
389 $alt =~ s/:\d\d\.\d+//g;
390 $alt =~ s/\s+/&nbsp;/g;
391 $alt .= $row->{specification}."<br>".
392 $row->{type}." <b>".$row->{status}."</b><br>".
393 $row->{user_group_host}." <i>".$row->{sessionid}."</i><br>".
394 $row->{device}."&nbsp;on&nbsp;".$row->{host};
395 # print bar($len,$row->{status},$alt);
396
397 push @line_arr,$row->{status}."\t".$alt;
398 # store use of devices for each minute
399 for (my $s=0; $s<$len; $s++) {
400 $line_t[$curr_t-$from_t+$s] = $#line_arr;
401 $dev_t[$curr_t-$from_t+$s]++;
402 $dev_sum_t[$curr_t-$from_t+$s]++;
403 }
404
405 $curr_t += $len;
406
407 # print Dumper($row);
408
409 }
410
411 if ($curr_t == $from_t) { # no entries in database!
412 # print "<tr><td></td><td colspan=2>";
413 }
414
415 if ($curr_t < $to_t ) {
416 my $t = $to_t - $curr_t;
417 print STDERR "[last_line filler $curr_t:$t]" if ($debug);
418 }
419
420 undef $sth;
421 $dbh->disconnect;
422
423 html_spec();
424 sum_bar("summary for $dev",@dev_t);
425 hour_grid();
426 sum_bar("summary for all devices",@dev_sum_t);
427 print "</table>";
428
429
430 # label and usage
431 $len_t = 50; # disable bar scaling
432
433 print "<hr>\nColors for statuses and usage (#):\n";
434 print "<table border=0><tr bgcolor=#e0e0e0><th>status</th><th>#</th><th>color</th></tr>\n";
435 foreach my $status (keys %count) {
436 #foreach my $status (keys %cols) {
437 print "<tr bgcolor=#e0e0e0><td><small>$status</small></td><td><small>",$count{$status}*1,"</small></td><td>",bar($count{$status},$status),"</td></tr>\n" if ($status ne "");
438 # *1 in line above is a cludge to display correct number on
439 # occurences on graph and without one on legend! If you remove * op
440 # it will first evaluate bar sub (thus increasing number by one) and
441 # then display number (wrongly).
442 }
443 print "</table></form>\n<p>Reload <a href=\"",$q->url(-relative=>1),"\">current</a> or see <a href=\"db2gantt_help.html\">help</a>.</p></body></html>";
444

  ViewVC Help
Powered by ViewVC 1.1.26