/[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.23 - (hide annotations)
Mon Sep 23 08:45:36 2002 UTC (16 years, 8 months ago) by dpavlin
Branch: MAIN
Changes since 1.22: +1 -0 lines
added new type

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

  ViewVC Help
Powered by ViewVC 1.1.26