#!/usr/bin/perl -w

use strict;
use Time::Local;
use Benchmark;

my (@allrecords);
my ($startday);
my ($startweek);
my ($record);
my (%total);
my ($grandtotal);
my ($sum);
my (@hourtotal);
my (@daytotal);
my (@weektotal);

my $tree = { "name" => "",
	     "count" => 0,
	     "children" => {}
};

while (<>) {
    $record = [split];
    my $time = $record->[3];
    my $file = $record->[5];
    if (!$startday) {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	   localtime($time);
	$startday = timelocal(0,0,0,$mday,$mon,$year);
	$startweek = $startday - (24*3600 * $wday);
    }

    $total{$file} ++;
    my (@pc) = split("/", $file);
    my $i; 
    for ($i = 0; $i < $#pc; $i++) {
	$pc[$i] .= "/";
    }
    if ($pc[0] ne "/") {
	unshift(@pc, "/", "no-leading-slash");
    }
    my $node = $tree;
    for ($i = 0; $i <= $#pc; $i++) {
	$node->{name} = $pc[$i];
	$node->{count} ++;
	if ($i < $#pc) {
		
	    if (!defined($node->{children}->{$pc[$i+1]})) {
		$node->{children}->{$pc[$i+1]} = { "name" => "",
					    "count" => 0,
					    "children" => {}
					  };
	    }
	    $node = $node->{children}->{$pc[$i+1]};
	}
    }
    $grandtotal ++;
    $hourtotal[($time - $startday) / 3600 % 24]++;
    $daytotal[($time - $startday) / (24*3600)]++;
    $weektotal[($time - $startweek) / (7*24*3600)]++;
}

print "<H1>Document access statistics</H1>\n";

print "<h2>Total document accesses</h2> $grandtotal\n";

print "<h2>Week history</h2> \n";
print "<table border>\n";
print "<tr><th align=left>Week</th><th align=right>Count</th></tr>\n";
my $i;
for ($i = 0; $i <= $#weektotal; $i++) {
    printf "<tr><td align=left>%d</td><td align=right>%d</td></tr>\n",
	    $i, $weektotal[$i];
}
print "</table>\n";
print "\n";

print "<h2>Day history</h2> \n";
print "<table border>\n";
print "<tr><th align=left>Day</th><th align=right>Count</th></tr>\n";
for ($i = 0; $i <= $#daytotal; $i++) {
    printf "<tr><td align=left>%d</td><td align=right>%d</td></tr>\n",
    		$i, $daytotal[$i];
}
print "</table>\n";
print "\n";

print "<h2>Hour of day history</h2> \n";
print "<table border>\n";
print "<tr><th align=left>Hour</th><th align=right>Count</th></tr>\n";
for ($i = 0; $i <= $#hourtotal; $i++) {
    printf "<tr><td align=left>%02d</td>" .
               "<td align=right>%d</td>" .
               "<td align=right>%.1f</td>" .
	   "</tr>\n",
      	$i, $hourtotal[$i], $hourtotal[$i] / ($#daytotal + 1);
}
print "</table>\n";
print "\n";

print "<h2>Hour of day history</h2> \n";
print "<table border>\n";
print "<tr><th align=left>File</th>" .
	   "<th align=right>Count</th>" .
	   "<th align=right>Pct</th>" .
	   "<th align=right>Sum</th>" .
	   "<th align=right>Pct</th>" .
      "</tr>\n";
for my $i (sort {$total{$b} <=> $total{$a}} keys (%total)) {
    $sum += ${total{$i}};
    printf "<tr><td align=left>%s</td>" .
               "<td align=right>%d</td>" .
               "<td align=right>%.1f</td>" .
               "<td align=right>%d</td>" .
               "<td align=right>%.1f</td>" .
	   "</tr>\n",
    	$i, ${total{$i}}, ($total{$i} * 100 / $grandtotal), $sum, ($sum * 100 / $grandtotal);
    
}
print "</table>\n";
print "\n";

my @treetable;
my $maxdepth = 0;
sub maketreetable {
    my ($node, $depth, $cutfrac, $oldcut) = (@_);

    push(@treetable, [ $depth, $node->{name}, $node->{count} ]);
    if ($depth > $maxdepth) { $maxdepth = $depth };

    if (defined($node->{count}) && $node->{count} != 0 && $oldcut != 0 && 
        $node->{count} * $cutfrac > $oldcut
    ) {
	$cutfrac = $oldcut / $node->{count};
    }
    my $cutoff = $node->{count} * $cutfrac;


    my $i;
    for $i (sort {$node->{children}->{$b}->{count} <=> $node->{children}->{$a}->{count}} keys %{$node->{children}}) {
	if ($node->{children}->{$i}->{count} < $cutoff) {last}
	maketreetable($node->{children}->{$i}, $depth + 1, $cutfrac*1.5, $cutoff);
    }
}

maketreetable($tree, 0, 0.01, 0);
print "<table border>\n";
for $i (@treetable) {
    my $j;

    print "  <tr>\n";
    for ($j = 0; $j < $i->[0]; $j++) {
	print "    <td></td>\n"
    }
    print "    <td>-></td>\n";
    print "    <td colspan=", $maxdepth - $i->[0] + 1, ">", $i->[1],"</td>\n";
    print "    <td align=right>", $i->[2], "</td>\n";
    print "  </tr>\n"
}
print "</table>\n";
