#!/usr/local/bin/perl5 -w

use Getopt::Std;

getopts('h');

$div = $opt_h ? 3600 : 900;
$div2 = $div / 2;

open(HIS, "tail -1000 /news/lib/history|") or die("cannot tail history: $!\n");
$av = 0;
$count = 0;
while ( <HIS> ) {
    chomp;
    @fields = split /\t/;
    next unless $fields[2];
    ($i, $p) = (split(/~/, $fields[1]))[0,2];
    $d = $i - $p;
    next if $d < 0;
    $h = int(($d + $div2) / $div);
    $h = 78 if $h > 78;
    ($file) = split(/ /, $fields[2]);
    $file or
	warn("bad history? $_\n"), next;
    $file =~ s-\.-/-g;
    open(ART, "/news/spool/$file") or
	warn("cannot open $file: $!"), next;
    $path = "";
    while ( <ART> ) {
	chomp;
	if ( /^Path: / ) {
	    $path = $_;
	    last;
	}
    }
    close(ART);
    $path =~ /^Path: [^!]+!([^!]*)(!|$)/ or
	warn("Invalid path header in $file: $path\n"), next;
    $hist[$h]{$1}++;
    $feeders{$1}++;
    push(@all, [$d, $h, $1]);
    $av += $d;
    $min = $d if ( !defined($min) || $d < $min );
    $max = $d if ( !defined($max) || $d > $max );
    ++$count;
    print STDERR $count/100, ", " unless $count % 100;
}
$av /= $count;
close(HIS);
print STDERR "\n";

sub lump ($) {
    my($h) = @_;
    my($t, $v);

    $t = 0;
    foreach $v ( values %$h ) {
	$t += $v;
    }
    $t;
}

$m = 1;
foreach $h ( @hist ) {
    $t = lump($h);
    $m = $t if $t > $m;
}

$sens = $scount = 0;
foreach $f ( keys %feeders ) {
    $sens{$f} = $scount{$f} = 0;
}
foreach $a ( @all ) {
    if ( $a->[1] != 78 && lump($hist[$a->[1]]) > $m / 10 ) {
	$sens += $a->[0];
	$scount++;
	$sens{$a->[2]} += $a->[0];
	$scount{$a->[2]}++;
    }
}
$sens /= $scount if $scount;
foreach $f ( keys %feeders ) {
    if ( $scount{$f} ) {
	$sens{$f} /= $scount{$f};
    }
}

sub tohms {
    my($time) = @_;
    my($h, $m, $s);

    $h = int($time/3600);
    $time -= $h*3600;
    $m = int($time/60);
    $time -= $m*60;
    ($h, $m, $time);
}

@chars = (split(//, q{*@#%+$&=-!|\/<>[]}), '0'..'9','A'..'Z','a'..'z');
@feeders = sort { $feeders{$b} <=> $feeders{$a} } keys %feeders;
foreach $f ( @feeders ) {
    $feedchar{$f} = shift @chars or warn("hey! you have too many feeders!\n");
    printf "Feeder %s (%s): articles %d, sensible lag: %02d:%02d:%02d\n",
	$f, $feedchar{$f}, $feeders{$f}, tohms($sens{$f} || 0);
}
printf "Sensible Lag: %02d:%02d:%02d ($sens, $scount measures)\n",
    tohms($sens);
printf "Minimum  Lag: %02d:%02d:%02d ($min)\n", tohms($min);
printf "Average  Lag: %02d:%02d:%02d ($av)\n", tohms($av);
printf "Maximum  Lag: %02d:%02d:%02d ($max)\n", tohms($max);

$m /= 20;
for $i ( 0 .. 20 ) {
  CHAR:
    for $j ( 0 .. 78 ) {
	$t = (20-$i)*$m;
	if ( lump($hist[$j]) > $t ) {
	    foreach $f ( @feeders ) {
		next unless $hist[$j]{$f};
		$t -= $hist[$j]{$f};
		if ( $t < 0 ) {
		    print $feedchar{$f};
		    next CHAR;
		}
	    }
	    print "?";
	}
	else {
	    print " ";
	}
    }
    print "\n";
}

if ( $opt_h ) {
    print <<FOOTER;
01234567891 1  1    2    2    3    3    4    4    5    5    6    6    7    7 7>
          0 2  5    0    5    0    5    0    5    0    5    0    5    0    5 7
FOOTER
}
else {
    print <<FOOTER;
0   1   2   3   4   5   6   7   8   9   1   1   1   1   1   1   1   1   1   1 >
                                        0   1   2   3   4   5   6   7   8   9
FOOTER
}
printf <<FOOTER, $m;
Histogram of lag of arriving articles. One character is %.2g articles
FOOTER

