#!/usr/athena/bin/perl 

require 'ctime.pl';

%mondaytrans = ("Jan", 0,
          "Feb", 31,
          "Mar", 59,
          "Apr", 90,
          "May", 120,
          "Jun", 151,
          "Jul", 181,
          "Aug", 212,
          "Sep", 243,
          "Oct", 273,
          "Nov", 304,
          "Dec", 334);


open (LOG, "/afs/fishwrap.mit.edu/export/data/log/PaperLog") ||
die "Error----  I can't seem to get at the Paperlog: $!";
while(<LOG>)
	{
	next if !/paper/;
	($jpaper, $user, $weekday, $month, $day, $time, @trash) = split;
	$day = $day+$mondaytrans{$month};
	($hour, $minute, $sec) = split(/:/, $time);
	$hours{int($hour)}++;
	$newusers{$day}++ unless $users{$user};
	$users{$user}++;
	$days{$day}++;
	$udays{$day}++ unless $foo{$day, $user};
	$foo{$day, $user} = 1;
	$firsttime{$user} = "$day:$time" unless $firsttime{$user};
	$lasttime{$user} = "$day:$time";
	}


$nUsers = (@us = keys %users);

for $u (@us){
	$Histogram[$users{$u}]++;
	# avg num seconds b/t papers
	if ($users{$u} > 1)
	{
		($firstdays, $firsthours, $firstminutes, $firstseconds)
		= split(/:/,$firsttime{$u});
		$firstsec = $firstdays * 3600 * 24 + 
		  $firsthours * 3600 + $firstminutes * 60 + $firstseconds;

		($lastdays, $lasthours, $lastminutes, $lastseconds)
		= split(/:/,$lasttime{$u});
		$lastsec = $lastdays * 3600 * 24 + 
		  $lasthours * 3600 + $lastminutes * 60 + $lastseconds;
		
		$avg = ($lastsec - $firstsec) / $users{$u};
		push(@timehist, $avg);
	}
}

print "Total number of distinct users is $nUsers\n";
print "Number of times used\n";
for $h (1..$#Histogram){
	printf("%3d", $h);
	printf(" (%3d)", $Histogram[$h]);
	print "-" x ($Histogram[$h]/4);
	print "\n";
}

$nDays = (@theDays = keys %days);

#print "I get $nDays distinct days\n";
#print "Last day is $days[$nDays] \n";

for $d (@theDays)
{
	$dayGram[$days{$d}]++;
}

print "Usage per day\n";
for $dg (1..150)
{
	printf("Day %2d", $dg);
	print "-" x ($days{$dg}/7);
	
	print " $days{$dg}\n";
}
print("Distinct users per day\n");
for $dg (1..150)
{
	printf("Day %2d", $dg);
	print "-" x ($udays{$dg}/4);
	
	print " $udays{$dg}\n";
}
print("Average number of uses per user by day\n");
for $dg (1..150)
{
	printf("Day %2d", $dg);
	print "-" x ($days{$dg}/$udays{$dg}*10) unless !$udays{$dg};
	
	printf(" %.2f",$days{$dg}/$udays{$dg}) unless !$udays{$dg};
	print("\n");
}
print("New users per day\n");
for $dg (1..150)
{
	printf("Day %2d", $dg);
	print "-" x $newusers{$dg};
	
	printf(" $newusers{$dg}");
	print("\n");
}


print "average hours between uses\n";
$binsize = 7200;
for $time (@timehist) {
	$histogram[int($time/$binsize)]++;
}

for $h (0..$#histogram){
	printf("%3d-%3d ",$h*$binsize/3600, ($h*$binsize/3600)+$binsize/3600); 
	print "-" x ($histogram[$h]/2);
	print " $histogram[$h]\n";
}
print("Usage by hour of day\n");
for $h (0..23){
	printf("%2d00 ", $h);
	print "-" x ($hours{$h}/20);
	print(" $hours{$h}\n");
}


close(LOG);
