#!/afs/athena/contrib/perl/p

$threshhours = 3;
$threshold = 3600*$threshhours;
$| = 1;
$pat = shift;

while(<>){
#    ($mon, $day, $time, $anx, $server, $ip, $method, $path, $prot) = split;
#    ($ip, $trash, $mon, $day, $time, $year, $method, $path, $prot) = split;
    ($ip, $trash, $trash, $date, $tzone, $method, $path, $prot, $stat, $size)=
	split;
    $date = substr($date, 1);
    
    next if $path=~/gif$/i;
    next if $path=~/jpg$/i;

    if($ltime = $host{$ip}){
	$session{$ip}++;
	$fullpath{$ip}.="$path\n";
	$host{$ip} = $date;
	$url{$ip} = $path;
	($trash, $hour, $min, $sec) = split(":", $date);
	$nowtime = $sec + $min*60+ $hour*3600;

	($trash, $hour, $min, $sec) = split(":", $ltime);
	$lasttime = $sec + $min*60+ $hour*3600;

	$ftime = $first{$ip};

	($trash, $hour, $min, $sec) = split(":", $ftime);
	$firsttime = $sec + $min*60+ $hour*3600;

#	print("$ip: Now; $nowtime, Last: $lasttime, First: $firsttime\n");
#	print("*********  $_\n") if $firsttime > 86000;

	$hist[int(($nowtime-$lasttime)/180)]++ if ($nowtime > $lasttime);

	if($nowtime-$lasttime > $threshold){
	    if(($session{$ip} > 2) && ($fullpath{$ip} =~ /$pat/)){
		print("Session time from $ip: ",
		      $lasttime - $firsttime, " secs, ",
		      $session{$ip}-1,
		      " docs. (", $nowtime-$lasttime,")\n\tvia $entry{$ip}",
		      " $nhosts unique hosts as of $date\n");
		$fp = $fullpath{$ip};
		$fp =~ s/\n/\n\t/g;
		print "\t$fp\n";
		$docs += ($session{$ip}-1);
		$dhist[$session{$ip}-1]++;
	    }
	    else{
		print("--", $nowtime-$lasttime,"--", "  One doc retrieved by $ip: $url{$ip}\n\n");
		$docs++;
		$dhist[1]++;
	    }
	    $totaltime += ($lasttime - $firsttime);
	    $totaldocs += ($session{$ip} -2);
	    $first{$ip} = $date;
	    $entry{$ip} = $path;
	    $fullpath{$ip} ="$path\n";
	    $sessions++;
	    $session{$ip}= 1;
	}
	
    }
    else{
	$first{$ip} = $date;
	$nhosts++;
	$sessions++;
	$entry{$ip} = $path;
	$fullpath{$ip} ="$path\n";
	$host{$ip} = $date;
	$url{$ip} = $path;
	$session{$ip}=1;
    }
}
print("-------------------------------------------------\n");

for $ipadd (keys %host){
    $ltime = $host{$ipadd};

    ($trash, $hour, $min, $sec) = split(":", $ltime);
    $lasttime = $sec + $min*60+ $hour*3600;
    
    $ftime = $first{$ipadd};
    
    ($trash, $hour, $min, $sec) = split(":", $ftime);
    $firsttime = $sec + $min*60+ $hour*3600;
    print("=====") if $session{$ipadd} > 10;
    print("Session time from $ipadd: ",
	  $lasttime - $firsttime, " secs, ",
	  $session{$ipadd},
	  " docs. (", ($lasttime-$firsttime)/($session{$ip}-2), ")\n");
    print("=====") if $session{$ipadd} > 10;
    print("\tvia $entry{$ipadd}\n");
    $docs += $session{$ipadd};
    $dhist[$session{$ipadd}]++;
    $totaltime += ($lasttime - $firsttime);
    $totaldocs += ($session{$ip} -2);
}
print("\"Average\" time per document: ", $totaltime/$totaldocs, "\n");
print("$totaltime, $totaldocs\n");

for $n (0..$#hist){
    $o = $n*3;
    $m = ($n+1)*3;
    $dashes = '-' x $hist[$n];
    $dashes = $hist[$n] if $hist[$n] > 70;
    print("$o-$m: $dashes\n");
}

for $n (1..$#dhist){
    $dashes = '-' x ($dhist[$n]);
    $dashes = $dhist[$n] if (($dhist[$n]) > 70);
    print("$n: $dashes\n");
}

print("Total sessions: $sessions, Total docs: $docs, Average docs/session: ",
      $docs/$sessions, "\n");



