4 |
use DBI; |
use DBI; |
5 |
use Data::Dumper; |
use Data::Dumper; |
6 |
use Date::Parse; |
use Date::Parse; |
7 |
|
use CGI qw/:standard/; |
8 |
|
use CGI::Carp qw(fatalsToBrowser); |
9 |
|
use POSIX qw(strftime); |
10 |
|
|
11 |
|
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 |
|
|
17 |
|
# status colors |
18 |
|
my %cols = ( |
19 |
|
'In Progress' => '0,255,0', |
20 |
|
'In Progress/Failure' => '255,64,255', |
21 |
|
'In Progress/Errors' => '255,64,0', |
22 |
|
'Queuing' => '255,255,0', |
23 |
|
'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 |
|
'Mount/Errors' => '255,64,128', |
30 |
|
); |
31 |
|
|
32 |
print "Content-type: text/html\n\n"; |
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 |
my $from="2002-09-11 20:00:00"; |
|
59 |
my $to="2002-09-13 00:00:00"; |
# keep count of each status |
60 |
|
my %count; |
61 |
|
|
62 |
|
if (param('pic')) { |
63 |
|
print "Content-type: image/png\nCache-Control: max-age=3600, must-revalidate\n\n"; |
64 |
|
# create picture using GD |
65 |
|
use GD; |
66 |
|
my $im = new GD::Image(1,$height); |
67 |
|
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 |
|
|
77 |
|
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 |
|
|
97 |
# all vars ending in *_t have utime in them. |
# all vars ending in *_t have utime in them. |
98 |
# |
# |
|
my $from_t = str2time($from); |
|
|
my $to_t = str2time($to); |
|
99 |
my $len_t = $to_t - $from_t; |
my $len_t = $to_t - $from_t; |
100 |
|
|
101 |
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); |
102 |
|
|
|
my $debug = 1; |
|
|
|
|
103 |
my $dbh = DBI->connect("DBI:Pg:dbname=gantt","","") || die $DBI::errstr; |
my $dbh = DBI->connect("DBI:Pg:dbname=gantt","","") || die $DBI::errstr; |
104 |
|
my $q=new CGI; |
105 |
|
|
|
#--- no user servicable parts below this line |
|
106 |
|
|
107 |
print "<table>"; |
sub mknav { |
108 |
print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>$from</td><td align=right>$to</td></tr>"; |
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 |
|
print "<table cellspacing=0 cellpadding=0>"; |
116 |
|
print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>"; |
117 |
|
print mknav(($from_t-$int_t),$to_t,'<small><<</small>'),$from; |
118 |
|
print mknav(($from_t+$int_t),$to_t,'<small>>></small>') if ($from_t+$int_t < $to_t); |
119 |
|
print "</td><td align=right>"; |
120 |
|
print mknav($from_t,($to_t-$int_t),'<small><<</small>') if ($to_t-$int_t > $from_t); |
121 |
|
print $to,mknav($from_t,($to_t+$int_t),'<small>>></small>'),"</td></tr>\n"; |
122 |
|
|
123 |
|
# draw hour grid |
124 |
|
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 |
|
} |
136 |
|
|
137 |
|
hour_grid(); |
138 |
|
|
139 |
my $width = 900; |
my $fix_d = 0; # used to fix graph len (in pixels) |
140 |
|
my $fix_s = 0; # used to collect round errors of size |
141 |
|
|
142 |
sub draw { |
sub bar { |
143 |
my $l = shift @_; # lenght of event utime |
my $l = shift @_; |
144 |
my $type = shift @_; # what to draw |
my $status = shift @_; |
145 |
my $alt = shift @_; |
my $alt = shift @_; |
146 |
|
|
147 |
my $size = int($l / ($len_t / $width)) || 1; # dump size (min. size=1) |
my $col; |
148 |
|
|
149 |
|
if ($status) { |
150 |
|
if ($cols{$status}) { |
151 |
|
$col .= $cols{$status}; |
152 |
|
} else { |
153 |
|
$col .= "0,0,0"; # unknown status, black |
154 |
|
} |
155 |
|
$count{$status}++; |
156 |
|
} else { |
157 |
|
# $col .= '240,240,240'; |
158 |
|
$col .= '220,220,220'; |
159 |
|
} |
160 |
|
return color_bar($l,$col,$alt); |
161 |
|
} |
162 |
|
|
163 |
|
sub color_bar { |
164 |
|
my $l = shift @_; # lenght of event utime |
165 |
|
my $col = shift @_ || '240,240,240'; # default color (filler) |
166 |
|
my $alt = shift @_ || undef; |
167 |
|
|
168 |
|
my $size = $l / ($len_t / $width); |
169 |
|
$fix_s += $size - int($size); |
170 |
|
$size=int($size); |
171 |
|
if ($fix_s > 1) { |
172 |
|
$fix_s -= 1; |
173 |
|
$size++; |
174 |
|
} |
175 |
|
|
176 |
|
if ($alt && $size < $min_l) { |
177 |
|
$fix_d += ($min_l - $size); |
178 |
|
print STDERR "fix_d: $fix_d\n" if ($debug); |
179 |
|
$size = $min_l; |
180 |
|
} |
181 |
|
if ($fix_d && $size > $fix_d+$min_l) { |
182 |
|
$size -= $fix_d; |
183 |
|
$fix_d = 0; |
184 |
|
print STDERR "fix_d: $fix_d\n" if ($debug); |
185 |
|
} |
186 |
|
|
187 |
|
print STDERR "bar[$col] len:$l s scale:",($len_t/$width)," size:$size px<br> alt:$alt\n" if ($debug); |
188 |
|
|
189 |
print STDERR "l[$type]:$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\""; |
190 |
|
|
191 |
|
if ($use_js && $alt) { |
192 |
|
$html .= " onmouseover=\"T('$alt')\" onmouseout=\"T()\""; |
193 |
|
} elsif ($alt) { |
194 |
|
$html .= " alt=\"$alt\""; |
195 |
|
} |
196 |
|
$html .= ">"; |
197 |
|
|
198 |
print "<img src=$type.png width=$size height=8 alt=\"$alt\">"; |
return($html); |
199 |
} |
} |
200 |
|
|
201 |
my $sql = "select start,finish,specification,status |
my $sql = "select start,finish,specification,status,user_group_host, |
202 |
|
type,sessionid,device,host |
203 |
from gantt |
from gantt |
204 |
where (start < '$from' and finish > '$from') or |
where (start < '$from' and finish > '$from') or |
205 |
(start > '$from' and start < '$to') |
(start > '$from' and start < '$to') |
206 |
order by specification |
order by device,specification |
207 |
"; |
"; |
208 |
|
|
209 |
my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr; |
my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr; |
216 |
$sth->execute() || die "sql: $sql ".$dbh->errstr; |
$sth->execute() || die "sql: $sql ".$dbh->errstr; |
217 |
|
|
218 |
while(my $row = $sth->fetchrow_hashref) { |
while(my $row = $sth->fetchrow_hashref) { |
219 |
if ($row->{specification} ne $curr_spec) { |
if (!defined $curr_spec || $row->{specification} ne $curr_spec) { |
220 |
|
|
221 |
if ($curr_t < $to_t ) { |
if ($curr_t < $to_t && $curr_spec) { |
222 |
my $t = $to_t - $curr_t; |
my $t = $to_t - $curr_t; |
223 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
print STDERR "[end filler $curr_t:$t]" if ($debug); |
224 |
draw($t,"gray",$row->{start}." - ".$row->{finish}." ".$row->{status}); |
print bar($t); |
225 |
} |
} |
226 |
|
|
227 |
print "</td></tr>\n" if ($curr_t != 0); |
print "</td></tr>\n" if ($curr_t != $from_t); |
228 |
print "<tr><td>", $row->{specification},"</td><td colspan=2>"; |
print "<tr><td>", $row->{specification},"</td><td colspan=2>"; |
229 |
|
|
230 |
$curr_t = $from_t; # init timeline |
$curr_t = $from_t; # init timeline |
232 |
|
|
233 |
} |
} |
234 |
|
|
235 |
my $start_t = str2time($row->{start}); |
my $start_t = m_round(str2time($row->{start})); |
236 |
my $fin_t = str2time($row->{finish}); |
my $fin_t = m_round(str2time($row->{finish})); |
237 |
|
|
238 |
if ($start_t > $curr_t) { |
if ($start_t > $curr_t + 60) { |
239 |
my $t = $start_t - $curr_t; |
my $t = $start_t - $curr_t; |
240 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
print STDERR "[middle filler $curr_t:$t]" if ($debug); |
241 |
draw($t,"gray",$row->{start}." - ".$row->{finish}." ".$row->{status}); |
print bar($t); |
242 |
$curr_t = $start_t; |
$curr_t = $start_t; |
243 |
} |
} |
244 |
|
|
252 |
$len = ($fin_t - $curr_t); |
$len = ($fin_t - $curr_t); |
253 |
$less = "<<"; |
$less = "<<"; |
254 |
} |
} |
255 |
|
|
256 |
if ($fin_t > $to_t) { |
if ($fin_t > $to_t) { |
257 |
# $len -= ($fin_t - $to_t); |
# $len -= ($fin_t - $to_t); |
258 |
$len = ($to_t - $curr_t); |
$len = ($to_t - $curr_t); |
260 |
} |
} |
261 |
|
|
262 |
print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug); |
print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug); |
263 |
draw($len,"red",$row->{start}." - ".$row->{finish}." ".$row->{status}); |
|
264 |
|
my $alt = $row->{start}." - ".$row->{finish}."<br>"; |
265 |
|
$alt =~ s/:\d\d\.\d+//g; |
266 |
|
$alt =~ s/\s+/ /g; |
267 |
|
$alt .= $row->{type}." <b>".$row->{status}."</b><br>". |
268 |
|
$row->{user_group_host}." <i>".$row->{sessionid}."</i><br>". |
269 |
|
$row->{device}." on ".$row->{host}; |
270 |
|
print bar($len,$row->{status},$alt); |
271 |
|
|
272 |
$curr_t += $len; |
$curr_t += $len; |
273 |
|
|
275 |
|
|
276 |
} |
} |
277 |
|
|
278 |
|
if ($curr_t == $from_t) { # no entries in database! |
279 |
|
print "<tr><td></td><td colspan=2>"; |
280 |
|
} |
281 |
|
|
282 |
if ($curr_t < $to_t ) { |
if ($curr_t < $to_t ) { |
283 |
my $t = $to_t - $curr_t; |
my $t = $to_t - $curr_t; |
284 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
print STDERR "[last_line filler $curr_t:$t]" if ($debug); |
285 |
draw($t,"gray"); |
print bar($t); |
286 |
} |
} |
287 |
|
|
288 |
undef $sth; |
undef $sth; |
289 |
$dbh->disconnect; |
$dbh->disconnect; |
290 |
|
|
291 |
print "</td></tr></table>"; |
print "</td></tr>\n"; |
292 |
|
hour_grid(); |
293 |
|
print "</table>"; |
294 |
|
|
295 |
|
|
296 |
|
# label and usage |
297 |
|
$len_t = 50; # disable bar scaling |
298 |
|
|
299 |
|
print "<hr>\nColors for statuses and usage (#):\n"; |
300 |
|
print "<table border=0><tr bgcolor=#e0e0e0><th>status</th><th>#</th><th>color</th></tr>\n"; |
301 |
|
foreach my $status (keys %count) { |
302 |
|
#foreach my $status (keys %cols) { |
303 |
|
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 ""); |
304 |
|
# *1 in line above is a cludge to display correct number on |
305 |
|
# occurences on graph and without one on legend! If you remove * op |
306 |
|
# it will first evaluate bar sub (thus increasing number by one) and |
307 |
|
# then display number (wrongly). |
308 |
|
} |
309 |
|
print "</table>\n<p>Reload <a href=\"",$q->url(-relative=>1),"\">current</a>.</p></body></html>"; |
310 |
|
|