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