/[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

Annotation of /db2gantt.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations)
Sun Mar 9 18:57:07 2003 UTC (21 years ago) by dpavlin
Branch: MAIN
Changes since 1.26: +2 -0 lines
fix division by zero

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

  ViewVC Help
Powered by ViewVC 1.1.26