1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use DBI; |
5 |
use Data::Dumper; |
6 |
use Date::Parse; |
7 |
use CGI qw/:standard/; |
8 |
use POSIX qw(strftime); |
9 |
|
10 |
my $debug = 1; |
11 |
my $width = 600; |
12 |
my $use_js = 1; |
13 |
|
14 |
# status colors |
15 |
my %cols = ( |
16 |
'In Progress' => '0,255,0', |
17 |
'In Progress/Failure' => '255,64,255', |
18 |
'In Progress/Errors' => '255,64,0', |
19 |
'Queuing' => '255,255,0', |
20 |
'Aborted' => '255,0,0', |
21 |
'Failed' => '255,0,0', |
22 |
'Completed' => '64,255,64', |
23 |
'Completed/Errors' => '255,128,0', |
24 |
'Completed/Failure' => '128,0,128', |
25 |
'Mount Request' => '128,128,255', |
26 |
); |
27 |
|
28 |
#--- no user servicable parts below this line |
29 |
|
30 |
# time range |
31 |
|
32 |
my $day_t = (24 * 60 * 60); # 24h interval |
33 |
|
34 |
my ($from_t,$to_t) = (time()-$day_t,time()); |
35 |
|
36 |
$to_t = param('to_t') if (param('to_t')); |
37 |
$from_t = param('from_t') if (param('from_t')); |
38 |
|
39 |
my $to=strftime("%Y-%m-%d %H:%M:%S",localtime ($to_t)); |
40 |
my $from=strftime("%Y-%m-%d %H:%M:%S",localtime ($from_t)); |
41 |
|
42 |
|
43 |
# keep count of each status |
44 |
my %count; |
45 |
|
46 |
if (param('pic')) { |
47 |
print "Content-type: image/png\nCache-Control: max-age=3600, must-revalidate\n\n"; |
48 |
# create picture using GD |
49 |
use GD; |
50 |
my $im = new GD::Image(1,8); |
51 |
my $back = $im->colorAllocate(255,255,255); |
52 |
$im->transparent($back); |
53 |
my ($r,$g,$b) = split(/,/,param('pic')); |
54 |
my $col = $im->colorAllocate($r,$g,$b); |
55 |
$im->fill(0,0,$col); |
56 |
binmode STDOUT; |
57 |
print $im->png; |
58 |
exit; |
59 |
} |
60 |
|
61 |
print "Content-type: text/html |
62 |
|
63 |
<html> |
64 |
<head> |
65 |
<title>OmniBack Gantt: $from - $to</title>"; |
66 |
if ($use_js) { |
67 |
print ' |
68 |
<script type="text/javascript" language="javascript" src="1k.js"></script> |
69 |
<script type="text/javascript" language="javascript" src="tooltip.js"></script> |
70 |
<script language="javascript" type="text/javascript"> |
71 |
onload=function(){ |
72 |
T.init() |
73 |
T.follows = false // false by default |
74 |
T.delay = .3 // any nonnegative value (0.7 by default) |
75 |
} |
76 |
</script> |
77 |
'; |
78 |
} |
79 |
print "</head><body>"; |
80 |
|
81 |
# all vars ending in *_t have utime in them. |
82 |
# |
83 |
my $len_t = $to_t - $from_t; |
84 |
|
85 |
die "interval must be positive and bigger than 1 sec !" if ($len_t < 1); |
86 |
|
87 |
my $dbh = DBI->connect("DBI:Pg:dbname=gantt","","") || die $DBI::errstr; |
88 |
my $q=new CGI; |
89 |
|
90 |
|
91 |
sub mknav { |
92 |
my $f = shift @_; # from_t |
93 |
my $t = shift @_; # to_t |
94 |
my $ch = shift @_; # char |
95 |
|
96 |
return "<a href=\"".$q->url(-relative=>1)."?from_t=${f}&to_t=${t}\">$ch</a>"; |
97 |
} |
98 |
|
99 |
print "<table>"; |
100 |
print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>"; |
101 |
print mknav(($from_t-$day_t),$to_t,'<small><<</small>'),$from; |
102 |
print mknav(($from_t+$day_t),$to_t,'<small>>></small>') if ($from_t+$day_t < $to_t); |
103 |
print "</td><td align=right>"; |
104 |
print mknav($from_t,($to_t-$day_t),'<small><<</small>') if ($to_t-$day_t > $from_t); |
105 |
print $to,mknav($from_t,($to_t+$day_t),'<small>>></small>'),"</td></tr>\n"; |
106 |
|
107 |
my $fix_d = 0; # used to fix graph len |
108 |
my $min_l = 3; # min length; |
109 |
|
110 |
sub bar { |
111 |
my $l = shift @_; # lenght of event utime |
112 |
my $status = shift @_ || undef; # what to draw |
113 |
my $alt = shift @_ || undef; |
114 |
|
115 |
my $size = int($l / ($len_t / $width)); |
116 |
if ($size < $min_l) { |
117 |
$size = $min_l; |
118 |
$fix_d += $min_l; |
119 |
} |
120 |
if ($fix_d && $size > $fix_d+$min_l) { |
121 |
$size -= $fix_d; |
122 |
$fix_d = 0; |
123 |
} |
124 |
|
125 |
print STDERR "l[$status]:$l scale:",($len_t/$width)," size:$size<br> alt:$alt\n" if ($debug); |
126 |
|
127 |
my $html = "<img src=\"".$q->url(-relative=>1)."?pic="; |
128 |
|
129 |
if ($status) { |
130 |
$html .= $cols{$status}; |
131 |
$count{$status}++; |
132 |
} else { |
133 |
# $html .= '240,240,240'; |
134 |
$html .= '220,220,220'; |
135 |
} |
136 |
|
137 |
$html .= "\" width=\"$size\" height=\"12\""; |
138 |
if ($use_js && $alt) { |
139 |
$html .= " onmouseover=\"T('$alt')\" onmouseout=\"T()\""; |
140 |
} elsif ($alt) { |
141 |
$html .= " alt=\"$alt\""; |
142 |
} |
143 |
$html .= ">"; |
144 |
|
145 |
return($html); |
146 |
} |
147 |
|
148 |
my $sql = "select start,finish,specification,status |
149 |
from gantt |
150 |
where (start < '$from' and finish > '$from') or |
151 |
(start > '$from' and start < '$to') |
152 |
order by specification |
153 |
"; |
154 |
|
155 |
my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr; |
156 |
|
157 |
my %spec; # specification hash |
158 |
|
159 |
my $curr_spec; |
160 |
my $curr_t = $from_t; |
161 |
|
162 |
$sth->execute() || die "sql: $sql ".$dbh->errstr; |
163 |
|
164 |
while(my $row = $sth->fetchrow_hashref) { |
165 |
if ($row->{specification} ne $curr_spec) { |
166 |
|
167 |
if ($curr_t < $to_t && $curr_spec) { |
168 |
my $t = $to_t - $curr_t; |
169 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
170 |
print bar($t); |
171 |
} |
172 |
|
173 |
print "</td></tr>\n" if ($curr_t != 0); |
174 |
print "<tr><td>", $row->{specification},"</td><td colspan=2>"; |
175 |
|
176 |
$curr_t = $from_t; # init timeline |
177 |
$curr_spec = $row->{specification}; |
178 |
|
179 |
} |
180 |
|
181 |
my $start_t = str2time($row->{start}); |
182 |
my $fin_t = str2time($row->{finish}); |
183 |
|
184 |
if ($start_t > $curr_t) { |
185 |
my $t = $start_t - $curr_t; |
186 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
187 |
print bar($t); |
188 |
$curr_t = $start_t; |
189 |
} |
190 |
|
191 |
my $len = $fin_t - $start_t; |
192 |
# $len = $len_t if ($len > $len_t); |
193 |
my $less = ''; |
194 |
my $more = ''; |
195 |
|
196 |
if ($start_t < $from_t) { |
197 |
# $len += ($from_t - $start_t); |
198 |
$len = ($fin_t - $curr_t); |
199 |
$less = "<<"; |
200 |
} |
201 |
|
202 |
next if ($fin_t > $to_t); |
203 |
|
204 |
if ($fin_t > $to_t) { |
205 |
# $len -= ($fin_t - $to_t); |
206 |
$len = ($to_t - $curr_t); |
207 |
$more = ">>"; # event now shown whole |
208 |
} |
209 |
|
210 |
print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug); |
211 |
print bar($len,$row->{status},$row->{start}." - ".$row->{finish}." ".$row->{status}); |
212 |
|
213 |
$curr_t += $len; |
214 |
|
215 |
# print Dumper($row); |
216 |
|
217 |
} |
218 |
|
219 |
if ($curr_t < $to_t ) { |
220 |
my $t = $to_t - $curr_t; |
221 |
print STDERR "[filler $curr_t:$t]" if ($debug); |
222 |
print bar($t); |
223 |
} |
224 |
|
225 |
undef $sth; |
226 |
$dbh->disconnect; |
227 |
|
228 |
print "</td></tr>\n</table>"; |
229 |
|
230 |
|
231 |
# label and usage |
232 |
$len_t = 50; # disable bar scaling |
233 |
|
234 |
print "<hr>\nColors for statuses and usage (#):\n"; |
235 |
print "<table border=0><tr bgcolor=#e0e0e0><th>status</th><th>#</th><th>color</th></tr>\n"; |
236 |
foreach my $status (keys %count) { |
237 |
#foreach my $status (keys %cols) { |
238 |
print "<tr bgcolor=#e0e0e0><td><small>$status</small></td><td><small>",$count{$status},"</small></td><td>",bar($count{$status},$status),"</td></tr>\n" if ($status ne ""); |
239 |
} |
240 |
print "</table>\n</body></html>"; |
241 |
|