/[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

Contents of /db2gantt.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (show annotations)
Thu Jun 26 20:36:48 2003 UTC (20 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.27: +15 -2 lines
add links to repgen.pl generated html pages

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 CGI::Carp qw(fatalsToBrowser);
9 use POSIX qw(strftime);
10
11 my $debug = 0;
12
13 # comment out following field if you don't want links to omni reports
14 # or haven't installed repgen.pl
15 my $repgen_url="/omni/"; # URL to pages with rptgen.pl
16
17 my $width = 600; # width of bar
18 my $height = 15; # height of bar
19 my $use_js = 1; # use JavaScript pop-up
20
21 # status colors
22 my %cols = (
23 'In Progress' => '0,255,0',
24 'In Progress/Failures' => '192,64,192',
25 'In Progress/Errors' => '255,128,128',
26 'Queuing' => '255,255,0',
27 'Queuing/Errors' => '255,192,64',
28 'Aborted' => '255,0,0',
29 'Failed' => '255,0,0',
30 'Completed' => '64,255,64',
31 'Completed/Errors' => '255,128,0',
32 'Completed/Failure' => '128,0,128',
33 'Mount Request' => '128,128,255',
34 'Mount/Errors' => '255,64,128',
35 'Mount/Failures' => '255,128,192',
36 );
37
38 my $int_t = (12 * 60 * 60); # interval to display on one screen
39 my $min_l = 3; # min length of bar segment (in pixels)
40
41 #--- no user servicable parts below this line
42
43 my $q=new CGI;
44
45 my ($from_t,$to_t) = (time()-$int_t,time());
46
47 $from_t = param('from_t') if (param('from_t'));
48 $to_t = param('to_t') if (param('to_t'));
49
50 if (param('f-1')) {
51 $from_t = $from_t - param('int_f-1') || $int_t;
52 } elsif (param('f+1')) {
53 $from_t = $from_t + param('int_f+1') || $int_t;
54 } elsif (param('t-1')) {
55 $to_t = $from_t - param('int_t-1') || $int_t;
56 } elsif (param('t+1')) {
57 $to_t = $from_t + $int_t;
58 }
59
60 # time range
61 $int_t = m_round($int_t);
62
63 # round to nearest minute
64 sub m_round {
65 my $t = shift @_;
66 return ($t - ($t % 60));
67 }
68
69 $to_t = m_round($to_t);
70 $from_t = m_round($from_t);
71
72 my $to=strftime("%Y-%m-%d %H:%M",localtime ($to_t));
73 my $from=strftime("%Y-%m-%d %H:%M",localtime ($from_t));
74
75
76 # keep count of each status
77 my %count;
78
79 if (path_info()) {
80 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";
81 # create picture using GD
82 use GD;
83 my $im = new GD::Image(1,$height);
84 my $back = $im->colorAllocate(255,255,255);
85 $im->transparent($back);
86 my $col = path_info(); $col =~ s,/,,g;
87 my ($r,$g,$b) = split(/,/,$col);
88 $col = $im->colorAllocate($r,$g,$b);
89 $im->fill(0,0,$col);
90 print "Content-Length: ",length($im->png),"\n\n";
91 binmode STDOUT;
92 print $im->png;
93 exit;
94 }
95
96 # open DBI connection
97 my $dbh = DBI->connect("DBI:Pg:dbname=gantt","","") || die $DBI::errstr;
98
99 print "Content-type: text/html
100 Cache-Control: max-age=60, must-revalidate
101
102 <html>
103 <head>
104 <title>OmniBack Gantt: $from - $to</title>
105 <meta HTTP-EQUIV=\"Refresh\" CONTENT=600>";
106 if ($use_js) {
107 print '
108 <script type="text/javascript" language="javascript" src="1k.js"></script>
109 <script type="text/javascript" language="javascript" src="tooltip.js"></script>
110 <script language="javascript" type="text/javascript">
111 onload=function(){
112 T.init()
113 T.follows = false // false by default
114 T.delay = .3 // any nonnegative value (0.7 by default)
115 }
116 </script>
117 ';
118 }
119 print "</head><body>
120 <form action=".$q->url(-relative=>1)." method=get>
121 <input type=hidden name=from_t value=$from_t>
122 <input type=hidden name=to_t value=$to_t>
123 ";
124
125 # all vars ending in *_t have utime in them.
126 #
127 my $len_t = $to_t - $from_t;
128
129 die "interval must be positive and bigger than 1 sec !" if (abs($len_t) < 1);
130
131
132 sub mknav {
133 my $f = shift @_; # from_t
134 my $t = shift @_; # to_t
135 my $d = shift @_; # delta
136 my $ch = shift @_; # char
137
138 return "
139 <select name=int_${d}>
140 <option value=".(12*60*60).">12h</option>
141 <option value=".(24*60*60).">day</option>
142 <option value=".(7*24*60*60).">week</option>
143 </select>
144 <input type=submit name=$d value=\"$ch\">
145 ";
146 #return "<a href=\"".$q->url(-relative=>1)."?from_t=${f}&to_t=${t}\">$ch</a>";
147 }
148
149 print "<table cellspacing=0 cellpadding=0>";
150 print "<tr bgcolor=#e0e0e0><td>Specification</td><td align=left>";
151 print mknav($from_t,$to_t,'f-1','&lt;&lt;'),$from;
152 print mknav($from_t,$to_t,'f+1','&gt;&gt;') if ($from_t+$int_t < $to_t);
153 print "</td><td align=right>";
154 print mknav($from_t,$to_t,'t-1','&lt;&lt;') if ($to_t+$int_t > $from_t);
155 print $to,mknav($from_t,$to_t,'t+1','&gt;&gt;'),"</td></tr>\n";
156
157 # draw hour grid
158 sub hour_grid {
159 print "<tr><td align=right><small>hour grid</small></td><td colspan=2>";
160 my @c = ("255,255,128","255,192,128");
161 my $hr=strftime("%H",localtime ($from_t));
162 sub hour_bar {
163 my ($t,$c,$hr) = @_;
164 my $clock_hr = $hr % 24;
165 my $alt = sprintf("%02d:00",$clock_hr);
166 if ($clock_hr == 0) {
167 my $pix_hr = int(1 / $width);
168 print color_bar($pix_hr,"0,0,0",strftime("%Y-%m-%d", localtime($from_t + $hr*3600)));
169 print color_bar($t-$pix_hr,$c,$alt);
170 } else {
171 print color_bar($t,$c,$alt);
172 }
173 }
174 hour_bar(3600 - $from_t % 3600,$c[0],$hr++);
175 for (my $i=1; $i<int(($to_t-$from_t)/3600); $i++) {
176 hour_bar(3600,$c[1],$hr++);
177 push @c, shift @c;
178 }
179 hour_bar($from_t % 3600,$c[1],$hr);
180 print "</td></tr>\n";
181 }
182
183 hour_grid();
184
185 my $fix_d = 0; # used to fix graph len (in pixels)
186 my $fix_s = 0; # used to collect round errors of size
187
188 sub bar {
189 my $l = shift @_;
190 my $status = shift @_;
191 my $alt = shift @_;
192
193 my $col;
194
195 if ($status) {
196 if ($cols{$status}) {
197 $col .= $cols{$status};
198 } else {
199 $col .= "0,0,0"; # unknown status, black
200 }
201 $count{$status}++;
202 } else {
203 # $col .= '240,240,240';
204 $col .= '220,220,220';
205 }
206 return color_bar($l,$col,$alt,$min_l);
207 }
208
209 sub color_bar {
210 my $l = shift @_; # lenght of event utime
211 my $col = shift @_ || '240,240,240'; # default color (filler)
212 my $alt = shift @_ || undef;
213 my $min_l = shift @_ || 1;
214 my $h = shift @_ || $height;
215
216 my $size = $l / ($len_t / $width);
217 $fix_s += $size - int($size);
218 $size=int($size);
219 # add rounding error to size
220 if ($fix_s > 1) {
221 my $i = $fix_s ; $i = int($i);
222 $fix_s -= $i;
223 $size += $i;
224 }
225
226 if ($alt && $size < $min_l) {
227 $fix_d += ($min_l - $size);
228 print STDERR "fix_d: $fix_d\n" if ($debug);
229 $size = $min_l;
230 }
231 if ($fix_d && $size > $fix_d+$min_l) {
232 $size -= $fix_d;
233 $fix_d = 0;
234 print STDERR "fix_d: $fix_d\n" if ($debug);
235 }
236
237 print STDERR "bar[$col] len:$l s scale:",($len_t/$width)," size:$size px<br> alt:$alt\n" if ($debug);
238
239 my $html;
240
241 my ($html_end, $img_end) = ("","");
242 if ($repgen_url && $alt =~ m#(\d\d\d\d)/(\d\d)/(\d\d)-(\d+)#) {
243 $html .= "<a href=\"$repgen_url/$1-$2-$3-$4.html\">";
244 $html_end .= "</a>";
245 $img_end .= " border=0";
246 }
247
248 $html .= "<img src=\"".$q->url(-relative=>1)."/$col\" width=\"$size\" height=\"$h\"";
249
250 if ($use_js && $alt) {
251 $html .= " onmouseover=\"T('$alt')\" onmouseout=\"T()\"";
252 } elsif ($alt) {
253 $html .= " alt=\"$alt\"";
254 }
255 $html .= "$img_end>$html_end";
256
257 return($html);
258 }
259
260 my $sql = "select start,finish,specification,status,user_group_host,
261 type,sessionid,device,host
262 from gantt
263 where (start < '$from' and finish > '$from') or
264 (start > '$from' and start < '$to')
265 order by device,specification,start
266 ";
267
268 my $sth = $dbh->prepare($sql) || die "sql: $sql ".$dbh->errstr;
269 print STDERR "sql: $sql\n" if ($debug);
270
271 my %spec; # specification hash
272
273 my $curr_spec = "";
274 my $curr_t = $from_t;
275
276 $sth->execute() || die "sql: $sql ".$dbh->errstr;
277
278 my $dev; # current device
279 my @dev_t; # all minutes in one hour [1440]
280 my @dev_sum_t;
281
282 sub sum_bar {
283 my $label = shift @_;
284 my @dev_t = @_;
285 # display device summary
286 my $max_use = 0;
287 for (my $i=0; $i < $len_t; $i++) {
288 $max_use = $dev_t[$i] if (defined $dev_t[$i] && $dev_t[$i] > $max_use);
289 }
290 print "<tr><td align=right><small>$label [$max_use]</small></td><td colspan=2>";
291 my $last_var = 0;
292 my $len = 0;
293 for (my $i=1; $i<= ($to_t-$from_t); $i++) {
294 my $v = $dev_t[$i] || 0;
295 if ($v == $last_var) {
296 $len++;
297 } else {
298 my $h = $last_var / $max_use * $height;
299 my $c = 255 - int (255 * $last_var / $max_use);
300 print color_bar($len,"$c,$c,$c","$last_var concurrent jobs",1,$h+1);
301 $len = 0;
302 $last_var = $v;
303 }
304 }
305 # fix division by zero
306 $max_use++ if ($max_use == 0);
307 my $h = $last_var / $max_use * $height;
308 my $c = 255 - int (255 * $last_var / $max_use);
309 print color_bar($len,"$c,$c,$c","$last_var concurrent jobs",1,$h+1) if ($len);
310 print "</td></tr>";
311 }
312
313 my @line_t; # all seconds for this specification
314 my @line_arr = (""); # all segments on line which are not fillers
315
316 sub html_spec {
317 print "<tr><td>$curr_spec</td><td colspan=2>";
318
319 my $last_var = 0;
320 my $len = 0;
321 my $i;
322 for (my $t=0; $t<=$len_t; $t++) {
323 $i = $line_t[$t] || 0;
324 if ($i == $last_var) {
325 $len++;
326 } else {
327 if ($last_var == 0) {
328 print bar($len);
329 } else {
330 print bar($len,split(/\t/,$line_arr[$last_var],2));
331 }
332 $len = 0;
333 $last_var = $i;
334 }
335 }
336 print bar($len,split(/\t/,$line_arr[$i],2));
337
338 print "</td></tr>\n";
339 }
340
341 while(my $row = $sth->fetchrow_hashref) {
342 if ($row->{specification} ne $curr_spec) {
343
344 if ($curr_t < $to_t && $curr_spec) {
345 my $t = $to_t - $curr_t;
346 print STDERR "[end filler $curr_t:$t]" if ($debug);
347 # print bar($t);
348 }
349
350 # print "</td></tr>\n" if ($curr_t != $from_t);
351
352
353 # init vars for next line
354 ($fix_s,$fix_d) = (0,0); # init fix vars for bar
355
356 if ($curr_spec ne "") {
357 $curr_spec =~ s/ +/&nbsp;/g;
358 html_spec;
359 }
360
361 $curr_t = $from_t; # init timeline
362 $curr_spec = $row->{specification};
363
364 @line_t = ();
365 @line_arr = ( "" );
366
367 if ($dev && @dev_t && $dev ne $row->{device}) {
368 sum_bar("summary for $dev",@dev_t);
369 $dev = $row->{device};
370 @dev_t = ();
371 } elsif (! $dev) {
372 $dev = $row->{device};
373 }
374 }
375
376 my $start_t = str2time($row->{start});
377 my $fin_t = str2time($row->{finish});
378
379 # Can I squeeze here 1 pixel of time (many seconds) ?
380 if ($start_t > $curr_t + ($len_t / $width)) {
381 my $t = $start_t - $curr_t;
382 print STDERR "[middle filler $curr_t:$t]" if ($debug);
383 # print bar($t);
384 $curr_t = $start_t;
385 } else {
386 # prepend too few seconds to next event
387 $start_t = $curr_t;
388 }
389
390 my $len = $fin_t - $start_t;
391 # $len = $len_t if ($len > $len_t);
392 my $less = '';
393 my $more = '';
394
395 if ($start_t < $from_t) {
396 # $len += ($from_t - $start_t);
397 $len = ($fin_t - $curr_t);
398 $less = "<<";
399 }
400
401 if ($fin_t > $to_t) {
402 # $len -= ($fin_t - $to_t);
403 $len = ($to_t - $curr_t);
404 $more = ">>"; # event now shown whole
405 }
406
407 print STDERR "[$less",$row->{status}," $curr_t:$len$more]" if ($debug);
408
409 my $alt = $row->{start}." - ".$row->{finish}."<br>";
410 $alt =~ s/:\d\d\.\d+//g;
411 $alt =~ s/\s+/&nbsp;/g;
412 $alt .= $row->{specification}."<br>".
413 $row->{type}." <b>".$row->{status}."</b><br>".
414 $row->{user_group_host}." <i>".$row->{sessionid}."</i><br>".
415 $row->{device}."&nbsp;on&nbsp;".$row->{host};
416 # print bar($len,$row->{status},$alt);
417
418 push @line_arr,$row->{status}."\t".$alt;
419 # store use of devices for each minute
420 for (my $s=0; $s<$len; $s++) {
421 $line_t[$curr_t-$from_t+$s] = $#line_arr;
422 $dev_t[$curr_t-$from_t+$s]++;
423 $dev_sum_t[$curr_t-$from_t+$s]++;
424 }
425
426 $curr_t += $len;
427
428 # print Dumper($row);
429
430 }
431
432 if ($curr_t == $from_t) { # no entries in database!
433 # print "<tr><td></td><td colspan=2>";
434 }
435
436 if ($curr_t < $to_t ) {
437 my $t = $to_t - $curr_t;
438 print STDERR "[last_line filler $curr_t:$t]" if ($debug);
439 }
440
441 undef $sth;
442 $dbh->disconnect;
443
444 html_spec();
445 sum_bar("summary for $dev",@dev_t);
446 hour_grid();
447 sum_bar("summary for all devices",@dev_sum_t);
448 print "</table>";
449
450
451 # label and usage
452
453 print "<hr>\nColors for statuses and usage (#):\n";
454 print "<table border=0><tr bgcolor=#e0e0e0><th>status</th><th>#</th><th>color</th></tr>\n";
455 my $max = 0;
456 foreach my $status (keys %count) {
457 $max = $count{$status} if ($count{$status} > $max);
458 }
459 $len_t = $max ; # disable bar scaling
460
461 foreach my $status (keys %count) {
462 #foreach my $status (keys %cols) {
463 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 "");
464 # *1 in line above is a cludge to display correct number on
465 # occurences on graph and without one on legend! If you remove * op
466 # it will first evaluate bar sub (thus increasing number by one) and
467 # then display number (wrongly).
468 }
469 print "</table></form>\n<p>Reload <a href=\"",$q->url(-relative=>1),"\">current</a> or see <a href=\"db2gantt_help.html\">help</a>.</p></body></html>";

  ViewVC Help
Powered by ViewVC 1.1.26