5 |
use Data::Dumper; |
use Data::Dumper; |
6 |
use Date::Parse; |
use Date::Parse; |
7 |
use CGI qw/:standard/; |
use CGI qw/:standard/; |
8 |
|
use CGI::Carp qw(fatalsToBrowser); |
9 |
# default range |
use POSIX qw(strftime); |
|
my $from="2002-09-11 18:00"; |
|
|
my $to="2002-09-13 00:00:00"; |
|
10 |
|
|
11 |
my $debug = 1; |
my $debug = 1; |
|
my $width = 800; |
|
12 |
|
|
13 |
#--- no user servicable parts below this line |
my $width = 600; # width of bar |
14 |
|
my $height = 19; # height of bar |
15 |
|
my $use_js = 1; # use JavaScript pop-up |
16 |
|
|
17 |
|
# status colors |
18 |
my %cols = ( |
my %cols = ( |
19 |
'In Progress' => '0,255,0', |
'In Progress' => '0,255,0', |
20 |
'In Progress/Failure' => '255,128,128', |
'In Progress/Failures' => '192,64,192', |
21 |
'In Progress/Errors' => '255,128,128', |
'In Progress/Errors' => '255,128,128', |
22 |
'Queuing' => '255,255,0', |
'Queuing' => '255,255,0', |
23 |
'Aborted', => '255,0,0', |
'Aborted' => '255,0,0', |
24 |
'Failed', => '255,0,0', |
'Failed' => '255,0,0', |
25 |
'Completed', => '0,255,0', |
'Completed' => '64,255,64', |
26 |
'Completed/Errors' => '0,255,255', |
'Completed/Errors' => '255,128,0', |
27 |
'Completed/Failure', => '0,255,255' |
'Completed/Failure' => '128,0,128', |
28 |
|
'Mount Request' => '128,128,255', |
29 |
|
'Mount/Errors' => '255,64,128', |
30 |
); |
); |
31 |
|
|
32 |
|
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 |
|
#--- no user servicable parts below this line |
36 |
|
|
37 |
|
# time range |
38 |
|
|
39 |
|
$int_t = m_round($int_t); |
40 |
|
|
41 |
|
my ($from_t,$to_t) = (time()-$int_t,time()); |
42 |
|
|
43 |
|
$to_t = param('to_t') if (param('to_t')); |
44 |
|
$from_t = param('from_t') if (param('from_t')); |
45 |
|
|
46 |
|
# round to nearest minute |
47 |
|
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 |
|
|
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 |
|
|
58 |
|
|
59 |
|
# keep count of each status |
60 |
|
my %count; |
61 |
|
|
62 |
if (param('pic')) { |
if (param('pic')) { |
63 |
print "Content-type: image/png\n\n"; |
print "Content-type: image/png\nCache-Control: max-age=3600, must-revalidate\n\n"; |
64 |
# create picture using GD |
# create picture using GD |
65 |
use GD; |
use GD; |
66 |
my $im = new GD::Image(1,8); |
my $im = new GD::Image(1,$height); |
67 |
my $back = $im->colorAllocate(255,255,255); |
my $back = $im->colorAllocate(255,255,255); |
68 |
$im->transparent($back); |
$im->transparent($back); |
69 |
my ($r,$g,$b) = split(/,/,param('pic')); |
my ($r,$g,$b) = split(/,/,param('pic')); |
74 |
exit; |
exit; |
75 |
} |
} |
76 |
|
|
77 |
print "Content-type: text/html\n\n"; |
print "Content-type: text/html |
78 |
|
Cache-Control: max-age=60, must-revalidate |
79 |
|
|
80 |
|
<html> |
81 |
|
<head> |
82 |
|
<title>OmniBack Gantt: $from - $to</title> |
83 |
|
<meta HTTP-EQUIV=\"Refresh\" CONTENT=60>"; |
84 |
|
if ($use_js) { |
85 |
|
print ' |
86 |
|
<script type="text/javascript" language="javascript" src="1k.js"></script> |
87 |
|
<script type="text/javascript" language="javascript" src="tooltip.js"></script> |
88 |
|
<script language="javascript" type="text/javascript"> |
89 |
|
onload=function(){ |
90 |
|
T.init() |
91 |
|
T.follows = false // false by default |
92 |
|
T.delay = .3 // any nonnegative value (0.7 by default) |
93 |
|
} |
94 |
|
</script> |
95 |
|
'; |
96 |
|
} |
97 |
|
print "</head><body>"; |
98 |
|
|
99 |
# all vars ending in *_t have utime in them. |
# all vars ending in *_t have utime in them. |
100 |
# |
# |
|
my $from_t = str2time($from); |
|
|
my $to_t = str2time($to); |
|
101 |
my $len_t = $to_t - $from_t; |
my $len_t = $to_t - $from_t; |
102 |
|
|
103 |
die "interval must be positive and bigger than 1 sec !" if ($len_t < 1); |
die "interval must be positive and bigger than 1 sec !" if ($len_t < 1); |
106 |
my $q=new CGI; |
my $q=new CGI; |
107 |
|
|
108 |
|
|
109 |
print "<table>"; |
sub mknav { |
110 |
print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>$from</td><td align=right>$to</td></tr>\n"; |
my $f = shift @_; # from_t |
111 |
|
my $t = shift @_; # to_t |
112 |
|
my $ch = shift @_; # char |
113 |
|
|
114 |
|
return "<a href=\"".$q->url(-relative=>1)."?from_t=${f}&to_t=${t}\">$ch</a>"; |
115 |
|
} |
116 |
|
|
117 |
|
print "<table cellspacing=0 cellpadding=0>"; |
118 |
|
print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>"; |
119 |
|
print mknav(($from_t-$int_t),$to_t,'<small><<</small>'),$from; |
120 |
|
print mknav(($from_t+$int_t),$to_t,'<small>>></small>') if ($from_t+$int_t < $to_t); |
121 |
|
print "</td><td align=right>"; |
122 |
|
print mknav($from_t,($to_t-$int_t),'<small><<</small>') if ($to_t-$int_t > $from_t); |
123 |
|
print $to,mknav($from_t,($to_t+$int_t),'<small>>></small>'),"</td></tr>\n"; |
124 |
|
|
125 |
|
# draw hour grid |
126 |
|
sub hour_grid { |
127 |
|
print "<tr><td align=right><small>hour grid</small></td><td colspan=2>"; |
128 |
|
my @c = ("255,255,128","255,192,128"); |
129 |
|
my $hr=strftime("%H",localtime ($from_t)); |
130 |
|
sub hour_bar { |
131 |
|
my ($t,$c,$hr) = @_; |
132 |
|
my $clock_hr = $hr % 24; |
133 |
|
my $alt = sprintf("%02d:00",$clock_hr); |
134 |
|
if ($clock_hr == 0) { |
135 |
|
my $pix_hr = int(1 / $width); |
136 |
|
print color_bar($pix_hr,"0,0,0",strftime("%Y-%m-%d", localtime($from_t + $hr*3600))); |
137 |
|
print color_bar($t-$pix_hr,$c,$alt); |
138 |
|
} else { |
139 |
|
print color_bar($t,$c,$alt); |
140 |
|
} |
141 |
|
} |
142 |
|
hour_bar(3600 - $from_t % 3600,$c[0],$hr++); |
143 |
|
for (my $i=1; $i<int(($to_t-$from_t)/3600); $i++) { |
144 |
|
hour_bar(3600,$c[1],$hr++); |
145 |
|
push @c, shift @c; |
146 |
|
} |
147 |
|
hour_bar($from_t % 3600,$c[1],$hr); |
148 |
|
print "</td></tr>\n"; |
149 |
|
} |
150 |
|
|
151 |
|
hour_grid(); |
152 |
|
|
153 |
|
my $fix_d = 0; # used to fix graph len (in pixels) |
154 |
|
my $fix_s = 0; # used to collect round errors of size |
155 |
|
|
156 |
sub draw { |
sub bar { |
157 |
my $l = shift @_; # lenght of event utime |
my $l = shift @_; |
158 |
my $status = shift @_; # what to draw |
my $status = shift @_; |
159 |
my $alt = shift @_; |
my $alt = shift @_; |
160 |
|
|
161 |
my $size = int($l / ($len_t / $width)) || 1; # dump size (min. size=1) |
my $col; |
162 |
|
|
163 |
|
if ($status) { |
164 |
|
if ($cols{$status}) { |
165 |
|
$col .= $cols{$status}; |
166 |
|
} else { |
167 |
|
$col .= "0,0,0"; # unknown status, black |
168 |
|
} |
169 |
|
$count{$status}++; |
170 |
|
} else { |
171 |
|
# $col .= '240,240,240'; |
172 |
|
$col .= '220,220,220'; |
173 |
|
} |
174 |
|
return color_bar($l,$col,$alt,$min_l); |
175 |
|
} |
176 |
|
|
177 |
|
sub color_bar { |
178 |
|
my $l = shift @_; # lenght of event utime |
179 |
|
my $col = shift @_ || '240,240,240'; # default color (filler) |
180 |
|
my $alt = shift @_ || undef; |
181 |
|
my $min_l = shift @_ || 1; |
182 |
|
|
183 |
|
my $size = $l / ($len_t / $width); |
184 |
|
$fix_s += $size - int($size); |
185 |
|
$size=int($size); |
186 |
|
# add rounding error to size |
187 |
|
if ($fix_s > 1) { |
188 |
|
my $i = $fix_s ; $i = int($i); |
189 |
|
$fix_s -= $i; |
190 |
|
$size += $i; |
191 |
|
} |
192 |
|
|
193 |
|
if ($alt && $size < $min_l) { |
194 |
|
$fix_d += ($min_l - $size); |
195 |
|
print STDERR "fix_d: $fix_d\n" if ($debug); |
196 |
|
$size = $min_l; |
197 |
|
} |
198 |
|
if ($fix_d && $size > $fix_d+$min_l) { |
199 |
|
$size -= $fix_d; |
200 |
|
$fix_d = 0; |
201 |
|
print STDERR "fix_d: $fix_d\n" if ($debug); |
202 |
|
} |
203 |
|
|
204 |
|
print STDERR "bar[$col] len:$l s scale:",($len_t/$width)," size:$size px<br> alt:$alt\n" if ($debug); |
205 |
|
|
206 |
print STDERR "l[$status]:$l scale:",($len_t/$width)," size:$size<br>\n" if ($debug); |
my $html = "<img src=\"".$q->url(-relative=>1)."?pic=$col\" width=\"$size\" height=\"$height\""; |
207 |
|
|
208 |
|
if ($use_js && $alt) { |
209 |
|
$html .= " onmouseover=\"T('$alt')\" onmouseout=\"T()\""; |
210 |
|
} elsif ($alt) { |
211 |
|
$html .= " alt=\"$alt\""; |
212 |
|
} |
213 |
|
$html .= ">"; |
214 |
|
|
215 |
my $col = $cols{$status} || '240,240,240'; |
return($html); |
|
print "<img src=".$q->url(-relative=>1)."?pic=$col width=$size height=8 alt=\"$alt\">"; |
|
216 |
} |
} |
217 |
|
|
218 |
my $sql = "select start,finish,specification,status |
my $sql = "select start,finish,specification,status,user_group_host, |
219 |
|
type,sessionid,device,host |
220 |
from gantt |
from gantt |
221 |
where (start < '$from' and finish > '$from') or |
where (start < '$from' and finish > '$from') or |
222 |
(start > '$from' and start < '$to') |
(start > '$from' and start < '$to') |
223 |
order by specification |
order by device,specification,start |
224 |
"; |
" |
225 |
|
|
226 |
my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr; |
my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr; |
227 |
|
|
233 |
$sth->execute() || die "sql: $sql ".$dbh->errstr; |
$sth->execute() || die "sql: $sql ".$dbh->errstr; |
234 |
|
|
235 |
while(my $row = $sth->fetchrow_hashref) { |
while(my $row = $sth->fetchrow_hashref) { |
236 |
if ($row->{specification} ne $curr_spec) { |
if (!defined $curr_spec || $row->{specification} ne $curr_spec) { |
237 |
|
|
238 |
if ($curr_t < $to_t && $curr_spec) { |
if ($curr_t < $to_t && $curr_spec) { |
239 |
my $t = $to_t - $curr_t; |
my $t = $to_t - $curr_t; |
240 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
print STDERR "[end filler $curr_t:$t]" if ($debug); |
241 |
draw($t,undef,$row->{start}." - ".$row->{finish}." ".$row->{status}); |
print bar($t); |
242 |
} |
} |
243 |
|
|
244 |
print "</td></tr>\n" if ($curr_t != 0); |
print "</td></tr>\n" if ($curr_t != $from_t); |
245 |
|
($fix_s,$fix_d) = (0,0); # init fix vars for bar |
246 |
print "<tr><td>", $row->{specification},"</td><td colspan=2>"; |
print "<tr><td>", $row->{specification},"</td><td colspan=2>"; |
247 |
|
|
248 |
$curr_t = $from_t; # init timeline |
$curr_t = $from_t; # init timeline |
253 |
my $start_t = str2time($row->{start}); |
my $start_t = str2time($row->{start}); |
254 |
my $fin_t = str2time($row->{finish}); |
my $fin_t = str2time($row->{finish}); |
255 |
|
|
256 |
if ($start_t > $curr_t) { |
# Can I squeeze here 1 pixel of time (many seconds) ? |
257 |
|
if ($start_t > $curr_t + ($len_t / $width)) { |
258 |
my $t = $start_t - $curr_t; |
my $t = $start_t - $curr_t; |
259 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
print STDERR "[middle filler $curr_t:$t]" if ($debug); |
260 |
draw($t,undef,$row->{start}." - ".$row->{finish}." ".$row->{status}); |
print bar($t); |
261 |
$curr_t = $start_t; |
$curr_t = $start_t; |
262 |
|
} else { |
263 |
|
# prepend too few seconds to next event |
264 |
|
$start_t = $curr_t; |
265 |
} |
} |
266 |
|
|
267 |
my $len = $fin_t - $start_t; |
my $len = $fin_t - $start_t; |
274 |
$len = ($fin_t - $curr_t); |
$len = ($fin_t - $curr_t); |
275 |
$less = "<<"; |
$less = "<<"; |
276 |
} |
} |
277 |
|
|
278 |
if ($fin_t > $to_t) { |
if ($fin_t > $to_t) { |
279 |
# $len -= ($fin_t - $to_t); |
# $len -= ($fin_t - $to_t); |
280 |
$len = ($to_t - $curr_t); |
$len = ($to_t - $curr_t); |
282 |
} |
} |
283 |
|
|
284 |
print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug); |
print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug); |
285 |
draw($len,$row->{status},$row->{start}." - ".$row->{finish}." ".$row->{status}); |
|
286 |
|
my $alt = $row->{start}." - ".$row->{finish}."<br>"; |
287 |
|
$alt =~ s/:\d\d\.\d+//g; |
288 |
|
$alt =~ s/\s+/ /g; |
289 |
|
$alt .= $row->{type}." <b>".$row->{status}."</b><br>". |
290 |
|
$row->{user_group_host}." <i>".$row->{sessionid}."</i><br>". |
291 |
|
$row->{device}." on ".$row->{host}; |
292 |
|
$alt .= "-- ".$row->{specification}; |
293 |
|
print bar($len,$row->{status},$alt); |
294 |
|
|
295 |
$curr_t += $len; |
$curr_t += $len; |
296 |
|
|
298 |
|
|
299 |
} |
} |
300 |
|
|
301 |
|
if ($curr_t == $from_t) { # no entries in database! |
302 |
|
print "<tr><td></td><td colspan=2>"; |
303 |
|
} |
304 |
|
|
305 |
if ($curr_t < $to_t ) { |
if ($curr_t < $to_t ) { |
306 |
my $t = $to_t - $curr_t; |
my $t = $to_t - $curr_t; |
307 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
print STDERR "[last_line filler $curr_t:$t]" if ($debug); |
308 |
draw($t); |
print bar($t); |
309 |
} |
} |
310 |
|
|
311 |
undef $sth; |
undef $sth; |
312 |
$dbh->disconnect; |
$dbh->disconnect; |
313 |
|
|
314 |
print "</td></tr>\n</table>"; |
print "</td></tr>\n"; |
315 |
|
hour_grid(); |
316 |
|
print "</table>"; |
317 |
|
|
318 |
|
|
319 |
|
# label and usage |
320 |
|
$len_t = 50; # disable bar scaling |
321 |
|
|
322 |
|
print "<hr>\nColors for statuses and usage (#):\n"; |
323 |
|
print "<table border=0><tr bgcolor=#e0e0e0><th>status</th><th>#</th><th>color</th></tr>\n"; |
324 |
|
foreach my $status (keys %count) { |
325 |
|
#foreach my $status (keys %cols) { |
326 |
|
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 ""); |
327 |
|
# *1 in line above is a cludge to display correct number on |
328 |
|
# occurences on graph and without one on legend! If you remove * op |
329 |
|
# it will first evaluate bar sub (thus increasing number by one) and |
330 |
|
# then display number (wrongly). |
331 |
|
} |
332 |
|
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>"; |
333 |
|
|