/[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.11 - (hide annotations)
Fri Sep 13 18:35:50 2002 UTC (17 years, 1 month ago) by dpavlin
Branch: MAIN
Changes since 1.10: +16 -10 lines
dual hour grid

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.6 my $debug = 0;
12    
13     my $width = 600; # width of bar
14     my $height = 12; # height of bar
15     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.4 'In Progress/Failure' => '255,64,255',
21     'In Progress/Errors' => '255,64,0',
22 dpavlin 1.3 'Queuing' => '255,255,0',
23 dpavlin 1.4 'Aborted' => '255,0,0',
24     'Failed' => '255,0,0',
25     'Completed' => '64,255,64',
26     'Completed/Errors' => '255,128,0',
27     'Completed/Failure' => '128,0,128',
28     'Mount Request' => '128,128,255',
29 dpavlin 1.9 'Mount/Errors' => '255,64,128',
30 dpavlin 1.3 );
31    
32 dpavlin 1.6 my $int_t = (12 * 60 * 60); # interval to display on one screen
33     my $min_l = 3; # min length of bar segment (in pixels)
34    
35 dpavlin 1.4 #--- no user servicable parts below this line
36    
37     # time range
38    
39 dpavlin 1.9 $int_t = m_round($int_t);
40    
41 dpavlin 1.6 my ($from_t,$to_t) = (time()-$int_t,time());
42 dpavlin 1.4
43     $to_t = param('to_t') if (param('to_t'));
44     $from_t = param('from_t') if (param('from_t'));
45    
46 dpavlin 1.6 # round to nearest minute
47 dpavlin 1.9 sub m_round {
48     my $t = shift @_;
49     return ($t - ($t % 60));
50     }
51    
52     $to_t = m_round($to_t);
53     $from_t = m_round($from_t);
54 dpavlin 1.6
55     my $to=strftime("%Y-%m-%d %H:%M",localtime ($to_t));
56     my $from=strftime("%Y-%m-%d %H:%M",localtime ($from_t));
57 dpavlin 1.4
58    
59     # keep count of each status
60     my %count;
61 dpavlin 1.3
62     if (param('pic')) {
63 dpavlin 1.4 print "Content-type: image/png\nCache-Control: max-age=3600, must-revalidate\n\n";
64 dpavlin 1.3 # create picture using GD
65     use GD;
66 dpavlin 1.6 my $im = new GD::Image(1,$height);
67 dpavlin 1.3 my $back = $im->colorAllocate(255,255,255);
68     $im->transparent($back);
69     my ($r,$g,$b) = split(/,/,param('pic'));
70     my $col = $im->colorAllocate($r,$g,$b);
71     $im->fill(0,0,$col);
72     binmode STDOUT;
73     print $im->png;
74     exit;
75     }
76 dpavlin 1.1
77 dpavlin 1.4 print "Content-type: text/html
78    
79     <html>
80     <head>
81     <title>OmniBack Gantt: $from - $to</title>";
82     if ($use_js) {
83     print '
84     <script type="text/javascript" language="javascript" src="1k.js"></script>
85     <script type="text/javascript" language="javascript" src="tooltip.js"></script>
86     <script language="javascript" type="text/javascript">
87     onload=function(){
88     T.init()
89     T.follows = false // false by default
90     T.delay = .3 // any nonnegative value (0.7 by default)
91     }
92     </script>
93     ';
94     }
95     print "</head><body>";
96 dpavlin 1.1
97     # all vars ending in *_t have utime in them.
98     #
99     my $len_t = $to_t - $from_t;
100    
101     die "interval must be positive and bigger than 1 sec !" if ($len_t < 1);
102    
103     my $dbh = DBI->connect("DBI:Pg:dbname=gantt","","") || die $DBI::errstr;
104 dpavlin 1.3 my $q=new CGI;
105 dpavlin 1.1
106    
107 dpavlin 1.4 sub mknav {
108     my $f = shift @_; # from_t
109     my $t = shift @_; # to_t
110     my $ch = shift @_; # char
111    
112     return "<a href=\"".$q->url(-relative=>1)."?from_t=${f}&to_t=${t}\">$ch</a>";
113     }
114    
115 dpavlin 1.1 print "<table>";
116 dpavlin 1.4 print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>";
117 dpavlin 1.6 print mknav(($from_t-$int_t),$to_t,'<small>&lt;&lt;</small>'),$from;
118     print mknav(($from_t+$int_t),$to_t,'<small>&gt;&gt;</small>') if ($from_t+$int_t < $to_t);
119 dpavlin 1.4 print "</td><td align=right>";
120 dpavlin 1.6 print mknav($from_t,($to_t-$int_t),'<small>&lt;&lt;</small>') if ($to_t-$int_t > $from_t);
121     print $to,mknav($from_t,($to_t+$int_t),'<small>&gt;&gt;</small>'),"</td></tr>\n";
122 dpavlin 1.4
123 dpavlin 1.10 # draw hour grid
124 dpavlin 1.11 sub hour_grid {
125     print "<tr><td align=right><small>hour grid</small></td><td colspan=2>";
126     my @c = ("255,255,128","255,192,128");
127     my $hr=strftime("%H",localtime ($from_t));
128     print color_bar(3600 - $from_t % 3600,$c[0],sprintf("%02d",$hr++ % 24));
129     for (my $i=1; $i<int(($to_t-$from_t)/3600); $i++) {
130     print color_bar(3600,$c[1],sprintf("%02d",$hr++ % 24));
131     push @c, shift @c;
132     }
133     print color_bar($from_t % 3600,$c[1],sprintf("%02d",$hr % 24));
134     print "</td></tr>\n";
135 dpavlin 1.10 }
136 dpavlin 1.11
137     hour_grid();
138 dpavlin 1.10
139 dpavlin 1.4 my $fix_d = 0; # used to fix graph len
140 dpavlin 1.3
141 dpavlin 1.4 sub bar {
142 dpavlin 1.10 my $l = shift @_;
143     my $status = shift @_;
144     my $alt = shift @_;
145    
146     my $col;
147    
148     if ($status) {
149     if ($cols{$status}) {
150     $col .= $cols{$status};
151     } else {
152     $col .= "0,0,0"; # unknown status, black
153     }
154     $count{$status}++;
155     } else {
156     # $col .= '240,240,240';
157     $col .= '220,220,220';
158     }
159     return color_bar($l,$col,$alt);
160     }
161    
162     sub color_bar {
163     my $l = shift @_; # lenght of event utime
164     my $col = shift @_ || '240,240,240'; # default color (filler)
165 dpavlin 1.4 my $alt = shift @_ || undef;
166 dpavlin 1.1
167 dpavlin 1.4 my $size = int($l / ($len_t / $width));
168     if ($size < $min_l) {
169 dpavlin 1.8 $fix_d += ($min_l - $size);
170     print STDERR "fix_d: $fix_d\n" if ($debug);
171 dpavlin 1.4 $size = $min_l;
172     }
173     if ($fix_d && $size > $fix_d+$min_l) {
174     $size -= $fix_d;
175     $fix_d = 0;
176 dpavlin 1.8 print STDERR "fix_d: $fix_d\n" if ($debug);
177 dpavlin 1.4 }
178 dpavlin 1.1
179 dpavlin 1.10 print STDERR "bar[$col] len:$l s scale:",($len_t/$width)," size:$size px<br> alt:$alt\n" if ($debug);
180 dpavlin 1.1
181 dpavlin 1.10 my $html = "<img src=\"".$q->url(-relative=>1)."?pic=$col\" width=\"$size\" height=\"$height\"";
182 dpavlin 1.1
183 dpavlin 1.4 if ($use_js && $alt) {
184     $html .= " onmouseover=\"T('$alt')\" onmouseout=\"T()\"";
185     } elsif ($alt) {
186     $html .= " alt=\"$alt\"";
187     }
188     $html .= ">";
189    
190     return($html);
191 dpavlin 1.1 }
192    
193 dpavlin 1.6 my $sql = "select start,finish,specification,status,user_group_host,
194     type,sessionid,device,host
195 dpavlin 1.1 from gantt
196     where (start < '$from' and finish > '$from') or
197     (start > '$from' and start < '$to')
198 dpavlin 1.6 order by device,specification
199 dpavlin 1.1 ";
200    
201     my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr;
202    
203     my %spec; # specification hash
204    
205     my $curr_spec;
206     my $curr_t = $from_t;
207    
208     $sth->execute() || die "sql: $sql ".$dbh->errstr;
209 dpavlin 1.2
210 dpavlin 1.1 while(my $row = $sth->fetchrow_hashref) {
211 dpavlin 1.6 if (!defined $curr_spec || $row->{specification} ne $curr_spec) {
212 dpavlin 1.1
213 dpavlin 1.3 if ($curr_t < $to_t && $curr_spec) {
214 dpavlin 1.1 my $t = $to_t - $curr_t;
215 dpavlin 1.6 print STDERR "[end filler $curr_t:$t]" if ($debug);
216 dpavlin 1.4 print bar($t);
217 dpavlin 1.1 }
218    
219 dpavlin 1.6 print "</td></tr>\n" if ($curr_t != $from_t);
220 dpavlin 1.1 print "<tr><td>", $row->{specification},"</td><td colspan=2>";
221    
222     $curr_t = $from_t; # init timeline
223     $curr_spec = $row->{specification};
224    
225     }
226    
227 dpavlin 1.9 my $start_t = m_round(str2time($row->{start}));
228     my $fin_t = m_round(str2time($row->{finish}));
229 dpavlin 1.1
230 dpavlin 1.9 if ($start_t > $curr_t + 60) {
231 dpavlin 1.1 my $t = $start_t - $curr_t;
232 dpavlin 1.6 print STDERR "[middle filler $curr_t:$t]" if ($debug);
233 dpavlin 1.4 print bar($t);
234 dpavlin 1.1 $curr_t = $start_t;
235     }
236    
237     my $len = $fin_t - $start_t;
238     # $len = $len_t if ($len > $len_t);
239     my $less = '';
240     my $more = '';
241    
242     if ($start_t < $from_t) {
243     # $len += ($from_t - $start_t);
244     $len = ($fin_t - $curr_t);
245     $less = "<<";
246     }
247 dpavlin 1.4
248 dpavlin 1.1 if ($fin_t > $to_t) {
249     # $len -= ($fin_t - $to_t);
250     $len = ($to_t - $curr_t);
251     $more = ">>"; # event now shown whole
252     }
253    
254     print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug);
255 dpavlin 1.5
256 dpavlin 1.6 my $alt = $row->{start}." - ".$row->{finish}."<br>";
257 dpavlin 1.5 $alt =~ s/:\d\d\.\d+//g;
258 dpavlin 1.6 $alt =~ s/\s+/&nbsp;/g;
259     $alt .= $row->{type}." <b>".$row->{status}."</b><br>".
260     $row->{user_group_host}." <i>".$row->{sessionid}."</i><br>".
261     $row->{device}."&nbsp;on&nbsp;".$row->{host};
262 dpavlin 1.5 print bar($len,$row->{status},$alt);
263 dpavlin 1.1
264     $curr_t += $len;
265    
266     # print Dumper($row);
267    
268     }
269    
270 dpavlin 1.6 if ($curr_t == $from_t) { # no entries in database!
271     print "<tr><td></td><td colspan=2>";
272     }
273    
274 dpavlin 1.1 if ($curr_t < $to_t ) {
275     my $t = $to_t - $curr_t;
276 dpavlin 1.6 print STDERR "[last_line filler $curr_t:$t]" if ($debug);
277 dpavlin 1.4 print bar($t);
278 dpavlin 1.1 }
279    
280     undef $sth;
281     $dbh->disconnect;
282    
283 dpavlin 1.11 print "</td></tr>\n";
284     hour_grid();
285     print "</table>";
286 dpavlin 1.4
287    
288     # label and usage
289     $len_t = 50; # disable bar scaling
290    
291     print "<hr>\nColors for statuses and usage (#):\n";
292     print "<table border=0><tr bgcolor=#e0e0e0><th>status</th><th>#</th><th>color</th></tr>\n";
293     foreach my $status (keys %count) {
294     #foreach my $status (keys %cols) {
295 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 "");
296     # *1 in line above is a cludge to display correct number on
297     # occurences on graph and without one on legend! If you remove * op
298     # it will first evaluate bar sub (thus increasing number by one) and
299     # then display number (wrongly).
300 dpavlin 1.4 }
301 dpavlin 1.7 print "</table>\n<p>Reload <a href=\"",$q->url(-relative=>1),"\">current</a>.</p></body></html>";
302 dpavlin 1.1

  ViewVC Help
Powered by ViewVC 1.1.26