/[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.28 - (hide annotations)
Thu Jun 26 20:36:48 2003 UTC (20 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.27: +15 -2 lines
add links to repgen.pl generated html pages

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